aboutsummaryrefslogtreecommitdiff
path: root/pypy
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2021-02-16 17:04:00 +0200
committerMatti Picus <matti.picus@gmail.com>2021-02-16 17:04:00 +0200
commita741833be17fbae4963123c1d30ac38ca2bdca34 (patch)
tree849dd76b3c150047f9a23df83afd7275847f6dd3 /pypy
parentmention the paper and Remi's PhD thesis on the stm page (diff)
downloadpypy-a741833be17fbae4963123c1d30ac38ca2bdca34.tar.gz
pypy-a741833be17fbae4963123c1d30ac38ca2bdca34.tar.bz2
pypy-a741833be17fbae4963123c1d30ac38ca2bdca34.zip
test, add PyUnicode_Contains (issue 3400)
Diffstat (limited to 'pypy')
-rw-r--r--pypy/module/cpyext/stubs.py9
-rw-r--r--pypy/module/cpyext/test/test_unicodeobject.py9
-rw-r--r--pypy/module/cpyext/unicodeobject.py15
3 files changed, 24 insertions, 9 deletions
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
index 23e818ee94..aa7ffc005b 100644
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1611,15 +1611,6 @@ def PyUnicode_RichCompare(space, left, right, op):
Py_NE, Py_LT, and Py_LE."""
raise NotImplementedError
-@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
-def PyUnicode_Contains(space, container, element):
- """Check whether element is contained in container and return true or false
- accordingly.
-
- element has to coerce to a one element Unicode string. -1 is returned if
- there was an error."""
- raise NotImplementedError
-
@cpython_api([rffi.INT_real, rffi.CCHARPP], rffi.INT_real, error=2)
def Py_Main(space, argc, argv):
"""The main program for the standard interpreter. This is made available for
diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py
index 73fefaf466..7791198c6e 100644
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -695,6 +695,15 @@ class TestUnicode(BaseApiTest):
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 0, 4, -1) == 2
assert PyUnicode_Find(space, w_str, space.wrap(u"z"), 0, 4, -1) == -1
+ def test_contains(self, space):
+ w_str = space.wrap(u"abcabcd")
+ assert PyUnicode_Contains(space, w_str, space.wrap(u"a")) == 1
+ assert PyUnicode_Contains(space, w_str, space.wrap(u"e")) == 0
+ with raises_w(space, TypeError):
+ PyUnicode_Contains(space, w_str, space.wrap(1)) == -1
+ with raises_w(space, TypeError) as e:
+ PyUnicode_Contains(space, space.wrap(1), space.wrap(u"a")) == -1
+
def test_split(self, space):
w_str = space.wrap(u"a\nb\nc\nd")
assert "[u'a', u'b', u'c', u'd']" == space.unwrap(space.repr(
diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py
index 9b92df2d05..645ea668c3 100644
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -744,6 +744,21 @@ def PyUnicode_Find(space, w_str, w_substr, start, end, direction):
space.newint(start), space.newint(end))
return space.int_w(w_pos)
+@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PyUnicode_Contains(space, w_str, w_substr):
+ """Check whether element is contained in container and return true or false
+ accordingly.
+
+ element has to coerce to a one element Unicode string. -1 is returned if
+ there was an error."""
+ if not space.isinstance_w(w_substr, space.w_unicode):
+ raise oefmt(space.w_TypeError,
+ "in <string> requires string as left operand, not %T",
+ w_substr)
+ if not space.isinstance_w(w_str, space.w_unicode):
+ raise oefmt(space.w_TypeError, "must be str, not %T", w_str)
+ return space.int_w(space.call_method(w_str, '__contains__', w_substr))
+
@cpython_api([PyObject, PyObject, Py_ssize_t], PyObject)
def PyUnicode_Split(space, w_str, w_sep, maxsplit):
"""Split a string giving a list of Unicode strings. If sep is