Discussion:
[Cython] Bug: operator[] does not respect except +
J Robert Ray
2014-08-09 02:04:31 UTC
Permalink
I apologize for not having a working demonstration, but I have found this
bug and verified it is still an issue in Cython 0.20.2. No try/catch block
is generated for operator[]. In contrast, a try/catch block does get
generated for operator().

If it is any use, here is a paraphrasing of the pyx code and the generated
cpp code.

cdef extern from "blah.h" namespace "blah":
cdef cppclass Blah:
int operator[](int) except +

...

cdef class PyBlah:
...
def demo(self, int index):
return deref(self.thisptr)[index]

...

/* "Blah.pyx":107
* def demo(self, int index):
* return deref(self.thisptr)[index] # <<<<<<<<<<<<<<
*/
__Pyx_XDECREF(__pyx_r);
__pyx_t_1 =
__Pyx_PyInt_From_int(((*__pyx_v_self->thisptr)[__pyx_v_index])); if
(unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;


Please let me know if I'm doing anything wrong. Thanks!
J Robert Ray
2014-09-05 23:05:01 UTC
Permalink
I looked into at least adding a unit test to verify "except +" behavior for
all operators and I found that aside from operator(), it seems that all
operators ignore "except +".

I have C++ code that can throw exceptions inside operator[] and sadly this
ends up causing the python interpreter to abort.
Post by J Robert Ray
I apologize for not having a working demonstration, but I have found this
bug and verified it is still an issue in Cython 0.20.2. No try/catch block
is generated for operator[]. In contrast, a try/catch block does get
generated for operator().
If it is any use, here is a paraphrasing of the pyx code and the generated
cpp code.
int operator[](int) except +
...
...
return deref(self.thisptr)[index]
...
/* "Blah.pyx":107
* return deref(self.thisptr)[index] # <<<<<<<<<<<<<<
*/
__Pyx_XDECREF(__pyx_r);
__pyx_t_1 =
__Pyx_PyInt_From_int(((*__pyx_v_self->thisptr)[__pyx_v_index])); if
(unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;
Please let me know if I'm doing anything wrong. Thanks!
Robert Bradshaw
2014-09-05 23:15:54 UTC
Permalink
Yes, this is a bug.
Post by J Robert Ray
I looked into at least adding a unit test to verify "except +" behavior for
all operators and I found that aside from operator(), it seems that all
operators ignore "except +".
I have C++ code that can throw exceptions inside operator[] and sadly this
ends up causing the python interpreter to abort.
Post by J Robert Ray
I apologize for not having a working demonstration, but I have found this
bug and verified it is still an issue in Cython 0.20.2. No try/catch block
is generated for operator[]. In contrast, a try/catch block does get
generated for operator().
If it is any use, here is a paraphrasing of the pyx code and the generated
cpp code.
int operator[](int) except +
...
...
return deref(self.thisptr)[index]
...
/* "Blah.pyx":107
* return deref(self.thisptr)[index] # <<<<<<<<<<<<<<
*/
__Pyx_XDECREF(__pyx_r);
__pyx_t_1 =
__Pyx_PyInt_From_int(((*__pyx_v_self->thisptr)[__pyx_v_index])); if
(unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;
Please let me know if I'm doing anything wrong. Thanks!
_______________________________________________
cython-devel mailing list
https://mail.python.org/mailman/listinfo/cython-devel
Loading...