Skip to content

Upgrade Cython to version 3 #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

LasseBlaauwbroek
Copy link
Contributor

@LasseBlaauwbroek LasseBlaauwbroek commented Oct 13, 2023

While looking at #333, I hypothesized that upgrading Cython might solve the issue. It didn't. But upgrading should still happen at some point. This is my work in progress on that. The tests pass, but there are two main things missing:

Problem (1):
Starting with Cython 3, you can only do except+ or except +reraise_kj_exception on extern functions coming from C++. (This makes sense, and the way things were declared in Pycapnp wasn't too good.) As a result, I had to remove a lot of these declarations. This results in some segmentation faults, because Cython no longer detects C++ exceptions and converts them to Python exceptions in some places.

To solve this, all extern declarations in .pxd files have to be examined and except +reraise_kj_exception clauses need to be added to anything that might throw. Previously, this was done really inconsistently. The lazy solution would be to just add the clause everywhere, but I'm not sure what the performance implications are.

Problem (2):
The compilation output of python setup.py build_ext --inplace is now full of messages like these:

capnp/lib/capnp.cpp: In function ‘PyObject* __pyx_f_5capnp_3lib_5capnp_18_DynamicListReader__get(__pyx_obj_5capnp_3lib_5capnp__DynamicListReader*, int64_t, int)’:
capnp/lib/capnp.cpp:4871:51: warning: moving a temporary object prevents copy elision [-Wpessimizing-move]
 4871 |   #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x)
      |                                          ~~~~~~~~~^~~
capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’
20944 |   __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error)
      |                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
capnp/lib/capnp.cpp:4871:51: note: remove ‘std::move’ call
 4871 |   #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x)
      |                                          ~~~~~~~~~^~~
capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’
20944 |   __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error)
      |                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~

There are to many move calls inserted. I'm not sure if this is a Cython issue, or if we are somehow annotating things wrong. Might be worth asking the Cython people.

I'm not planning on working on this further in the short term. If someone wants to take over on this, feel free.

@LasseBlaauwbroek LasseBlaauwbroek marked this pull request as draft October 13, 2023 09:45
While looking at capnproto#333, I hypothesized that upgrading Cython might solve the
issue. It didn't. But upgrading should still happen at some point. This is my
work in progress on that. The tests pass, but there are two main things missing:

Problem (1):
Starting with Cython 3, you can only do `except+` or `except
+reraise_kj_exception` on `extern` functions coming from C++. (This makes sense,
and the way things were declared in Pycapnp wasn't too good.) As a result, I had
to remove a lot of these declaration. This results in some segmentation faults,
because Cython no longer detects C++ exceptions and converts them to Python
exceptions in some places.

To solve this, all `extern` declarations in `.pxd` files have to be examined and
`except +reraise_kj_exception` clauses need to be added to anything that might
throw. Previously, this was done really inconsistently. The lazy solution would
be to just add the clause everywhere, but I'm not sure what the performance
implications are.

Problem (2):
The compilation output of `python setup.py build_ext --inplace` is now full of messages like these:
```
capnp/lib/capnp.cpp: In function ‘PyObject* __pyx_f_5capnp_3lib_5capnp_18_DynamicListReader__get(__pyx_obj_5capnp_3lib_5capnp__DynamicListReader*, int64_t, int)’:
capnp/lib/capnp.cpp:4871:51: warning: moving a temporary object prevents copy elision [-Wpessimizing-move]
 4871 |   #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x)
      |                                          ~~~~~~~~~^~~
capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’
20944 |   __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error)
      |                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
capnp/lib/capnp.cpp:4871:51: note: remove ‘std::move’ call
 4871 |   #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x)
      |                                          ~~~~~~~~~^~~
capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’
20944 |   __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error)
      |                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
```
There are to many `move` calls inserted. I'm not sure if this is a Cython issue,
or if we are somehow annotating things wrong. Might be worth asking the Cython
people.

I'm not planning on working on this further in the short term. If someone wants
to take over on this, feel free.
@LasseBlaauwbroek
Copy link
Contributor Author

Reviving this, because the old Cython version no longer works on Python 3.13.
I amended this PR to resolve the outstanding issues to build with 3.13. The build seems to pass with all versions between 3.8 and 3.13 now.

One issue still stands: With the new version of Cython, we may now have to annotate more extern functions with except +reraise_kj_exception. In principle, we have to annotate any function that might throw a C++ exception. It is a bit difficult to know which ones those are. I checked, and if we miss an exception then the Python code will panic with a reasonable error message. As such, I propose merging this for now and dealing with any extra annotations as we find them.

@haata Any objections to merging and creating a new release compatible with Python 3.13?

@LasseBlaauwbroek LasseBlaauwbroek marked this pull request as ready for review May 12, 2025 20:02
@haata
Copy link
Collaborator

haata commented May 12, 2025

No objections.

(Sorry everyone, I haven't had a lot of free time lately. It might be better if someone else took over maintainership, or at least has permissions to push new releases)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants