Skip to content

gh-95417: Add PyLong_FromUintptr_t and PyLong_AsUintptr_t #95446

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
``NULL`` on failure.

.. c:function:: PyObject* PyLong_FromUintptr_t(uintptr_t p)

Return a new :c:type:`PyLongObject` object from a C :c:type:`uintptr_t`, or
``NULL`` on failure.


.. c:function:: PyObject* PyLong_FromLongLong(long long v)

Expand Down Expand Up @@ -242,6 +247,18 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: uintptr_t PyLong_AsUintptr_t(PyObject *pylong)

Return a a C :c:type:`uintptr_t` representation of *pylong*. *pylong*
must be an instance of :c:type:`PyLongObject`.

Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
:c:type:`uintptr_t`.

Returns ``(uintptr_t)-1`` on error.
Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)

.. index::
Expand Down
6 changes: 6 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,9 @@ PyLong_AsSize_t:PyObject*:pylong:0:
PyLong_AsSsize_t:Py_ssize_t:::
PyLong_AsSsize_t:PyObject*:pylong:0:

PyLong_AsUintptr_t:uintptr_t:::
PyLong_AsUintptr_t:PyObject*:pylong:0:

PyLong_AsUnsignedLong:unsigned long:::
PyLong_AsUnsignedLong:PyObject*:pylong:0:

Expand Down Expand Up @@ -1221,6 +1224,9 @@ PyLong_FromString:const char*:str::
PyLong_FromString:char**:pend::
PyLong_FromString:int:base::

PyLong_FromUintptr_t:PyObject*::+1:
PyLong_FromUintptr_t:uintptr_t:v::

PyLong_FromUnicodeObject:PyObject*::+1:
PyLong_FromUnicodeObject:PyObject*:u:0:
PyLong_FromUnicodeObject:int:base::
Expand Down
2 changes: 2 additions & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,10 @@ New Features
an additional metaclass argument.
(Contributed by Wenzel Jakob in :gh:`93012`.)

* Added limited C API :c:func:`PyLong_AsUintptr_t` and
:c:func:`PyLong_FromUintptr_t`.
(Contributed by Oleg Iarygin in :gh:`95417`.)

* API for creating objects that can be called using
:ref:`the vectorcall protocol <vectorcall>` was added to the
:ref:`Limited API <stable>`:
Expand Down
2 changes: 2 additions & 0 deletions Include/longobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ PyAPI_FUNC(PyObject *) PyLong_FromLong(long);
PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long);
PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t);
PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t);
PyAPI_FUNC(PyObject *) PyLong_FromUintptr_t(uintptr_t);
PyAPI_FUNC(PyObject *) PyLong_FromDouble(double);
PyAPI_FUNC(long) PyLong_AsLong(PyObject *);
PyAPI_FUNC(long) PyLong_AsLongAndOverflow(PyObject *, int *);
PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *);
PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *);
PyAPI_FUNC(uintptr_t) PyLong_AsUintptr_t(PyObject *);
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ Oleg Höfling
Robert Hölzl
Stefan Hölzl
Catalin Iacob
Oleg Iarygin
Mihai Ibanescu
Ali Ikinci
Aaron Iles
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added :c:func:`PyLong_AsUintptr_t` and :c:func:`PyLong_FromUintptr_t`. Patch
by Oleg Iarygin.
4 changes: 4 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@
added = '3.2'
[function.PyLong_AsSsize_t]
added = '3.2'
[function.PyLong_AsUintptr_t]
added = '3.12'
[function.PyLong_AsUnsignedLong]
added = '3.2'
[function.PyLong_AsUnsignedLongLong]
Expand All @@ -951,6 +953,8 @@
added = '3.2'
[function.PyLong_FromString]
added = '3.2'
[function.PyLong_FromUintptr_t]
added = '3.12'
[function.PyLong_FromUnsignedLong]
added = '3.2'
[function.PyLong_FromUnsignedLongLong]
Expand Down
17 changes: 17 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,15 @@ PyLong_AsSize_t(PyObject *vv)
return x;
}

/* Get a C uintptr_t from an int object. Returns (uintptr_t)-1 and sets
an error condition if overflow occurs. */

uintptr_t
PyLong_AsUintptr_t(PyObject *vv)
{
return (uintptr_t)PyLong_AsVoidPtr(vv);
}

/* Get a C unsigned long int from an int object, ignoring the high bits.
Returns -1 and sets an error condition if an error occurs. */

Expand Down Expand Up @@ -1228,6 +1237,14 @@ PyLong_FromSsize_t(Py_ssize_t ival)
return (PyObject *)v;
}

/* Create a new int object from a C uintptr_t. */

PyObject *
PyLong_FromUintptr_t(uintptr_t ival)
{
return PyLong_FromVoidPtr((void *)ival);
}

/* Get a C long long int from an int object or any object that has an
__index__ method. Return -1 and set an error if overflow occurs. */

Expand Down
2 changes: 2 additions & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.