diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-04-03 15:26:31 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-04-03 15:26:31 (GMT) |
commit | 055a3fbe3e6b7606e12d257352a92fd8a55f4392 (patch) | |
tree | 33b52c1a4eb3eec45f5d8c823c6314171df548fc /Modules/_struct.c | |
parent | b9f751ad9140719d3c6f84284143e0e891027df8 (diff) | |
download | cpython-055a3fbe3e6b7606e12d257352a92fd8a55f4392.zip cpython-055a3fbe3e6b7606e12d257352a92fd8a55f4392.tar.gz cpython-055a3fbe3e6b7606e12d257352a92fd8a55f4392.tar.bz2 |
Internal refactoring in struct.pack: make all integer conversions go through get_pylong.
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index 7f6e305..43321a4 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -89,7 +89,8 @@ typedef struct { char c; _Bool x; } s_bool; #pragma options align=reset #endif -/* Helper to get a PyLongObject. Caller should decref. */ +/* Helper for integer format codes: converts an arbitrary Python object to a + PyLongObject if possible, otherwise fails. Caller should decref. */ static PyObject * get_pylong(PyObject *v) @@ -113,13 +114,13 @@ get_long(PyObject *v, long *p) { long x; - if (!PyLong_Check(v)) { - PyErr_SetString(StructError, - "required argument is not an integer"); + v = get_pylong(v); + if (v == NULL) return -1; - } + assert(PyLong_Check(v)); x = PyLong_AsLong(v); - if (x == -1 && PyErr_Occurred()) { + Py_DECREF(v); + if (x == (long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_SetString(StructError, "argument out of range"); @@ -137,11 +138,10 @@ get_ulong(PyObject *v, unsigned long *p) { unsigned long x; - if (!PyLong_Check(v)) { - PyErr_SetString(StructError, - "required argument is not an integer"); + v = get_pylong(v); + if (v == NULL) return -1; - } + assert(PyLong_Check(v)); x = PyLong_AsUnsignedLong(v); if (x == (unsigned long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) @@ -161,13 +161,13 @@ static int get_longlong(PyObject *v, PY_LONG_LONG *p) { PY_LONG_LONG x; - if (!PyLong_Check(v)) { - PyErr_SetString(StructError, - "required argument is not an integer"); + + v = get_pylong(v); + if (v == NULL) return -1; - } + assert(PyLong_Check(v)); x = PyLong_AsLongLong(v); - if (x == -1 && PyErr_Occurred()) { + if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_SetString(StructError, "argument out of range"); @@ -183,13 +183,13 @@ static int get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) { unsigned PY_LONG_LONG x; - if (!PyLong_Check(v)) { - PyErr_SetString(StructError, - "required argument is not an integer"); + + v = get_pylong(v); + if (v == NULL) return -1; - } + assert(PyLong_Check(v)); x = PyLong_AsUnsignedLongLong(v); - if (x == -1 && PyErr_Occurred()) { + if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_SetString(StructError, "argument out of range"); |