question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Using types.CodeType in Python 3.11 fails with ValueError: types.CodeType size changed, may indicate binary incompatibility. Expected 168 from C header, got 160 from PyObject

See original GitHub issue

Versions:

Reproducer, myext.pyx:

# cython: language_level=3
cdef extern from "Python.h":
    ctypedef class types.CodeType [object PyCodeObject]:
        pass

Build the extension (I make the assumption that cythonize uses Python 3.11):

cythonize -i myext.pyx

Importing the C extension fails:

$ python3.11 -c 'import myext'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "myext.pyx", line 1, in init myext
    # cython: language_level=3
                               
ValueError: types.CodeType size changed, may indicate binary incompatibility. Expected 168 from C header, got 160 from PyObject

To get Python 3.11 with Cython, you can use for example:

wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0b3.tar.xz
tar -xf Python-3.11.0b3.tar.xz
cd Python-3.11.0b3
./configure --prefix /opt/py311
make
make install
cd ..
/opt/py311/bin/python3.11 -m pip install Cython

Then use /opt/py311/bin/cythonize. You can use a different prefix.


The problem is that tp_basicsize of &PyCode_Type is 160 bytes, whereas and sizeof(PyCodeObject) is 168 bytes.


Issue discovered when on my PR to update gevent to Python 3.11: https://github.com/gevent/gevent/pull/1872#issuecomment-1146149244

@hroncok proposed a Cython pull request in Fedora: https://src.fedoraproject.org/rpms/Cython/pull-request/35

I am working on reworking @hroncok’s PR to propose a PR to the master branch of Cython.

cc @encukou who helped to debug this tedious issue.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:20 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
scodercommented, Jul 31, 2022

This has been resolved by weakening the conditions in https://github.com/cython/cython/pull/4894

0reactions
encukoucommented, Jul 18, 2022

I wonder if sizeof() also adds padding after the last struct field?

Yes, it does 😦 It seems that the upper bound should be rounded up to the nearest alignof(the struct) boundary. (But alignof is C11. For older C… well, people tend to get away with using sizeof(long long) as maximum alignment, or even hardcoding it to 64 – even on platforms where long long has 128-byte alignment, since long long is rare in complex structs.)

Adding an option always means admitting defeat regarding a proper solution. In the end, this is a safety check meant to produce error messages instead of crashes.

It’s good to know there is possibility to admit defeat 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

may indicate binary incompatibility.. PyObject segmentation fault
Expected 896 from C header, got 904 from PyObject Segmentation fault ... builtins.type size changed, may indicate binary incompatibility.
Read more >
Issue 47236: Document types.CodeType.replace() changes ...
CodeType.replace(co_code=new_code) must now pass co_exceptiontable or exception handling will no longer work as expected. Callers are ...
Read more >
RuntimeWarning: numpy.dtype size changed, may indicate ...
When import scipy, error info shows: RuntimeWarning: builtin.type size changed, may indicate binary incompatibility. Expected zd, got zd.
Read more >
The Python/C API
Most Python/C API functions have one or more arguments as well as a return value of type PyObject*. This type is.
Read more >
What's New in Python 3.11 - YouTube
Anthony Shaw – Microsoft's own Senior Open Source Advocate specializing in Python and Cloud solutions gives an exhaustive speech on what's ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found