Re: PyCodeObject incompatibility

Matti Picus <[email protected]> Tue, 15 Feb 2022 08:23:01 +0200
Newsgroups gmane.comp.python.pypy
Message-ID <[email protected]>
On 15/2/22 05:15, Dmitry Kovner wrote:

> Hi, again! First of all, thanks for the fast reply! The answer helped 
> me a lot. However, during the porting of the extension to the awesome 
> PyPy, I've got more questions:
> 1. The extension uses opcodes from cPython's opcode.h header file. 
> (https://github.com/python/cpython/blob/8a84aef0123bd8c13cf81fbc3b5f6d45f96c2656/Include/opcode.h) 
> There is no such file in the implementation of C API in PyPy. What 
> could you recommend as the best alternative?
> 2. The extension uses some fields of PyThreadState structure. For 
> example, its frame field 
> (https://github.com/python/cpython/blob/8a84aef0123bd8c13cf81fbc3b5f6d45f96c2656/Include/cpython/pystate.h#L59). 
> Is it right to use PyObject_GetAttrString() to access that field in 
> PyPy C API?
> 3. There is no function PyFrame_FastToLocals 
> (https://github.com/python/cpython/blob/8a84aef0123bd8c13cf81fbc3b5f6d45f96c2656/Objects/frameobject.c#L931) 
> in the C API of PyPy. Are there any alternatives?
> 4. Functions PyEval_SetTrace(), PyEval_SetProfile(), 
> PyFrame_GetLineNumber() are defined only in stubs.py in PyPy. 
> (https://foss.heptapod.net/pypy/pypy/-/blob/branch/py3.7/pypy/module/cpyext/stubs.py#L918) 
> Is there a way to get that information in PyPy except patching of its 
> source code?
> 5. Are there any analogues of all PyTrace_* constants 
> (https://github.com/python/cpython/blob/8a84aef0123bd8c13cf81fbc3b5f6d45f96c2656/Include/cpython/pystate.h#L26) 
> in the PyPy C API?
> 6. There is no function PyFrame_Check() 
> (https://github.com/python/cpython/blob/8a84aef0123bd8c13cf81fbc3b5f6d45f96c2656/Include/frameobject.h#L53) 
> in the PyPy C API. Are there any alternatives?
>
> I'm really sorry for the long question! I hope the list of the 
> problems described above is full enough to port the extension and I 
> will have no more questions. :)
>
> Best regards, Dmitrii
>

Even if PyPy could expose the interfaces you desire (and it would be a 
lot of work to do that), you would lose out on speed since the C-API on 
PyPy is much much slower than pure python. If you really want to port 
your extension, you might want to rethink what it is doing and refactor 
it to do so in a PyPy-friendly way: pure python + CFFI where needed to 
interface to libraries that expose C interfaces. The C-API is a layer we 
bolted on top of PyPy to support popular libraries like Cython and 
NumPy, it is not meant for projects that probe CPython internals.


Matti