[pypy-commit] pypy cpyext-ext: test, implement unicode hash

mattip pypy.commits at gmail.com
Wed Dec 23 17:24:31 EST 2015


Author: mattip <matti.picus at gmail.com>
Branch: cpyext-ext
Changeset: r81438:43629fab94e1
Date: 2015-12-20 20:47 +0200
http://bitbucket.org/pypy/pypy/changeset/43629fab94e1/

Log:	test, implement unicode hash

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
@@ -66,6 +66,7 @@
                  c = PyUnicode_AsUnicode(s);
                  c[0] = 'a';
                  c[1] = 0xe9;
+                 c[2] = 0x00;
                  c[3] = 'c';
                  return s;
              """),
@@ -74,6 +75,18 @@
         assert len(s) == 4
         assert s == u'a�\x00c'
 
+    def test_hash(self):
+        module = self.import_extension('foo', [
+            ("test_hash", "METH_VARARGS",
+             '''
+                PyObject* obj = (PyTuple_GetItem(args, 0));
+                long hash = ((PyUnicodeObject*)obj)->hash;
+                return PyLong_FromLong(hash);  
+             '''
+             ),
+            ])
+        res = module.test_hash(u"xyz")
+        assert res == hash(u'xyz')
 
 
 class TestUnicode(BaseApiTest):
@@ -575,6 +588,3 @@
                 api.PyUnicode_Splitlines(w_str, 0)))
         assert r"[u'a\n', u'b\n', u'c\n', u'd']" == space.unwrap(space.repr(
                 api.PyUnicode_Splitlines(w_str, 1)))
-
-    def test_hash_and_defenc(self, space, api):
-        assert False # XXX test newly added struct members hash and defenc
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
@@ -58,7 +58,7 @@
     py_uni.c_str = lltype.malloc(rffi.CWCHARP.TO, buflen,
                                     flavor='raw', zero=True)
     py_uni.c_hash = -1
-    #py_uni.c_defenc = lltype.nullptr(PyObject)
+    py_uni.c_defenc = lltype.nullptr(PyObject.TO)
     return py_uni
 
 def unicode_attach(space, py_obj, w_obj):
@@ -66,8 +66,9 @@
     py_unicode = rffi.cast(PyUnicodeObject, py_obj)
     py_unicode.c_length = len(space.unicode_w(w_obj))
     py_unicode.c_str = lltype.nullptr(rffi.CWCHARP.TO)
-    py_unicode.c_hash = -1
-    #py_unicode.c_defenc = lltype.nullptr(PyObject)
+    print w_obj
+    py_unicode.c_hash = space.hash_w(w_obj)
+    py_unicode.c_defenc = lltype.nullptr(PyObject.TO)
 
 def unicode_realize(space, py_obj):
     """
@@ -77,6 +78,7 @@
     py_uni = rffi.cast(PyUnicodeObject, py_obj)
     s = rffi.wcharpsize2unicode(py_uni.c_str, py_uni.c_length)
     w_obj = space.wrap(s)
+    py_uni.c_hash = space.hash_w(w_obj)
     track_reference(space, py_obj, w_obj)
     return w_obj
 


More information about the pypy-commit mailing list