Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-102471, PEP 757: Add PyLong import and export API#121339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
f4fdbf2c2e568ef0d9525b19764f6f7fd11080e0791a7902fb70a6dd07552a70d0f942762c33a20be7a3d92bf1eb3b02a2caca2d74221a4937b1d49d70a1214aa25f690973d45d3e224c7d7cb2a3d601ac04926806b196b3e8d29686c68c2a8fd669a04f9d0b2be94aca98ad1167d75e5e53a5bc24789f0422f9da529a483db44f31d2863ed663511816798d033bd6536b87d494d852ea72ff8353d584b577598ab08cd55eaebef303248c70a26f9788a62fe45517ab92007d16d3cb80File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -615,21 +615,6 @@ Export API | ||||||||||||||||
| .. versionadded:: 3.14 | ||||||||||||||||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||||||||||
| .. c:struct:: PyLongLayout | ||||||||||||||||
| Layout of an array of digits, used by Python :class:`int` object. | ||||||||||||||||
| @@ -669,11 +654,23 @@ Export API | ||||||||||||||||
| See the :c:struct:`PyLongLayout` structure. | ||||||||||||||||
| .. c:struct:: PyLongExport | ||||||||||||||||
| Export of a Python :class:`int` object. | ||||||||||||||||
| There are two cases: | ||||||||||||||||
| * If :c:member:`digits` is ``NULL``, only use the :c:member:`value` member. | ||||||||||||||||
| Calling :c:func:`PyLong_FreeExport` is optional in this case. | ||||||||||||||||
| * If :c:member:`digits` is not ``NULL``, use :c:member:`negative`, | ||||||||||||||||
| :c:member:`ndigits` and :c:member:`digits` members. | ||||||||||||||||
| Calling :c:func:`PyLong_FreeExport` is mandatory in this case. | ||||||||||||||||
| .. c:member:: int64_t value | ||||||||||||||||
| Integer value if :c:member:`digits` is ``NULL``. | ||||||||||||||||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||||||||||
| .. c:member:: uint8_t negative | ||||||||||||||||
| 1 if the number is negative, 0 otherwise. | ||||||||||||||||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||||||||||
| @@ -683,26 +680,26 @@ Export API | ||||||||||||||||
| .. c:member:: const void *digits | ||||||||||||||||
| Read-only array of unsigned digits. Can be ``NULL``. | ||||||||||||||||
| .. c:function:: int PyLong_Export(PyObject *obj,PyLongExport *export_long) | ||||||||||||||||
| Export a Python :class:`int` object. | ||||||||||||||||
| On success, set *\*export_long* and return 0. | ||||||||||||||||
| On error, set an exception and return -1. | ||||||||||||||||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||||||||||
| This function always succeeds if *obj* is a Python :class:`int` object or a | ||||||||||||||||
| subclass. | ||||||||||||||||
| If *export_long.digits* is not ``NULL,:c:func:`PyLong_FreeExport` must be | ||||||||||||||||
| called when the export is no longer needed. | ||||||||||||||||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
Lets see if we can restore this in a that way. It might be helpful for e.g. Sage, which doesn't support PyPy. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I would prefer to not add this note. It was controversial during PEP 757 design. @serhiy-storchaka@encukou: What do you think? Would you be ok to declare that the PyLong_Export() function cannot fail if the argument is a Python int? Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
It was proposed unconditionally, not as CPython's implementation detail. Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm fine with it, as an implementation detail. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. | ||||||||||||||||
| .. c:function:: void PyLong_FreeExport(PyLongExport *export_long) | ||||||||||||||||
| Release the export *export_long* created by :c:func:`PyLong_Export`. | ||||||||||||||||
| PyLongWriter API | ||||||||||||||||
| @@ -748,21 +745,3 @@ The :c:type:`PyLongWriter` API can be used to import an integer. | ||||||||||||||||
| .. c:function:: void PyLongWriter_Discard(PyLongWriter *writer) | ||||||||||||||||
| Discard the internal object and destroy the writer instance. | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -186,21 +186,21 @@ pylong_export(PyObject *module, PyObject *obj) | ||
| // PyLong_FreeExport() is not needed in this case | ||
| } | ||
| assert(PyLong_GetNativeLayout()->digit_size == sizeof(digit)); | ||
| constdigit *export_long_digits = export_long.digits; | ||
| PyObject *digits = PyList_New(0); | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| for (Py_ssize_t i=0; i < export_long.ndigits; i++) { | ||
| PyObject *item = PyLong_FromUnsignedLong(export_long_digits[i]); | ||
| if (item == NULL) { | ||
| goto error; | ||
| } | ||
| if (PyList_Append(digits,item) < 0) { | ||
| Py_DECREF(item); | ||
| goto error; | ||
| } | ||
| Py_DECREF(item); | ||
| } | ||
| PyObject *res = Py_BuildValue("(iN)", export_long.negative, digits); | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| @@ -230,7 +230,7 @@ pylongwriter_create(PyObject *module, PyObject *args) | ||
| } | ||
| Py_ssize_t ndigits = PyList_GET_SIZE(list); | ||
| digit *digits = PyMem_Malloc(ndigits * sizeof(digit)); | ||
skirpichev marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (digits == NULL) { | ||
| PyErr_NoMemory(); | ||
| return NULL; | ||
| @@ -239,16 +239,16 @@ pylongwriter_create(PyObject *module, PyObject *args) | ||
| for (Py_ssize_t i=0; i < ndigits; i++) { | ||
| PyObject *item = PyList_GET_ITEM(list, i); | ||
| longnum = PyLong_AsLong(item); | ||
| if (num == -1 && PyErr_Occurred()) { | ||
| goto error; | ||
| } | ||
| if (num < 0 ||num >= PyLong_BASE) { | ||
| PyErr_SetString(PyExc_ValueError, "digit doesn't fit intodigit"); | ||
| goto error; | ||
| } | ||
| digits[i] = (digit)num; | ||
| } | ||
| void *writer_digits; | ||
| @@ -257,8 +257,8 @@ pylongwriter_create(PyObject *module, PyObject *args) | ||
| if (writer == NULL) { | ||
| goto error; | ||
| } | ||
| assert(PyLong_GetNativeLayout()->digit_size == sizeof(digit)); | ||
| memcpy(writer_digits, digits, ndigits * sizeof(digit)); | ||
skirpichev marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyObject *res = PyLongWriter_Finish(writer); | ||
| PyMem_Free(digits); | ||