Commit b724d3ee authored by Kirill Smelkov's avatar Kirill Smelkov

golang_str: Fix pybstr/pyustr .tp_dealloc wrt upcoming str=bstr and unicode=ustr

For pybstr/pyustr cython generates .tp_dealloc that refer to
bytes/unicode types directly. That works ok in normal circumstances, but
will lead to crash when gpython will start patching builtin str and
unicode types with bstr and ustr:

    (py39.venv) kirr@deca:~/src/tools/go/pygolang-master$ gpython
    Ошибка сегментирования (образ памяти сброшен на диск)

    (py39.venv) kirr@deca:~/src/tools/go/pygolang-master$ gdb python core
    ...
    Core was generated by `/home/kirr/src/tools/go/py39.venv/bin/python3.9 /home/kirr/src/tools/go/py39.ve'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x00007f2edb247d5c in PyType_HasFeature (type=<error reading variable: Cannot access memory at address 0x7ffc6ca1bff8>,
        feature=<error reading variable: Cannot access memory at address 0x7ffc6ca1bff0>)
        at /home/kirr/local/py3.9/include/python3.9/object.h:622
    622     {
    (gdb) bt
    #0  0x00007f2edb247d5c in PyType_HasFeature (type=<error reading variable: Cannot access memory at address 0x7ffc6ca1bff8>,
        feature=<error reading variable: Cannot access memory at address 0x7ffc6ca1bff0>)
        at /home/kirr/local/py3.9/include/python3.9/object.h:622
    #1  0x00007f2edb2f4b28 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88982
    #2  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #3  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #4  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #5  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #6  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #7  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #8  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #9  0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    #10 0x00007f2edb2f4bc8 in __pyx_tp_dealloc_6golang_7_golang__pyustr (o=0x7f2edae99030) at golang/_golang.cpp:88986
    ...

-> Fix that crash by manually repointing .tp_dealloc of bstr/ustr to
.tp_dealloc of original bytes and unicode.
parent e6570490
......@@ -1165,6 +1165,12 @@ if PY2:
# ---- adjust bstr/ustr classes after what cython generated ----
# for pybstr/pyustr cython generates .tp_dealloc that refer to bytes/unicode types directly.
# override that to refer to zbytes/zunicode to avoid infinite recursion on free
# when builtin bytes and unicode are replaced with bstr/ustr.
(<PyTypeObject*>pybstr).tp_dealloc = (<PyTypeObject*>zbytes) .tp_dealloc
(<PyTypeObject*>pyustr).tp_dealloc = (<PyTypeObject*>zunicode) .tp_dealloc
# remove unsupported bstr/ustr methods. do it outside of `cdef class` to
# workaround https://github.com/cython/cython/issues/4556 (`if ...` during
# `cdef class` is silently handled wrongly)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment