diff options
author | Bob Ippolito <bob@redivi.com> | 2021-08-23 21:32:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-23 21:32:46 -0700 |
commit | 316b399ba5ea2720710203942e1514bd0a9f00f3 (patch) | |
tree | 3e3469eedf611456b8c1a4e57ced45a53314a0fa /simplejson/encoder.py | |
parent | 8dce5b06d3faf9cc439a56dfa3cbdcde2dd8c8ec (diff) | |
parent | 39a269f8ac829a0e8cb7ee5ff3597227e334b4d0 (diff) | |
download | simplejson-316b399ba5ea2720710203942e1514bd0a9f00f3.tar.gz |
Merge pull request #289 from simplejson/fix_is_namedtuple_dict_fu
v3.17.5
Diffstat (limited to 'simplejson/encoder.py')
-rw-r--r-- | simplejson/encoder.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/simplejson/encoder.py b/simplejson/encoder.py index 7ea172e..2f81cab 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -520,7 +520,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, else: _asdict = _namedtuple_as_object and getattr(value, '_asdict', None) if _asdict and callable(_asdict): - chunks = _iterencode_dict(_asdict(), + dct = _asdict() + if not isinstance(dct, dict): + raise TypeError("_asdict() must return a dict, not %s" % (type(dct).__name__,)) + chunks = _iterencode_dict(dct, _current_indent_level) elif _tuple_as_array and isinstance(value, tuple): chunks = _iterencode_list(value, _current_indent_level) @@ -641,7 +644,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, else: _asdict = _namedtuple_as_object and getattr(value, '_asdict', None) if _asdict and callable(_asdict): - chunks = _iterencode_dict(_asdict(), + dct = _asdict() + if not isinstance(dct, dict): + raise TypeError("_asdict() must return a dict, not %s" % (type(dct).__name__,)) + chunks = _iterencode_dict(dct, _current_indent_level) elif _tuple_as_array and isinstance(value, tuple): chunks = _iterencode_list(value, _current_indent_level) @@ -686,8 +692,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, else: _asdict = _namedtuple_as_object and getattr(o, '_asdict', None) if _asdict and callable(_asdict): - for chunk in _iterencode_dict(_asdict(), - _current_indent_level): + dct = _asdict() + if not isinstance(dct, dict): + raise TypeError("_asdict() must return a dict, not %s" % (type(dct).__name__,)) + for chunk in _iterencode_dict(dct, _current_indent_level): yield chunk elif (_tuple_as_array and isinstance(o, tuple)): for chunk in _iterencode_list(o, _current_indent_level): |