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.

Title: Should we re-export `PyObj_FromPtr` in `ctypes`?
Type: behavior Stage:
Components: ctypes Versions: Python 3.11
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 in

it seems that we should address this comment:

# This section should be moved into ctypes\, 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: and 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)
    >>> obj2 = ctypes.cast(id(obj), ctypes.py_object).value
    >>> obj2 is obj
    >>> sys.getrefcount(obj)
    >>> del obj2
    >>> sys.getrefcount(obj)
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