question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bad code generated for `except +*` function

See original GitHub issue

Hello 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 with except + – thus functions that calls ggg may throw C++ exception themselves;
  • fff raises Python exception but returns void. It thus is marked with except * for its caller to check whether Python exception occurred.
  • hhh calls both fff and ggg. It thus may raise either Python exception (from fff) or C++ exception (from ggg). hhh is thus marked as except +*.

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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Jun 17, 2021

bba69054b4aff2c0a63a8be9d6ce5c9fdd079a52

0reactions
navytuxcommented, Jun 23, 2021

Thanks, @da-woods.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found