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
Mutable default arguments in FusedCFuncDefNode().make_fused_cpdef()
/__pyx_fused_cpdef()
cause segfaults for functions later assigned/bound to classes?
#3370
Comments
Could be that you're running into a problem related to this. Note that all the working variants that you're giving above are special because constant tuples and |
cython/Cython/Compiler/ExprNodes.py Line 9436 in 5377210
(I have no real idea for how to fix it though) |
So the issue as I understand it is: When a fused node is bound it doesn't copy across its cython/Cython/Utility/CythonFunction.c Line 1074 in a8cb127
It can't directly copy the cython/Cython/Utility/CythonFunction.c Lines 546 to 555 in a8cb127
This is data that it doesn't own. It can't allocate its own copy of I think the best option would be to also store Note - this is a real bug beyond just being able to use default arguments in
|
Whenever the signature generated for
__pyx_fused_cpdef()
inCython.Compiler.FusedNode.FusedCFuncDefNode().make_fused_cpdef()
contains either a mutable object or an object that contains a mutable object as a default argument, anyfused
cpdef
functions produced by it will segfault once later dynamically bound and called as methods.A
print
function placed as the first non-comment line of the function body doesn't seem to be visibly executed, so I assume the crash occurs somewhere within and as a result of how the descriptor protocol is handled by Cython.Attribute access for the bound function works fine as well as long as it's never called, so I assume the crash occurs somewhere between invoking the descriptor protocol and successfully dispatching the arguments into the function body.
The mutable object does not have to ever be explicitly accessed or modified for the segfault to occur.
This occurs both with classes defined dynamically in Python-space and with
cdef classes
. It doesn't affect methods that are explicitly defined under the class— but it does still happen when the code accessing and calling the instance method is compiled with the instance statically typed.This is probably either a specific case of a somewhat more general issue, or some kind of user error on my part. But I'm not familiar enough with Cython or low-level programming languages in general to tell which it is, and if it is a symptom of a larger issue, I don't know enough about how
cyfunction
s are structured and how they handle the descriptor protocol to be able to narrow it down any further.I also tried emulating how I might imagine
cyfunction
s to work using plaindef
andcdef
functions, as well as acdef class
that implements the descriptor protocol. But those all work fine, so this issue, if it is an actual issue, doesn't seem to have any chance of actually affecting normal usage.The segfault so far seems to require two things to occur:
fused
cpdef
function and then binds it as a method by assigning it as a class attribute.The code that I've modified whenever it's occured is around line
619
ofCompiler/FusedNode.py
:The segfaults seem to occur once any of the arguments have default values that either are or include mutable objects:
The below forms all seem to be fine, IIRC:
Normal functions, as well as explicitly defined methods, all seem to work fine even with the problematic forms. The segfault only seems to occur when a stand-alone function is later bound to a class:
fusedtest.py
fusedtest.pxd
This behaviour is already tested for with
A().meth()
undertests/run/fused_cpdef.pyx
as well.The text was updated successfully, but these errors were encountered: