Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64()#120390
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 our terms of service and privacy 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 from all commits
3e503417c7b976c239d181186043c2150c1050f415830d25042b99020199a8071748a7dd2194e7070514File 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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Add new functions to convert C ``<stdint.h>`` numbers from/to Python | ||
| :class:`int`: | ||
| * :c:func:`PyLong_FromInt32` | ||
| * :c:func:`PyLong_FromUInt32` | ||
| * :c:func:`PyLong_FromInt64` | ||
| * :c:func:`PyLong_FromUInt64` | ||
| * :c:func:`PyLong_AsInt32` | ||
| * :c:func:`PyLong_AsUInt32` | ||
| * :c:func:`PyLong_AsInt64` | ||
| * :c:func:`PyLong_AsUInt64` | ||
| Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #include "pyconfig.h" // Py_GIL_DISABLED | ||
| #ifndef Py_GIL_DISABLED | ||
| // Need limited C API 3.13 to test PyLong_AsInt() | ||
| # define Py_LIMITED_API 0x030d0000 | ||
| // Need limited C API 3.14 to test PyLong_AsInt64() | ||
vstinner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # define Py_LIMITED_API 0x030e0000 | ||
| #endif | ||
| #include "parts.h" | ||
| @@ -758,6 +758,52 @@ pylong_aspid(PyObject *module, PyObject *arg) | ||
| } | ||
| static PyObject * | ||
| pylong_asint32(PyObject *module, PyObject *arg) | ||
| { | ||
| NULLABLE(arg); | ||
| int32_t value; | ||
| if (PyLong_AsInt32(arg, &value) < 0) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromInt32(value); | ||
| } | ||
| static PyObject * | ||
| pylong_asuint32(PyObject *module, PyObject *arg) | ||
| { | ||
| NULLABLE(arg); | ||
| uint32_t value; | ||
| if (PyLong_AsUInt32(arg, &value) < 0) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromUInt32(value); | ||
| } | ||
| static PyObject * | ||
| pylong_asint64(PyObject *module, PyObject *arg) | ||
| { | ||
| NULLABLE(arg); | ||
| int64_t value; | ||
| if (PyLong_AsInt64(arg, &value) < 0) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromInt64(value); | ||
| } | ||
| static PyObject * | ||
| pylong_asuint64(PyObject *module, PyObject *arg) | ||
| { | ||
| NULLABLE(arg); | ||
| uint64_t value; | ||
| if (PyLong_AsUInt64(arg, &value) < 0) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromUInt64(value); | ||
| } | ||
| static PyMethodDef test_methods[] = { | ||
| _TESTLIMITEDCAPI_TEST_LONG_AND_OVERFLOW_METHODDEF | ||
| _TESTLIMITEDCAPI_TEST_LONG_API_METHODDEF | ||
| @@ -785,6 +831,10 @@ static PyMethodDef test_methods[] = { | ||
| {"pylong_asdouble", pylong_asdouble, METH_O}, | ||
| {"pylong_asvoidptr", pylong_asvoidptr, METH_O}, | ||
| {"pylong_aspid", pylong_aspid, METH_O}, | ||
| {"pylong_asint32", pylong_asint32, METH_O}, | ||
| {"pylong_asuint32", pylong_asuint32, METH_O}, | ||
| {"pylong_asint64", pylong_asint64, METH_O}, | ||
| {"pylong_asuint64", pylong_asuint64, METH_O}, | ||
| {NULL}, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed. You can define them in the Python code as
INT32_MAX = 2**31 - 1etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to reuse <limits.h> C constants to avoid any typo.