8000 bpo-37170: Fix the cast on error in PyLong_AsUnsignedLongLongMask() (… · python/cpython@dc24765 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc24765

Browse files
ZackerySpytzvstinner
authored andcommitted
bpo-37170: Fix the cast on error in PyLong_AsUnsignedLongLongMask() (GH-13860)
1 parent f6713e8 commit dc24765

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
@@ -288,7 +288,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
288288
If the value of *obj* is out of range for an :c:type:`unsigned long`,
289289
return the reduction of that value modulo ``ULONG_MAX + 1``.
290290
291-
Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
291+
Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
292+
disambiguate.
292293
293294
.. versionchanged:: 3.8
294295
Use :meth:`__index__` if available.
@@ -307,7 +308,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
307308
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
308309
return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.
309310
310-
Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
311+
Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred`
312+
to disambiguate.
311313
312314
.. versionchanged:: 3.8
313315
Use :meth:`__index__` if available.
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
@@ -825,6 +825,26 @@ test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored))
825825
return Py_None;
826826
}
827827

828+
static PyObject *
829+
test_long_as_unsigned_long_long_mask(PyObject *self,
830+
PyObject *Py_UNUSED(ignored))
831+
{
832+
unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);
833+
834+
if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
835+
return rai 10000 seTestError("test_long_as_unsigned_long_long_mask",
836+
"PyLong_AsUnsignedLongLongMask(NULL) didn't "
837+
"complain");
838+
}
839+
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
840+
return raiseTestError("test_long_as_unsigned_long_long_mask",
841+
"PyLong_AsUnsignedLongLongMask(NULL) raised "
842+
"something other than SystemError");
843+
}
844+
PyErr_Clear();
845+
Py_RETURN_NONE;
846+
}
847+
828848
/* Test the PyLong_AsDouble API. At present this just tests that
829849
non-integer arguments are handled correctly.
830850
*/
@@ -5070,6 +5090,8 @@ static PyMethodDef TestMethods[] = {
50705090
{"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
50715091
{"test_long_as_double", test_long_as_double, METH_NOARGS},
50725092
{"test_long_as_size_t", test_long_as_size_t, METH_NOARGS},
5093+
{"test_long_as_unsigned_long_long_mask",
5094+
test_long_as_unsigned_long_long_mask, METH_NOARGS},
50735095
{"test_long_numbits", test_long_numbits, METH_NOARGS},
50745096
{"test_k_code", test_k_code, METH_NOARGS},
50755097
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},

Objects/longobject.c

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

13771377
if (vv == NULL || !PyLong_Check(vv)) {
13781378
PyErr_BadInternalCall();
1379-
return (unsigned long) -1;
1379+
return (unsigned long long) -1;
13801380
}
13811381
v = (PyLongObject *)vv;
13821382
switch(Py_SIZE(v)) {
@@ -1404,7 +1404,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *op)
14041404

14051405
if (op == NULL) {
14061406
PyErr_BadInternalCall();
1407-
return (unsigned long)-1;
1407+
return (unsigned long long)-1;
14081408
}
14091409

14101410
if (PyLong_Check(op)) {

0 commit comments

Comments
 (0)
0