Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Type of result of contextmanager is not recognized #3158
The following piece of Cython code does not work. I get the error message "Cannot convert Python object to 'int'":
cdef class CustomCType: cdef int x; def __enter__(self) -> 'CustomCType': return self ... with CustomCType() as cobj: c_func_that_requires_intparam(cobj.x)
everything works fine when replacing the "with" statement with
cobj = CustomCType() with cobj: c_func_that_requires_intparam(cobj.x)
The problem seems to be, that the result type of
Oh. I intuitively thought Cython would use the standard type annotation information (not only the cython specific ones). My fault.
After switching to the proposed "cdef" variant (same with "cpdef") cython works ;-)
I looked into the generated code and it is definitly bad typed:
PyObject *__pyx_t_3 = NULL; struct __pyx_obj_5pytcc_Binary *__pyx_t_4 = NULL; ... __pyx_t_3 = ((PyObject *)((struct __pyx_vtabstruct_5pytcc_Binary *)((struct __pyx_obj_5pytcc_Binary *)__pyx_t_1)->__pyx_vtab)->__pyx___enter__(((struct __pyx_obj_5pytcc_Binary *)__pyx_t_1), 0)); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 152, __pyx_L3_error) __Pyx_GOTREF(__pyx_t_3); __pyx_t_4 = __pyx_t_3; // <- WARNING as __pyx_t_3 and __pyx_t_4 have different pointer types!!!
One more thing: