Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 055a3fb

Browse files
committed
Internal refactoring in struct.pack: make all integer conversions go through get_pylong.
1 parent b9f751a commit 055a3fb

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

Modules/_struct.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ typedef struct { char c; _Bool x; } s_bool;
8989
#pragma options align=reset
9090
#endif
9191

92-
/* Helper to get a PyLongObject. Caller should decref. */
92+
/* Helper for integer format codes: converts an arbitrary Python object to a
93+
PyLongObject if possible, otherwise fails. Caller should decref. */
9394

9495
static PyObject *
9596
get_pylong(PyObject *v)
@@ -113,13 +114,13 @@ get_long(PyObject *v, long *p)
113114
{
114115
long x;
115116

116-
if (!PyLong_Check(v)) {
117-
PyErr_SetString(StructError,
118-
"required argument is not an integer");
117+
v = get_pylong(v);
118+
if (v == NULL)
119119
return -1;
120-
}
120+
assert(PyLong_Check(v));
121121
x = PyLong_AsLong(v);
122-
if (x == -1 && PyErr_Occurred()) {
122+
Py_DECREF(v);
123+
if (x == (long)-1 && PyErr_Occurred()) {
123124
if (PyErr_ExceptionMatches(PyExc_OverflowError))
124125
PyErr_SetString(StructError,
125126
"argument out of range");
@@ -137,11 +138,10 @@ get_ulong(PyObject *v, unsigned long *p)
137138
{
138139
unsigned long x;
139140

140-
if (!PyLong_Check(v)) {
141-
PyErr_SetString(StructError,
142-
"required argument is not an integer");
141+
v = get_pylong(v);
142+
if (v == NULL)
143143
return -1;
144-
}
144+
assert(PyLong_Check(v));
145145
x = PyLong_AsUnsignedLong(v);
146146
if (x == (unsigned long)-1 && PyErr_Occurred()) {
147147
if (PyErr_ExceptionMatches(PyExc_OverflowError))
@@ -161,13 +161,13 @@ static int
161161
get_longlong(PyObject *v, PY_LONG_LONG *p)
162162
{
163163
PY_LONG_LONG x;
164-
if (!PyLong_Check(v)) {
165-
PyErr_SetString(StructError,
166-
"required argument is not an integer");
164+
165+
v = get_pylong(v);
166+
if (v == NULL)
167167
return -1;
168-
}
168+
assert(PyLong_Check(v));
169169
x = PyLong_AsLongLong(v);
170-
if (x == -1 && PyErr_Occurred()) {
170+
if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
171171
if (PyErr_ExceptionMatches(PyExc_OverflowError))
172172
PyErr_SetString(StructError,
173173
"argument out of range");
@@ -183,13 +183,13 @@ static int
183183
get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
184184
{
185185
unsigned PY_LONG_LONG x;
186-
if (!PyLong_Check(v)) {
187-
PyErr_SetString(StructError,
188-
"required argument is not an integer");
186+
187+
v = get_pylong(v);
188+
if (v == NULL)
189189
return -1;
190-
}
190+
assert(PyLong_Check(v));
191191
x = PyLong_AsUnsignedLongLong(v);
192-
if (x == -1 && PyErr_Occurred()) {
192+
if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
193193
if (PyErr_ExceptionMatches(PyExc_OverflowError))
194194
PyErr_SetString(StructError,
195195
"argument out of range");

0 commit comments

Comments
 (0)