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

Steven D'Aprano steve at pearwood.info
Fri Jan 18 04:49:26 EST 2019


On Thu, Jan 17, 2019 at 11:37:13AM +0100, Antoine Pitrou wrote:

I said:

> > The id() function is documented as returning an abstract ID number. In 
> > CPython, that happens to have been implemented as the address of the 
> > object.
> > 
> > I understand that the only way to pass the address of an object to 
> > ctypes is to use that id. Is that intentional?


Antoine:
 
> Can you explain in detail what you're doing?

Code-wise, I'm not doing anything with ctypes.

Language-wise, I'm trying to get a definitive answer of whether or not 
id() returning the address of the object should be a guaranteed feature 
or not.

Across the entire Python ecosystem, no it isn't, as Jython and 
IronPython return consecutive integers. But should we consider it an 
intentional part of the CPython API?

There are developers who insist that when it comes to CPython, id() 
returning the object address is an intentional feature that they can and 
do rely on, because (so I was told by one of them) that using id() is 
the only way to get the address of an object from pure-Python.

According to this claim, using id() to get the address for use in ctypes 
is the correct and only way to do it, and this is a deliberate design 
choice by the core devs rather than an accident of the implementation. 
So long as you know you are using CPython, this is (so I was told) 
completely safe.

In the grand scheme of things this may be a pretty minor issue. But I 
suspect that it could be a pain point for implementations like PyPy that 
support both objects that move and a ctypes emulation.



-- 
Steve


More information about the Python-Dev mailing list