KEMBAR78
Get 0.29.x working on Python 3.11 by da-woods · Pull Request #4465 · cython/cython · GitHub
Skip to content

Conversation

@da-woods
Copy link
Contributor

This is a cherry pick of 9d1ffd5 and 3f56484 for Cython 0.29.x. It is not a hugely clean cherry-pick because __Pyx_PyCode_New has a different number of arguments in the two versions.

It also disables CYTHON_FAST_PYCALL on Python 3.11 (which I imagine is a performance regression). This is because the frame object no longer has a f_localsplus field to use. On Cython 3 this is fine - we use CYTHON_VECTORCALL instead, however that doesn't exist on 0.29.x. Therefore this is the least intrusive change available I think.

I've tested it to the extent of building Cython with these changes with Python versions 2.7, 3.7, 3.8, 3.9, 3.10, and 3.11 (twice, so it builds, then runs the compiled cython to rebuild itself) but haven't run anything more of the test-suite.

* Add a basic replacement for PyCode_New().

An optimized versions would be nice, but this is intended to work sufficiently to start testing. Also, CPython 3.11 might actually add a new C-API function to simplify setting the current code position. That might be used instead.

* Disable introspection of frame object with vectorcall

This feature looked to only be used for early Python versions that don't have the full vectorcall protocol (and the contents of the frame object are changed in Python 3.11).
…ythonGH-4427)

Use the accessor function instead of the direct struct member (which is now the wrong type).

Closes cython#4416
@da-woods
Copy link
Contributor Author

It also disables CYTHON_FAST_PYCALL on Python 3.11 (which I imagine is a performance regression). This is because the frame object no longer has a f_localsplus field to use. On Cython 3 this is fine - we use CYTHON_VECTORCALL instead, however that doesn't exist on 0.29.x. Therefore this is the least intrusive change available I think.

As an aside - this might be a good reason to target a release of Cython 3.0 sooner rather than later - it sort of feels like Cython the features needed to support modern versions of Python are in the Cython 3alpha only and it's beginning to diverge too much to easily maintain both.

@da-woods
Copy link
Contributor Author

It also disables CYTHON_FAST_PYCALL on Python 3.11 (which I imagine is a performance regression). This is because the frame object no longer has a f_localsplus field to use. On Cython 3 this is fine - we use CYTHON_VECTORCALL instead, however that doesn't exist on 0.29.x. Therefore this is the least intrusive change available I think.

Finally comment for now: It's probably possible to dedefine __Pyx_PyFrame_GetLocalsplus to go frame->interpreter_frame->localsplus and get the working again. But that's becoming a more significant change.

@scoder
Copy link
Contributor

scoder commented Nov 14, 2021

It also disables CYTHON_FAST_PYCALL on Python 3.11 (which I imagine is a performance regression).

That's fine. Users who really want Py3.11 support (and all of the other goodies) can switch to Cython 3.0 now. Everyone who chooses not to do that can certainly live with a reduced feature set, since that's specifically what they choose.

Comment on lines 719 to 720
// FIXME: unclear what to do if PyThreadState_GetFrame fails
f->f_back = PyThreadState_GetFrame(tstate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs say that it returns NULL is there is no current frame. That seems reasonable and not a failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the comment then. That should probably be updated in master too

@scoder
Copy link
Contributor

scoder commented Nov 18, 2021

Closing and reopening to get a CI run.

@scoder scoder closed this Nov 18, 2021
@scoder scoder reopened this Nov 18, 2021
@scoder scoder closed this Nov 18, 2021
@scoder scoder reopened this Nov 18, 2021
@scoder scoder closed this Nov 18, 2021
@scoder scoder reopened this Nov 18, 2021
@scoder
Copy link
Contributor

scoder commented Nov 18, 2021

Not complete, but good enough for a merge. Thanks!

@scoder scoder merged commit e138a84 into cython:0.29.x Nov 18, 2021
@da-woods da-woods deleted the 029x-311 branch November 18, 2021 17:43
@vstinner
Copy link
Contributor

vstinner commented Dec 2, 2021

Thanks for this cool fix!

I found this PR while trying to write a fix for include "longintrepr.h" while building lxml on Python 3.11. I wrote a fix and then I noticed that it's already fixed by this PR :-)

Would it be possible to have a Cython 0.29.x release including this fix?

It would help me to test my other changes to port projects to Python 3.11, like python/mypy#11652 : running the mypy test suite requires to install lxml which uses Cython ;-)

@scoder
Copy link
Contributor

scoder commented Dec 3, 2021

Yeah, I was planning for new releases a few times but ran into various issues each time I tried. 0.29 finally has CI support again, so it's much safer now to try a release again. I'll give it another try soon.

@vstinner
Copy link
Contributor

vstinner commented Dec 3, 2021

Oh obvisouly, please tell me if I can help you ;-)

@scoder
Copy link
Contributor

scoder commented Dec 6, 2021

FYI, 0.29.25 is out.

@vstinner
Copy link
Contributor

vstinner commented Dec 6, 2021

Yeah, that's great! Thanks a lot @scoder! It's great to have Cython compatible with alpha versions of Python, it helps me a lot for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants