Skip to content

Conversation

@jpe
Copy link
Contributor

@jpe jpe commented Jun 15, 2025

This makes the none flags for cpdef f(a: T) equivalent to those for cpdef(T a) which fixes possible cross module call segfaults.

Added run/cross_mod_nonecheck to check that something tests for None before accessing an attribue on a C pointer -- doesn't segfault because it's returning part of the memory for the None instance.

Note that not all tests succeed. If there is interest in merging this, I can fixup the other tests and / or limit the scope of this.

This makes the none flags for cpdef f(a: T) equivalent to those
for cpdef(T a) which fixes possible cross module call segfaults.

Added run/cross_mod_nonecheck to check that something tests for
None before accessing an attribue on a C pointer -- doesn't
segfault because it's returning part of the memory for the None
instance.

Note that not all tests succeed. If there is interest in merging
this, I can fixup the other tests and / or limit the scope of this.
Comment on lines +1040 to +1045
# Set both .not_none and .or_none to False so that cpdef f(a: T)
# and cpdef f(a: T | None) are equivalent to cpdef f(T a) --
# cpdef f(T not None a) and cpdef f(T or None a) are currently
# not allowed
self.not_none = False
self.or_none = False
Copy link
Contributor

Choose a reason for hiding this comment

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

Doing this unconditionally seems wrong, give how much effort we invest into correctly setting these two flags just above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is necessary if the function is called from another module because the only form acceptable in a .pxd file is def f(T a): which means that both not_none and or_none will be false when generating code for another module. This can and does lead to segfaults with cross module calls. I had another pull request to allow type hints in .pxd files but that wasn't going anywhere so I created this (and am using it internally) so that I don't get segfaults.

I now think that allowing type hints in .pxd files is the preferred approach so that whatever call convention is used with a type hinted function within a module is also used for cross module calls.

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