Bad code generated for `except +*` function
See original GitHub issueHello up there. Please consider the following example:
---- 8< ---- (y.pyx
)
# cython: language_level=2
# distutils: language=c++
cdef extern from *:
"""
#include <exception>
void ggg() {
throw std::bad_alloc();
}
"""
void ggg()
cdef void fff() except *:
raise RuntimeError('aaa')
cdef void hhh() except +*:
fff()
ggg()
def aaa():
hhh()
Explanation:
ggg
throws C++ exception and is not marked withexcept +
– thus functions that callsggg
may throw C++ exception themselves;fff
raises Python exception but returns void. It thus is marked withexcept *
for its caller to check whether Python exception occurred.hhh
calls bothfff
andggg
. It thus may raise either Python exception (fromfff
) or C++ exception (fromggg
).hhh
is thus marked asexcept +*
.
When trying to cythonize y.pyx
, it gives:
/home/kirr/tmp/trashme/pyx/y.cpp: In function ‘void __pyx_f_1y_hhh()’:
/home/kirr/tmp/trashme/pyx/y.cpp:1189:3: error: ‘__pyx_r’ was not declared in this scope
__pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
^~~~~~~
/home/kirr/tmp/trashme/pyx/y.cpp:1189:3: note: suggested alternative: ‘__pyx_f’
__pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
^~~~~~~
__pyx_f
/home/kirr/tmp/trashme/pyx/y.cpp:1189:13: error: expected primary-expression before ‘<’ token
__pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
^
/home/kirr/tmp/trashme/pyx/y.cpp:1189:14: error: ‘Cython’ was not declared in this scope
__pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
^~~~~~
/home/kirr/tmp/trashme/pyx/y.cpp: In function ‘PyObject* __pyx_pf_1y_aaa(PyObject*)’:
/home/kirr/tmp/trashme/pyx/y.cpp:1229:5: error: ‘__Pyx_CppExn2PyErr’ was not declared in this scope
__Pyx_CppExn2PyErr();
^~~~~~~~~~~~~~~~~~
With the following code generated for hhh
:
static void __pyx_f_1y_hhh(void) {
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("hhh", 0);
/* "y.pyx":17
*
* cdef void hhh() except +*:
* fff() # <<<<<<<<<<<<<<
* ggg()
*
*/
__pyx_f_1y_fff(); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 17, __pyx_L1_error)
/* "y.pyx":18
* cdef void hhh() except +*:
* fff()
* ggg() # <<<<<<<<<<<<<<
*
* def aaa():
*/
ggg();
/* "y.pyx":16
* raise RuntimeError('aaa')
*
* cdef void hhh() except +*: # <<<<<<<<<<<<<<
* fff()
* ggg()
*/
/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__Pyx_AddTraceback("y.hhh", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
}
Notice the __pyx_r = <Cython.Compiler.ExprNodes.CharNode object at 0x7f8e76e20810>;
line.
Cython version: 0.29.13-514-gac1c9fe47
(today’s master).
Thanks beforehand, Kirill
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Why is "except: pass" a bad programming practice?
If you use the generic try/catch, it indicates either that you do not understand the possible run-time errors in your program, or that...
Read more >Exceptions and debugging - Advanced R. - Hadley Wickham
This chapter will teach you how to fix unanticipated problems (debugging), show you how functions can communicate problems and how you can take...
Read more >30. Errors and Exception Handling | Python Tutorial
Exception Handling. An exception is an error that happens during the execution of a program. Exceptions are known to non-programmers as ...
Read more >8. Errors and Exceptions — Python 3.11.1 documentation
If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution...
Read more >How to Throw Exceptions in Python - Rollbar
If there's an exception, the code in the corresponding “except” block will run, and then the code in the “finally” block will run....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
bba69054b4aff2c0a63a8be9d6ce5c9fdd079a52
Thanks, @da-woods.