diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2000-04-12 21:19:47 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2000-04-12 21:19:47 (GMT) |
commit | 394b54d01a42fe235b3e6ec55f0ce38fe1a03f50 (patch) | |
tree | 4be4441d1c8224bf3bf9c05dbf33e5930474d24a | |
parent | 502d2b46158d371688b67ad53c75a3042649680e (diff) | |
download | cpython-394b54d01a42fe235b3e6ec55f0ce38fe1a03f50.zip cpython-394b54d01a42fe235b3e6ec55f0ce38fe1a03f50.tar.gz cpython-394b54d01a42fe235b3e6ec55f0ce38fe1a03f50.tar.bz2 |
ord: provide better error messages
-rw-r--r-- | Python/bltinmodule.c | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index cd3db23..84d7aeb 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1642,21 +1642,32 @@ builtin_ord(self, args) { PyObject *obj; long ord; + int size; if (!PyArg_ParseTuple(args, "O:ord", &obj)) return NULL; - if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1) - ord = (long)((unsigned char)*PyString_AS_STRING(obj)); - else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1) - ord = (long)*PyUnicode_AS_UNICODE(obj); - else { - PyErr_SetString(PyExc_TypeError, - "expected a string or unicode character"); + if (PyString_Check(obj)) { + size = PyString_GET_SIZE(obj); + if (size == 1) + ord = (long)((unsigned char)*PyString_AS_STRING(obj)); + } else if (PyUnicode_Check(obj)) { + size = PyUnicode_GET_SIZE(obj); + if (size == 1) + ord = (long)*PyUnicode_AS_UNICODE(obj); + } else { + PyErr_Format(PyExc_TypeError, + "expected string or unicode character, " \ + "%.200s found", obj->ob_type->tp_name); return NULL; } + if (size == 1) + return PyInt_FromLong(ord); - return PyInt_FromLong(ord); + PyErr_Format(PyExc_TypeError, + "expected a character, length-%d string found", + size); + return NULL; } static char ord_doc[] = |