Skip to content

Commit 2bfc2dc

Browse files
ZackerySpytzvstinner
authored andcommitted
[2.7] bpo-37170: Fix the cast on error in PyLong_AsUnsignedLongLongMask() (GH-13860) (GH-13898)
(cherry picked from commit dc24765) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
1 parent 1b57ab5 commit 2bfc2dc

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

Doc/c-api/long.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ Long Integer Objects
217217
Return a C :c:type:`unsigned long` from a Python long integer, without checking
218218
for overflow.
219219
220+
Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
221+
disambiguate.
222+
220223
.. versionadded:: 2.3
221224
222225
@@ -225,6 +228,9 @@ Long Integer Objects
225228
Return a C :c:type:`unsigned long long` from a Python long integer, without
226229
checking for overflow.
227230
231+
Returns ``(unsigned PY_LONG_LONG)-1`` on error. Use
232+
:c:func:`PyErr_Occurred` to disambiguate.
233+
228234
.. versionadded:: 2.3
229235
230236
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the cast on error in :c:func:`PyLong_AsUnsignedLongLongMask()`.

Modules/_testcapimodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,26 @@ test_long_long_and_overflow(PyObject *self)
888888
return Py_None;
889889
}
890890

891+
static PyObject *
892+
test_long_as_unsigned_long_long_mask(PyObject *self)
893+
{
894+
unsigned PY_LONG_LONG res = PyLong_AsUnsignedLongLongMask(NULL);
895+
896+
if (res != (unsigned PY_LONG_LONG)-1 || !PyErr_Occurred()) {
897+
return raiseTestError("test_long_as_unsigned_long_long_mask",
898+
"PyLong_AsUnsignedLongLongMask(NULL) didn't "
899+
"complain");
900+
}
901+
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
902+
return raiseTestError("test_long_as_unsigned_long_long_mask",
903+
"PyLong_AsUnsignedLongLongMask(NULL) raised "
904+
"something other than SystemError");
905+
}
906+
PyErr_Clear();
907+
Py_INCREF(Py_None);
908+
return Py_None;
909+
}
910+
891911
/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
892912
for both long and int arguments. The test may leak a little memory if
893913
it fails.
@@ -2715,6 +2735,8 @@ static PyMethodDef TestMethods[] = {
27152735
{"test_longlong_api", test_longlong_api, METH_NOARGS},
27162736
{"test_long_long_and_overflow",
27172737
(PyCFunction)test_long_long_and_overflow, METH_NOARGS},
2738+
{"test_long_as_unsigned_long_long_mask",
2739+
(PyCFunction)test_long_as_unsigned_long_long_mask, METH_NOARGS},
27182740
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
27192741
#endif
27202742
{"getargs_f", getargs_f, METH_VARARGS},

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv)
10221022

10231023
if (vv == NULL || !PyLong_Check(vv)) {
10241024
PyErr_BadInternalCall();
1025-
return (unsigned long) -1;
1025+
return (unsigned PY_LONG_LONG) -1;
10261026
}
10271027
v = (PyLongObject *)vv;
10281028
i = Py_SIZE(v);

0 commit comments

Comments
 (0)