summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-11-10 22:24:46 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2017-11-10 22:24:46 +0200
commit6df3ec19a81029ef035266a96505f814d8d6ccac (patch)
treec4933e54fc7f81b476c2ee904ebf20f050bb93ee
parent138a2ff3f6db37468f3c7509f69c984792fd49a0 (diff)
downloadsimplejson-6df3ec19a81029ef035266a96505f814d8d6ccac.tar.gz
Make TypeError messages contain type name instead of a repr.
The failure depends on the type, not on the value of an object. This combines CPython's issues 26623 and 24641.
-rw-r--r--simplejson/__init__.py3
-rw-r--r--simplejson/_speedups.c4
-rw-r--r--simplejson/encoder.py6
3 files changed, 9 insertions, 4 deletions
diff --git a/simplejson/__init__.py b/simplejson/__init__.py
index 2f14262..8ed9baa 100644
--- a/simplejson/__init__.py
+++ b/simplejson/__init__.py
@@ -77,7 +77,8 @@ Specializing JSON object encoding::
>>> def encode_complex(obj):
... if isinstance(obj, complex):
... return [obj.real, obj.imag]
- ... raise TypeError(repr(o) + " is not JSON serializable")
+ ... raise TypeError('Object of type %s is not JSON serializable' %
+ ... obj.__class__.__name__)
...
>>> json.dumps(2 + 1j, default=encode_complex)
'[2.0, 1.0]'
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
index bfd053a..3d9e702 100644
--- a/simplejson/_speedups.c
+++ b/simplejson/_speedups.c
@@ -680,7 +680,9 @@ encoder_stringify_key(PyEncoderObject *s, PyObject *key)
Py_INCREF(Py_None);
return Py_None;
}
- PyErr_SetString(PyExc_TypeError, "keys must be a string");
+ PyErr_Format(PyExc_TypeError,
+ "keys must be str, int, float, bool or None, "
+ "not %.100s", key->ob_type->tp_name);
return NULL;
}
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index c87468a..25ddf25 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -258,7 +258,8 @@ class JSONEncoder(object):
return JSONEncoder.default(self, o)
"""
- raise TypeError(repr(o) + " is not JSON serializable")
+ raise TypeError('Object of type %s is not JSON serializable' %
+ o.__class__.__name__)
def encode(self, o):
"""Return a JSON string representation of a Python data structure.
@@ -541,7 +542,8 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
elif _skipkeys:
key = None
else:
- raise TypeError("key " + repr(key) + " is not a string")
+ raise TypeError('keys must be str, int, float, bool or None, '
+ 'not %s' % key.__class__.__name__)
return key
def _iterencode_dict(dct, _current_indent_level):