This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Should we re-export `PyObj_FromPtr` in `ctypes`?
Type: behavior Stage:
Components: ctypes Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, eryksun, meador.inge, sobolevn, zach.ware
Priority: normal Keywords:

Created on 2022-01-30 15:03 by sobolevn, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg412155 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-30 15:03
After looking at https://github.com/python/cpython/blame/8fb36494501aad5b0c1d34311c9743c60bb9926c/Lib/ctypes/test/test_python_api.py#L5-L10 in https://bugs.python.org/issue46584

it seems that we should address this comment:

```
# This section should be moved into ctypes\__init__.py, when it's ready.

from _ctypes import PyObj_FromPtr
```

by either:
1. Making `PyObj_FromPtr` public by re-exporting it from `ctypes`
2. Removing this comment

Arguments for `(1)`:
- It is quite widely used and there are a lot of articles mentioning it: https://www.programcreek.com/python/example/77012/_ctypes.PyObj_FromPtr and https://programtalk.com/python-examples/_ctypes.PyObj_FromPtr/ Basically, Google is filled with results.
- It looks useful for educational purposes.

I don't think that I am familiar with `ctypes`'s history well enough to make an educated dicision here.

But, I would love to work on this after this decision is made :)
msg412165 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-30 17:14
Alternatively, one can cast the address to py_object and dereference its `value`. For example:

    >>> obj = bytearray(b'spam')
    >>> sys.getrefcount(obj)
    2
    >>> obj2 = ctypes.cast(id(obj), ctypes.py_object).value
    >>> obj2 is obj
    True
    >>> sys.getrefcount(obj)
    3
    >>> del obj2
    >>> sys.getrefcount(obj)
    2
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90743
2022-01-30 17:14:33eryksunsetnosy: + eryksun
messages: + msg412165
2022-01-30 15:03:49sobolevncreate