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

Fix timedelta JSON serialization on Python 2.6. #2430

Merged
merged 1 commit into from Jan 19, 2015
Merged

Fix timedelta JSON serialization on Python 2.6. #2430

merged 1 commit into from Jan 19, 2015

Conversation

fbochu
Copy link
Contributor

@fbochu fbochu commented Jan 19, 2015

Timedelta serialisation does not work on Python 2.6.

timedelta.total_seconds() is only supported since Python 2.7 (https://docs.python.org/2.7/library/datetime.html#datetime.timedelta.total_seconds), so we need a fallback for python 2.6.

if hasattr(obj, 'total_seconds'):
return six.text_type(obj.total_seconds())
else: # Python 2.6 compatibility
return six.text_type((obj.microseconds + (obj.seconds + obj.days * 24 * 3600) * 10**6) / 10**6)
Copy link
Member

@tomchristie tomchristie Jan 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We isolate any version branching code into compat.py.
Have a function named total_seconds(timedelta) that provides this in compat.py and then call that from here.
Otherwise seems fine.

@tomchristie tomchristie added this to the 3.0.4 Release milestone Jan 19, 2015
@@ -4,7 +4,7 @@
"""

# flake8: noqa
from __future__ import unicode_literals
from __future__ import unicode_literals, division
Copy link
Member

@tomchristie tomchristie Jan 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need this - the only division we've added will always be float on both 2 and 3, since we're dividing by a float.

tomchristie added a commit that referenced this issue Jan 19, 2015
…liser-python26-support

Fix timedelta JSON serialization on Python 2.6.
@tomchristie tomchristie merged commit b3a0a27 into encode:master Jan 19, 2015
1 check passed
@tomchristie
Copy link
Member

tomchristie commented Jan 19, 2015

Nice work, thanks!

@fbochu fbochu deleted the fbochu/timedelta-json-serialiser-python26-support branch Jan 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants