Skip to content

Commit b33a0e3

Browse files
committed
Change PyUnstable_Long_Export() argument to PyObject*
1 parent ea6b55a commit b33a0e3

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

Doc/c-api/long.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ Import/Export API
630630
Array of unsigned digits.
631631
632632
633-
.. c:function:: int PyUnstable_Long_Export(PyLongObject *obj, PyUnstable_LongExport *export)
633+
.. c:function:: int PyUnstable_Long_Export(PyObject *obj, PyUnstable_LongExport *export)
634634
635635
Export a Python :class:`int` object as an array of digits.
636636

Include/cpython/longintrepr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ typedef struct PyUnstable_LongExport {
176176
} PyUnstable_LongExport;
177177

178178
PyAPI_FUNC(int) PyUnstable_Long_Export(
179-
PyLongObject *obj,
179+
PyObject *obj,
180180
PyUnstable_LongExport *long_export);
181181
PyAPI_FUNC(void) PyUnstable_Long_ReleaseExport(
182182
PyUnstable_LongExport *long_export);

Lib/test/test_capi/test_long.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,13 @@ def test_long_export(self):
768768
self.assertEqual(pylong_export(shift**2 * 3 + shift * 2 + 1),
769769
(0, [1, 2, 3]))
770770

771+
with self.assertRaises(TypeError):
772+
pylong_export(1.0)
773+
with self.assertRaises(TypeError):
774+
pylong_export(0+1j)
775+
with self.assertRaises(TypeError):
776+
pylong_export("abc")
777+
771778
def test_long_import(self):
772779
# Test PyLong_Import()
773780
layout = _testcapi.get_pylong_layout()

Modules/_testcapi/long.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,8 @@ pylong_import(PyObject *module, PyObject *args)
163163
static PyObject *
164164
pylong_export(PyObject *module, PyObject *obj)
165165
{
166-
if (!PyLong_Check(obj)) {
167-
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
168-
return NULL;
169-
}
170-
171166
PyUnstable_LongExport long_export;
172-
if (PyUnstable_Long_Export((PyLongObject*)obj, &long_export) < 0) {
167+
if (PyUnstable_Long_Export(obj, &long_export) < 0) {
173168
return NULL;
174169
}
175170

Objects/longobject.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6702,17 +6702,21 @@ PyUnstable_Long_Import(int negative, size_t ndigits, Py_digit *digits)
67026702

67036703

67046704
int
6705-
PyUnstable_Long_Export(PyLongObject *obj, PyUnstable_LongExport *long_export)
6705+
PyUnstable_Long_Export(PyObject *obj, PyUnstable_LongExport *long_export)
67066706
{
6707-
assert(PyLong_Check(obj));
6707+
if (!PyLong_Check(obj)) {
6708+
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
6709+
return -1;
6710+
}
6711+
PyLongObject *self = (PyLongObject*)obj;
67086712

67096713
long_export->obj = (PyLongObject*)Py_NewRef(obj);
6710-
long_export->negative = _PyLong_IsNegative(obj);
6711-
long_export->ndigits = _PyLong_DigitCount(obj);
6714+
long_export->negative = _PyLong_IsNegative(self);
6715+
long_export->ndigits = _PyLong_DigitCount(self);
67126716
if (long_export->ndigits == 0) {
67136717
long_export->ndigits = 1;
67146718
}
6715-
long_export->digits = obj->long_value.ob_digit;
6719+
long_export->digits = self->long_value.ob_digit;
67166720
return 0;
67176721
}
67186722

0 commit comments

Comments
 (0)