[issue37997] Segfault when using pickle with exceptions and dynamic class inheritance

Gabriel C report at bugs.python.org
Mon Sep 9 15:01:56 EDT 2019


Gabriel C <gabrielc at mit.edu> added the comment:

Some further investigation suggests this may have nothing to do with pickle at all.

Consider the following short example:
```
def CreateDynamicClass(basetype):
    class DynamicClassImpl(basetype):
        def __init__(self):
            super(DynamicClassImpl, self).__init__()
    
    return DynamicClassImpl()

# Comment out any of the following three lines and the test passes
dynamic_memerror = CreateDynamicClass(MemoryError)
memory_error = MemoryError("Test")
dynamic_memerror = CreateDynamicClass(MemoryError)

print(MemoryError("Test2"))
```

This reliably produces the following stack trace:
```
Traceback (most recent call last):
  File "test_min2.py", line 13, in <module>
    print(MemoryError("Test2"))
TypeError: __init__() takes 1 positional argument but 2 were given
```
In addition it causes a bus error (signal 10) around 30% of the time when run on python 3.7.4 (macOS high sierra) and a segmentation fault around 10% of the time when run on python 3.5 (Ubuntu 16.04).

Now modify the short example as follows, inserting a dummy argument into the constructor of the dynamic exception type:
```
def CreateDynamicClass(basetype):
    class DynamicClassImpl(basetype):
        def __init__(self, dummy_arg):
            if dummy_arg is not None:
                raise AssertionError("Dynamic exception constructor called.")
            super(DynamicClassImpl, self).__init__()
    
    return DynamicClassImpl(None)

# Comment out any of the following three lines and the test passes
dynamic_memerror = CreateDynamicClass(MemoryError)
memory_error = MemoryError("Test")
dynamic_memerror = CreateDynamicClass(MemoryError)

print(MemoryError("Test2"))
```

This produces the following stack trace:
```
Traceback (most recent call last):
  File "test_min2.py", line 15, in <module>
    print(MemoryError("Test2"))
  File "test_min2.py", line 5, in __init__
    raise AssertionError("Dynamic exception constructor called.")
AssertionError: Dynamic exception constructor called.
```
showing that the user-defined type constructor is actually called when the MemoryError object is created.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37997>
_______________________________________


More information about the Python-bugs-list mailing list