<div dir="ltr">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.<div><br></div><div>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.</div><div>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.</div><div><br></div><div>[who uses id() anyways?]</div><div><br></div><div>-gps</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jan 17, 2019 at 2:26 AM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Disclaimer: I'm not a ctypes expert, so I might have this completely <br>
wrong. If so, I apologise for the noise.<br>
<br>
The id() function is documented as returning an abstract ID number. In <br>
CPython, that happens to have been implemented as the address of the <br>
object.<br>
<br>
I understand that the only way to pass the address of an object to <br>
ctypes is to use that id. Is that intentional?<br>
<br>
As I see it, there is a conflict between two facts:<br>
<br>
- that id() returns a memory address is an implementation detail; as <br>
such users should not rely on it, as the implementation could (in <br>
principle) change without notice;<br>
<br>
- but users using ctypes have no choice but to rely on id() returning <br>
the object memory address, as of it were an offical part of the API.<br>
<br>
Implementations like PyPy which emulate ctypes, while objects don't have <br>
fixed memory locations, will surely have a problem here. I don't know <br>
how PyPy solves this.<br>
<br>
Have I misunderstood something here?<br>
<br>
<br>
<br>
-- <br>
Steve<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/greg%40krypto.org" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/greg%40krypto.org</a><br>
</blockquote></div>