Skip to content

Commit b724d3e

Browse files
committed
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.
1 parent e657049 commit b724d3e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

golang/_golang_str.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,12 @@ if PY2:
11651165

11661166
# ---- adjust bstr/ustr classes after what cython generated ----
11671167

1168+
# for pybstr/pyustr cython generates .tp_dealloc that refer to bytes/unicode types directly.
1169+
# override that to refer to zbytes/zunicode to avoid infinite recursion on free
1170+
# when builtin bytes and unicode are replaced with bstr/ustr.
1171+
(<PyTypeObject*>pybstr).tp_dealloc = (<PyTypeObject*>zbytes) .tp_dealloc
1172+
(<PyTypeObject*>pyustr).tp_dealloc = (<PyTypeObject*>zunicode) .tp_dealloc
1173+
11681174
# remove unsupported bstr/ustr methods. do it outside of `cdef class` to
11691175
# workaround https://github.com/cython/cython/issues/4556 (`if ...` during
11701176
# `cdef class` is silently handled wrongly)

0 commit comments

Comments
 (0)