Skip to content

SIGSEGV or SIGILL from exception handler within exception handler #3666

@cuteufo

Description

@cuteufo

Hi Cython team, I encountered SIGSEGV or Illegal instruction errors upon return None from exception handler within another exception handler.

Here is the reproductive sample codes in three py files.

# sub.py ===
from datetime import datetime, date

def func():
    text = "2020-10-23"   # date format intentionally unmatched with arg in strptime()
    try:
        s = datetime.strptime(text, "%d%b%Y")
        return s
    except ValueError as err:
        try:
            print("Error1", err)
            s = datetime.strptime(text, "%d%b%Y")
            return s
        except ValueError as err:
            print("Error2", err)
            return None   # <------ this is where the code breaks

# main.py ===
import sub
print("main:", sub.func())

# setup.py ===
from distutils.core import setup
from Cython.Build import cythonize
setup(name="sub.app", ext_modules=cythonize("sub.py", language_level = "3"))

before I use python setup.py build_ext --inplace, I use python main.py and got:

Error1 time data '2020-10-23' does not match format '%d%b%Y'
Error2 time data '2020-10-23' does not match format '%d%b%Y'
main: None       # <---- 'None' is returned as expected

but, after Cython, I use python main.py again and got:

Error1 time data '2020-10-23' does not match format '%d%b%Y'
Error2 time data '2020-10-23' does not match format '%d%b%Y'
Illegal instruction: 4   # <--- code is broken before print() in main.py is executed

Here is what I did to overcome this:

# sub.py ===
from datetime import datetime, date

def func():
    s = None  # <---- add this line first
    text = "2020-10-23" 
    try:
        s = datetime.strptime(text, "%d%b%Y")
        return s
    except ValueError as err:
        try:
            print("Error1", err)
            s = datetime.strptime(text, "%d%b%Y")
            return s
        except ValueError as err:
            print("Error2", err)
            # return None   # <--- do not return None from the exception handler
    return s   # instead, return from main body of the function

In my project codes (sorry I cannot copy here), the program is also broken, except SIGSERV received at the same location. In the code, func() is actually a @classmethod function of a class.

Version: Python 3.7.7 (anaconda), Cython 0.29.15.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions