[Python-Dev] ctypes: is it intentional that id() is the only way to get the address of an object?

Walter Dörwald walter at livinglogic.de
Fri Jan 18 06:46:45 EST 2019


On 18 Jan 2019, at 11:57, Antoine Pitrou wrote:

> On Fri, 18 Jan 2019 03:00:54 +0000
> MRAB <python at mrabarnett.plus.com> wrote:
>> On 2019-01-18 00:48, Gregory P. Smith wrote:
>>> I've heard that libraries using ctypes, cffi, or cython code of 
>>> various
>>> sorts in the real world wild today does abuse the unfortunate side
>>> effect of CPython's implementation of id(). I don't have specific
>>> instances of this in mind but trust what I've heard: that it is 
>>> happening.
>>>
>>> id() should never be considered to be the PyObject*.  In as much as 
>>> code
>>> shouldn't assume it is running on top of a specific CPython 
>>> implementation.
>>> If there is a _need_ to get a pointer to a C struct handle 
>>> referencing a
>>> CPython C API PyObject, we should make an explicit API for that 
>>> rather
>>> than the id() hack.  That way code can be explicit about its need, 
>>> and
>>> code that is just doing a funky form of identity tracking without 
>>> using
>>> is and is not can continue using id() without triggering regressive
>>> behavior on VMs that don't have a CPython compatible PyObject under 
>>> the
>>> hood by default.
>>>
>>> [who uses id() anyways?]
>>>
>> I use it in some of my code.
>>
>> If I want to cache some objects, I put them in a dict, using the id 
>> as
>> the key. If I wanted to locate an object in a cache and didn't have
>> id(), I'd have to do a linear search for it.
>
> Indeed.  I've used it for the same purpose in the past 
> (identity-dict).

Its useful in all situations where you do topology preserving 
transformations, for example pickling (i.e. object serialization) or a 
deep copy of some object structures.

In these cases you need a way to record and quickly detect whether 
you've handled a specific object before. In Python we can do that with a 
dictionary that has object ids as keys. Java provides IdentityHashMap 
for that. Javascript provides neither, so deep-copying objects in 
Javascript seems to be impossible.

> Regards
>
> Antoine.

Servus,
    Walter


More information about the Python-Dev mailing list