[pypy-commit] pypy py3k: issue1883: add PyUnicode_InternFromString

pjenvey noreply at buildbot.pypy.org
Mon Oct 13 23:52:33 CEST 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r73930:bd8819a48208
Date: 2014-10-13 14:52 -0700
http://bitbucket.org/pypy/pypy/changeset/bd8819a48208/

Log:	issue1883: add PyUnicode_InternFromString

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
@@ -219,6 +219,13 @@
         assert space.unwrap(w_res) == u'sp�'
         rffi.free_charp(s)
 
+    def test_internfromstring(self, space, api):
+        with rffi.scoped_str2charp('foo') as s:
+            w_res = api.PyUnicode_InternFromString(s)
+            assert space.unwrap(w_res) == u'foo'
+            w_res2 = api.PyUnicode_InternFromString(s)
+            assert w_res is w_res2
+
     def test_unicode_resize(self, space, api):
         py_uni = new_empty_unicode(space, 10)
         ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
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
@@ -508,6 +508,16 @@
     w_str = space.wrapbytes(rffi.charp2str(s))
     return space.call_method(w_str, 'decode', space.wrap("utf-8"))
 
+ at cpython_api([CONST_STRING], PyObject)
+def PyUnicode_InternFromString(space, s):
+    """A combination of PyUnicode_FromString() and
+    PyUnicode_InternInPlace(), returning either a new unicode string
+    object that has been interned, or a new ("owned") reference to an
+    earlier interned string object with the same value.
+    """
+    w_str = PyUnicode_FromString(space, s)
+    return space.new_interned_w_str(w_str)
+
 @cpython_api([CONST_STRING, Py_ssize_t], PyObject)
 def PyUnicode_FromStringAndSize(space, s, size):
     """Create a Unicode Object from the char buffer u. The bytes will be


More information about the pypy-commit mailing list