8000 [3.7] bpo-37170: Fix the cast on error in PyLong_AsUnsignedLongLongMa… · python/cpython@e36ed47 · GitHub
[go: up one dir, main page]

Skip to content

Commit e36ed47

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

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

Doc/c-api/long.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
259259
If the value of *obj* is out of range for an :c:type:`unsigned long`,
260260
return the reduction of that value modulo ``ULONG_MAX + 1``.
261261
262-
Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
262+
Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
263+
disambiguate.
263264
264265
265266
.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
@@ -271,7 +272,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
271272
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
272273
return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.
273274
274-
Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
275+
Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred`
276+
to disambiguate.
275277
276278
277279
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
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
@@ -816,6 +816,26 @@ test_long_as_size_t(PyObject *self)
816816
return Py_None;
817817
}
818818

819+
static PyObject *
820+
test_long_as_unsigned_long_long_mask(PyObject *self,
821+
PyObject *Py_UNUSED(ignored))
822+
{
823+
unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);
824+
825+
if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
826+
return raiseTestError("test_long_as_unsigned_long_long_mask",
827+
"PyLong_AsUnsignedLongLongMask(NULL) didn't "
828+
"complain");
829+
}
830+
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
831+
return raiseTestError("test_long_as_unsigned_long_long_mask",
832+
"PyLong_AsUnsignedLongLongMask(NULL) raised "
833+
"something other than SystemError");
834+
}
835+
PyErr_Clear();
836+
Py_RETURN_NONE;
837+
}
838+
819839
/* Test the PyLong_AsDouble API. At present this just tests that
820840
non-integer arguments are handled correctly.
821841
*/
@@ -4663,6 +4683,8 @@ static PyMethodDef TestMethods[] = {
46634683
METH_NOARGS},
46644684
{"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS},
46654685
{"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS},
4686+
{"test_long_as_unsigned_long_long_mask",
4687+
(PyCFunction)test_long_as_unsigned_long_long_mask, METH_NOARGS},
46664688
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
46674689
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
46684690
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},

Objects/longobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv)
13091309

13101310
if (vv == NULL || !PyLong_Check(vv)) {
13111311
PyErr_BadInternalCall();
1312-
return (unsigned long) -1;
1312+
return (unsigned long long) -1;
13131313
}
13141314
v = (PyLongObject *)vv;
13151315
switch(Py_SIZE(v)) {
@@ -1337,7 +1337,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *op)
13371337

13381338
if (op == NULL) {
13391339
PyErr_BadInternalCall();
1340-
return (unsigned long)-1;
1340+
return (unsigned long long)-1;
13411341
}
13421342

13431343
if (PyLong_Check(op)) {

0 commit comments

Comments
 (0)
0