You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Hello up there. I've hit a situation where call to bytes.__contains__(self) is unconditionally translated to PySequence_Contains(self) even if type of self inherits from bytes and provides its own .__contains__. This incorrectly leads to RecursionError in my case. Please find details below.
To Reproduce
Code to reproduce the behaviour:
# cython: language_level=3classMyBytes(bytes):
def__contains__(self, key):
print('__contains__')
return bytes.__contains__(self, key)
x = MyBytes(b'zzz')
b'a'in x
Running it through pure python (both py2 and py3) works as expected:
$ python containsbug.pyx__contains__
However running it through Cython leads to RecursionError:
$ cythonize -i containsbug.pyx && python -c 'import containsbug'Compiling /home/kirr/src/tools/go/pygolang/containsbug.pyx because it changed.[1/1] Cythonizing /home/kirr/src/tools/go/pygolang/containsbug.pyxrunning build_extbuilding 'containsbug' extensioncreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/homecreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirrcreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/srccreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/src/toolscreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/src/tools/gocreating /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/src/tools/go/pygolangx86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -ffile-prefix-map=/build/python3.9-RNBry6/python3.9-3.9.2=. -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -ffile-prefix-map=/build/python3.9-RNBry6/python3.9-3.9.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/kirr/src/wendelin/venv/py3.venv/include -I/usr/include/python3.9 -c /home/kirr/src/tools/go/pygolang/containsbug.c -o /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/src/tools/go/pygolang/containsbug.ox86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-z,relro -g -fwrapv -O2 -g -ffile-prefix-map=/build/python3.9-RNBry6/python3.9-3.9.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 /home/kirr/src/tools/go/pygolang/tmpgzlwnd7x/home/kirr/src/tools/go/pygolang/containsbug.o -o /home/kirr/src/tools/go/pygolang/containsbug.cpython-39-x86_64-linux-gnu.so__contains____contains____contains____contains__...__contains____contains____contains__Traceback (most recent call last): File "<string>", line 1, in <module> File "containsbug.pyx", line 9, in init containsbug b'a' in x File "containsbug.pyx", line 6, in containsbug.MyBytes.__contains__ return bytes.__contains__(self, key) File "containsbug.pyx", line 6, in containsbug.MyBytes.__contains__ return bytes.__contains__(self, key) File "containsbug.pyx", line 6, in containsbug.MyBytes.__contains__ return bytes.__contains__(self, key) [Previous line repeated 985 more times] File "containsbug.pyx", line 5, in containsbug.MyBytes.__contains__ print('__contains__')RecursionError: maximum recursion depth exceeded while calling a Python object__contains__
Expected behavior
The same output as when run through pure python.
Environment (please complete the following information):
Describe the bug
Hello up there. I've hit a situation where call to
bytes.__contains__(self)
is unconditionally translated toPySequence_Contains(self)
even if type of self inherits from bytes and provides its own.__contains__
. This incorrectly leads toRecursionError
in my case. Please find details below.To Reproduce
Code to reproduce the behaviour:
Running it through pure python (both py2 and py3) works as expected:
However running it through Cython leads to RecursionError:
Expected behavior
The same output as when run through pure python.
Environment (please complete the following information):
0.29.28-26-gac29e8976
(today's 0.29.x)Additional context
The code for
bytes.__contains__(self)
is translated as follows:NOTE
PySequence_Contains(self)
call there which callsMyBytes.__contains__
again and leads to recursion.Thanks beforehand,
Kirill
The text was updated successfully, but these errors were encountered: