[pypy-commit] pypy py3.5: Add badly faked impl of PyUnicode_GetLength

rlamy pypy.commits at gmail.com
Tue Dec 13 07:26:15 EST 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r89041:6625a5970bb8
Date: 2016-12-13 12:25 +0000
http://bitbucket.org/pypy/pypy/changeset/6625a5970bb8/

Log:	Add badly faked impl of PyUnicode_GetLength

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
@@ -32,6 +32,17 @@
                  Py_DECREF(s);
                  return PyLong_FromLong(result);
              """),
+            ("test_GetLength", "METH_NOARGS",
+             """
+                 PyObject* s = PyUnicode_FromString("Hello world");
+                 int result = 0;
+
+                 if(PyUnicode_GetLength(s) != 11) {
+                     result = -PyUnicode_GetSize(s);
+                 }
+                 Py_DECREF(s);
+                 return PyLong_FromLong(result);
+             """),
             ("test_GetSize_exception", "METH_NOARGS",
              """
                  PyObject* f = PyFloat_FromDouble(1.0);
@@ -48,6 +59,9 @@
         assert module.test_GetSize() == 0
         raises(TypeError, module.test_GetSize_exception)
 
+        # XXX: needs a test where it differs from GetSize
+        assert module.test_GetLength() == 0
+
         assert module.test_is_unicode(u"")
         assert not module.test_is_unicode(())
 
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
@@ -253,6 +253,11 @@
 
 @cpython_api([PyObject], Py_ssize_t, error=-1)
 def PyUnicode_GetSize(space, ref):
+    """Return the size of the deprecated Py_UNICODE representation, in code
+    units (this includes surrogate pairs as 2 units).
+
+    Please migrate to using PyUnicode_GetLength().
+    """
     if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_unicode:
         ref = rffi.cast(PyUnicodeObject, ref)
         return ref.c_length
@@ -260,6 +265,15 @@
         w_obj = from_ref(space, ref)
         return space.len_w(w_obj)
 
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
+def PyUnicode_GetLength(space, w_unicode):
+    """Return the length of the Unicode object, in code points."""
+    # XXX: this is a stub
+    if not PyUnicode_Check(space, w_unicode):
+        PyErr_BadArgument(space)
+    #PyUnicode_READY(w_unicode)
+    return PyUnicode_GET_LENGTH(space, w_unicode)
+
 @cpython_api([PyObject, rffi.CWCHARP, Py_ssize_t], Py_ssize_t, error=-1)
 def PyUnicode_AsWideChar(space, ref, buf, size):
     """Copy the Unicode object contents into the wchar_t buffer w.  At most


More information about the pypy-commit mailing list