diff options
author | da-woods <dw-git@d-woods.co.uk> | 2022-05-16 12:03:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-16 13:03:35 +0200 |
commit | 0ad6b33065d2216cea3d52e14ce63eeef4e15d56 (patch) | |
tree | 64fbc4781002250a995ab4c6063de42eb0318d51 /tests | |
parent | c82da6db9d2ee54783e6b1eb0420c27c129da5a2 (diff) | |
download | cython-0ad6b33065d2216cea3d52e14ce63eeef4e15d56.tar.gz |
Remove ".__contains__" -> "PySequence_Contains" slot mappings for known builtin types (GH-4792)
They prevent explicitly calling the base-class __contains__.
Closes https://github.com/cython/cython/issues/4785
Diffstat (limited to 'tests')
-rw-r--r-- | tests/run/builtin_subtype_methods_T653.pyx | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/run/builtin_subtype_methods_T653.pyx b/tests/run/builtin_subtype_methods_T653.pyx index 522cffff8..3b88a9fb1 100644 --- a/tests/run/builtin_subtype_methods_T653.pyx +++ b/tests/run/builtin_subtype_methods_T653.pyx @@ -4,7 +4,21 @@ cimport cython +# The "contains" tests relate to GH-4785 - replacing the method +# call with PySequence_Contains was causing infinite recursion +# for some classes + cdef class MyList(list): + """ + >>> l = MyList() + >>> l.__contains__(1) + MyList.__contains__ + False + >>> l.append(1) + >>> l.__contains__(1) + MyList.__contains__ + True + """ def test_append(self, x): """ >>> l = MyList() @@ -18,7 +32,13 @@ cdef class MyList(list): """ self.append(x) + def __contains__(self, value): + print "MyList.__contains__" + return list.__contains__(self, value) # probably optimized + cdef class MyDict(dict): + # tests for __contains__ are in the global __doc__ to version-check a PyPy bug + @cython.test_assert_path_exists("//ComprehensionNode//AttributeNode", "//ComprehensionNode//AttributeNode[@attribute='items']") @cython.test_fail_if_path_exists("//ComprehensionNode//CMethodSelfCloneNode") @@ -40,6 +60,19 @@ cdef class MyDict(dict): l.sort() return l + def __contains__(self, key): + print "MyDict.__contains__" + return dict.__contains__(self, key) + +import sys +pypy_version = getattr(sys, 'pypy_version_info', None) +if not (pypy_version and pypy_version < (7, 3, 10)): + __doc__ = """ + >>> MyDict(a=1).__contains__("a") + MyDict.__contains__ + True + """ + @cython.final cdef class MyDictFinal(dict): @cython.test_assert_path_exists("//ComprehensionNode//CMethodSelfCloneNode") @@ -155,3 +188,17 @@ cdef class MyDictOverride2(MyDict): l = [ v for v in self.values() ] l.sort() return l + +class MyBytes(bytes): + """ + >>> mb = MyBytes(b"abc") + >>> mb.__contains__(b"a") + MyBytes.__contains__ + True + >>> mb.__contains__(b"z") + MyBytes.__contains__ + False + """ + def __contains__(self, value): + print "MyBytes.__contains__" + return bytes.__contains__(self, value) |