mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Simplifies ord()'s logic at the cost of some code duplication, removing a " `ord' might be used uninitialized in this function" warning
This commit is contained in:
parent
c867f74a10
commit
34c20cf705
1 changed files with 7 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
|
fo
|
||||||
/* Built-in functions */
|
/* Built-in functions */
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
@ -1512,20 +1512,22 @@ builtin_ord(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
if (PyString_Check(obj)) {
|
if (PyString_Check(obj)) {
|
||||||
size = PyString_GET_SIZE(obj);
|
size = PyString_GET_SIZE(obj);
|
||||||
if (size == 1)
|
if (size == 1) {
|
||||||
ord = (long)((unsigned char)*PyString_AS_STRING(obj));
|
ord = (long)((unsigned char)*PyString_AS_STRING(obj));
|
||||||
|
return PyInt_FromLong(ord);
|
||||||
|
}
|
||||||
} else if (PyUnicode_Check(obj)) {
|
} else if (PyUnicode_Check(obj)) {
|
||||||
size = PyUnicode_GET_SIZE(obj);
|
size = PyUnicode_GET_SIZE(obj);
|
||||||
if (size == 1)
|
if (size == 1) {
|
||||||
ord = (long)*PyUnicode_AS_UNICODE(obj);
|
ord = (long)*PyUnicode_AS_UNICODE(obj);
|
||||||
|
return PyInt_FromLong(ord);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"ord() expected string or Unicode character, " \
|
"ord() expected string or Unicode character, " \
|
||||||
"%.200s found", obj->ob_type->tp_name);
|
"%.200s found", obj->ob_type->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (size == 1)
|
|
||||||
return PyInt_FromLong(ord);
|
|
||||||
|
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"ord() expected a character, length-%d string found",
|
"ord() expected a character, length-%d string found",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue