Author: Amaury Forgeot d'Arc <amaur...@gmail.com> Branch: Changeset: r52641:0f8cad650dd3 Date: 2012-02-19 18:24 +0100 http://bitbucket.org/pypy/pypy/changeset/0f8cad650dd3/
Log: cpyext: add PyUnicode_Replace diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py --- a/pypy/module/cpyext/stubs.py +++ b/pypy/module/cpyext/stubs.py @@ -2373,16 +2373,6 @@ properly supporting 64-bit systems.""" raise NotImplementedError -@cpython_api([PyObject, PyObject, PyObject, Py_ssize_t], PyObject) -def PyUnicode_Replace(space, str, substr, replstr, maxcount): - """Replace at most maxcount occurrences of substr in str with replstr and - return the resulting Unicode object. maxcount == -1 means replace all - occurrences. - - This function used an int type for maxcount. This might - require changes in your code for properly supporting 64-bit systems.""" - raise NotImplementedError - @cpython_api([PyObject, PyObject, rffi.INT_real], PyObject) def PyUnicode_RichCompare(space, left, right, op): """Rich compare two unicode strings and return one of the following: diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py --- a/pypy/module/cpyext/test/test_unicodeobject.py +++ b/pypy/module/cpyext/test/test_unicodeobject.py @@ -429,3 +429,11 @@ w_char = api.PyUnicode_FromOrdinal(0xFFFF) assert space.unwrap(w_char) == u'\uFFFF' + def test_replace(self, space, api): + w_str = space.wrap(u"abababab") + w_substr = space.wrap(u"a") + w_replstr = space.wrap(u"z") + assert u"zbzbabab" == space.unwrap( + api.PyUnicode_Replace(w_str, w_substr, w_replstr, 2)) + assert u"zbzbzbzb" == space.unwrap( + api.PyUnicode_Replace(w_str, w_substr, w_replstr, -1)) diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py --- a/pypy/module/cpyext/unicodeobject.py +++ b/pypy/module/cpyext/unicodeobject.py @@ -548,6 +548,15 @@ @cpython_api([PyObject, PyObject], PyObject) def PyUnicode_Join(space, w_sep, w_seq): - """Join a sequence of strings using the given separator and return the resulting - Unicode string.""" + """Join a sequence of strings using the given separator and return + the resulting Unicode string.""" return space.call_method(w_sep, 'join', w_seq) + +@cpython_api([PyObject, PyObject, PyObject, Py_ssize_t], PyObject) +def PyUnicode_Replace(space, w_str, w_substr, w_replstr, maxcount): + """Replace at most maxcount occurrences of substr in str with replstr and + return the resulting Unicode object. maxcount == -1 means replace all + occurrences.""" + return space.call_method(w_str, "replace", w_substr, w_replstr, + space.wrap(maxcount)) + _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit