Upgrade Cython to version 3

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 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.
This commit is contained in:
Lasse Blaauwbroek
2023-10-13 11:33:28 +02:00
committed by Jacob Alexander
parent 59a639fa97
commit 983719fde9
9 changed files with 84 additions and 103 deletions

View File

@@ -28,7 +28,7 @@ cdef class _StringArrayPtr:
cdef StringPtr * thisptr
cdef object parent
cdef size_t size
cdef ArrayPtr[StringPtr] asArrayPtr(self) except +reraise_kj_exception
cdef ArrayPtr[StringPtr] asArrayPtr(self)
cdef class SchemaLoader:
cdef C_SchemaLoader * thisptr
@@ -38,7 +38,7 @@ cdef class SchemaParser:
cdef public dict modules_by_id
cdef list _all_imports
cdef _StringArrayPtr _last_import_array
cpdef _parse_disk_file(self, displayName, diskPath, imports) except +reraise_kj_exception
cpdef _parse_disk_file(self, displayName, diskPath, imports)
cdef class _DynamicOrphan:
cdef C_DynamicOrphan thisptr
@@ -79,10 +79,10 @@ cdef class _DynamicStructBuilder:
cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot=?, bint tryRegistry=?)
cdef _check_write(self)
cpdef to_bytes(_DynamicStructBuilder self) except +reraise_kj_exception
cpdef to_segments(_DynamicStructBuilder self) except +reraise_kj_exception
cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count) except +reraise_kj_exception
cpdef to_bytes_packed(_DynamicStructBuilder self) except +reraise_kj_exception
cpdef to_bytes(_DynamicStructBuilder self)
cpdef to_segments(_DynamicStructBuilder self)
cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count)
cpdef to_bytes_packed(_DynamicStructBuilder self)
cpdef _get(self, field)
cpdef _set(self, field, value)
@@ -128,7 +128,7 @@ cdef class _DynamicEnum:
cdef public object _parent
cdef _init(self, capnp.DynamicEnum other, object parent)
cpdef _as_str(self) except +reraise_kj_exception
cpdef _as_str(self)
cdef class _DynamicListBuilder:
cdef C_DynamicList.Builder thisptr
@@ -146,11 +146,11 @@ cdef class _DynamicListBuilder:
cdef class _MessageBuilder:
cdef schema_cpp.MessageBuilder * thisptr
cpdef init_root(self, schema)
cpdef get_root(self, schema) except +reraise_kj_exception
cpdef get_root_as_any(self) except +reraise_kj_exception
cpdef set_root(self, value) except +reraise_kj_exception
cpdef get_segments_for_output(self) except +reraise_kj_exception
cpdef new_orphan(self, schema) except +reraise_kj_exception
cpdef get_root(self, schema)
cpdef get_root_as_any(self)
cpdef set_root(self, value)
cpdef get_segments_for_output(self)
cpdef new_orphan(self, schema)
cdef to_python_reader(C_DynamicValue.Reader self, object parent)
cdef to_python_builder(C_DynamicValue.Builder self, object parent)