Skip to content

Commit 45517ab

Browse files
committed
Address Serhiy's review
1 parent 88a62fe commit 45517ab

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

Lib/test/test_capi/test_long.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,9 @@ def test_long_export(self):
748748
self.assertEqual(pylong_export(IntSubclass(base**10 * 2 + 1)),
749749
(0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
750750

751-
for value in (1.0, 0+1j, "abc"):
752-
with self.subTest(value=value):
753-
with self.assertRaises(TypeError):
754-
pylong_export(value)
751+
self.assertRaises(TypeError, pylong_export, 1.0)
752+
self.assertRaises(TypeError, pylong_export, 0+1j)
753+
self.assertRaises(TypeError, pylong_export, "abc")
755754

756755
def test_longwriter_create(self):
757756
# Test PyLongWriter_Create()

Modules/_testcapi/long.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,35 +144,11 @@ pylong_aspid(PyObject *module, PyObject *arg)
144144
static PyObject *
145145
layout_to_dict(const PyLongLayout *layout)
146146
{
147-
PyObject *dict = PyDict_New();
148-
if (dict == NULL) {
149-
goto error;
150-
}
151-
152-
#define SET_DICT(KEY, EXPR) \
153-
do { \
154-
PyObject *value = (EXPR); \
155-
if (value == NULL) { \
156-
goto error; \
157-
} \
158-
int res = PyDict_SetItemString(dict, KEY, value); \
159-
Py_DECREF(value); \
160-
if (res < 0) { \
161-
goto error; \
162-
} \
163-
} while (0)
164-
165-
SET_DICT("bits_per_digit", PyLong_FromUnsignedLong(layout->bits_per_digit));
166-
SET_DICT("digit_size", PyLong_FromUnsignedLong(layout->digit_size));
167-
SET_DICT("digits_order", PyLong_FromLong(layout->digits_order));
168-
SET_DICT("digit_endianness", PyLong_FromLong(layout->digit_endianness));
169-
#undef SET_DICT
170-
171-
return dict;
172-
173-
error:
174-
Py_XDECREF(dict);
175-
return NULL;
147+
return Py_BuildValue("{sisisisi}",
148+
"bits_per_digit", (int)layout->bits_per_digit,
149+
"digit_size", (int)layout->digit_size,
150+
"digits_order", (int)layout->digits_order,
151+
"digit_endianness", (int)layout->digit_endianness);
176152
}
177153

178154

@@ -185,6 +161,9 @@ pylong_export(PyObject *module, PyObject *obj)
185161
}
186162

187163
if (export_long.digits == NULL) {
164+
assert(export_long.negative == 0);
165+
assert(export_long.ndigits == 0);
166+
assert(export_long.digits == NULL);
188167
return PyLong_FromInt64(export_long.value);
189168
// PyLong_FreeExport() is not needed in this case
190169
}
@@ -209,6 +188,7 @@ pylong_export(PyObject *module, PyObject *obj)
209188
Py_DECREF(item);
210189
}
211190

191+
assert(export_long.value == 0);
212192
PyObject *res = Py_BuildValue("(iN)", export_long.negative, digits);
213193

214194
PyLong_FreeExport(&export_long);
@@ -228,6 +208,7 @@ pylongwriter_create(PyObject *module, PyObject *args)
228208
{
229209
int negative;
230210
PyObject *list;
211+
// TODO(vstinner): write test for negative ndigits and digits==NULL
231212
if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list)) {
232213
return NULL;
233214
}

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6817,7 +6817,7 @@ PyLong_Export(PyObject *obj, PyLongExport *export_long)
68176817
export_long->value = value;
68186818
export_long->negative = 0;
68196819
export_long->ndigits = 0;
6820-
export_long->digits = 0;
6820+
export_long->digits = NULL;
68216821
export_long->_reserved = 0;
68226822
}
68236823
else {

0 commit comments

Comments
 (0)