Created
December 29, 2011 16:53
-
-
Save wrunk/1534953 to your computer and use it in GitHub Desktop.
Simplejson 2.3.0 object dictionary encode issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from simplejson import JSONEncoder | |
class ObjectDictWorks(dict): | |
""" Makes a dictionary behave like an object. """ | |
def __getattr__(self, name): | |
try: | |
return self[name] | |
except KeyError: | |
raise AttributeError(name) | |
def __setattr__(self, name, value): | |
self[name] = value | |
class ObjectDictBroke(dict): | |
""" Makes a dictionary behave like an object. But allow a default | |
for getattr never raising exceptions. | |
""" | |
def __getattr__(self, name, default=None): | |
try: | |
return self[name] | |
except KeyError: | |
return default | |
def __setattr__(self, name, value): | |
self[name] = value | |
if __name__ == "__main__": | |
je = JSONEncoder() | |
# This will succeed | |
print je.encode(ObjectDictWorks({'test': 1234})) | |
# This will not | |
print je.encode(ObjectDictBroke({'test': 1234})) | |
# This is the exception raised running under lucid32 ubuntu server | |
# simplejson 2.3.0 python2.7 with c speedups being used. | |
# Also happens with same py and SJ on osx snow leopard and lion. | |
exception_raised = ''' | |
Traceback (most recent call last): | |
File "test.py", line 34, in <module> | |
print je.encode(ObjectDictBroke({'test': 1234})) | |
File "/usr/local/lib/python2.6/dist-packages/simplejson/encoder.py", line 226, in encode | |
chunks = self.iterencode(o, _one_shot=True) | |
File "/usr/local/lib/python2.6/dist-packages/simplejson/encoder.py", line 296, in iterencode | |
return _iterencode(o, 0) | |
TypeError: attribute of type 'NoneType' is not callable | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment