Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: dict value with unsupported type fails silently #2

Closed
quandyfactory opened this issue Feb 3, 2012 · 2 comments
Closed

Bug: dict value with unsupported type fails silently #2

quandyfactory opened this issue Feb 3, 2012 · 2 comments

Comments

@quandyfactory
Copy link
Owner

If you pass in a dict and one of the values is an unsupported type (e.g. a datetime object), the code doesn't raise an exception - it just passes silently over the value without adding it to the XML output. Here's an example that produces the issue:

>>> import dict2xml
>>> import datetime
>>> now = datetime.datetime.today()
>>> mydict = { 'name': 'Joe Blow', 'time': now }
>>> xml = dict2xml.dict2xml(mydict)
>>> print xml
<?xml version="1.0" encoding="UTF-8" ?><root><name type="str">Joe Blow</name></root>
>>> # the 'time' key and its datetime value just silently disappeared

I propose the following fix:

  • Raise an exception in convert_dict() and convert_list() if one of the elements is an unsupported type (currently, only convert() raises an exception).
  • Include an optional parameter that will automatically convert datetimes into UTC strings in the XML output.
  • Include an optional parameter that will automatically convert other unsupported types into their string representations.
@quandyfactory
Copy link
Owner Author

One easy way to handle datetimes is to check an object for hasattr(obj, 'isoformat') and return obj.isoformat() if it does.

@quandyfactory
Copy link
Owner Author

Fixed in release 0.5. Now supports datetime (converts to ISO format string) and set (converts to list) data types, and raises a TypeError exception on an unsupported data type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant