[pypy-commit] pypy unicode-utf8-py3: test, fix for surrogates in type name

mattip pypy.commits at gmail.com
Tue Nov 13 03:19:58 EST 2018


Author: Matti Picus <matti.picus at gmail.com>
Branch: unicode-utf8-py3
Changeset: r95308:720cb17cdc85
Date: 2018-11-12 20:29 -0800
http://bitbucket.org/pypy/pypy/changeset/720cb17cdc85/

Log:	test, fix for surrogates in type name

diff --git a/pypy/objspace/std/test/test_typeobject.py b/pypy/objspace/std/test/test_typeobject.py
--- a/pypy/objspace/std/test/test_typeobject.py
+++ b/pypy/objspace/std/test/test_typeobject.py
@@ -942,12 +942,15 @@
         assert Abc.__name__ == 'Def'
         raises(TypeError, "Abc.__name__ = 42")
         raises(TypeError, "Abc.__name__ = b'A'")
-        try:
-            Abc.__name__ = 'G\x00hi'
-        except ValueError as e:
-            assert str(e) == "type name must not contain null characters"
-        else:
-            assert False
+        for v, err in [('G\x00hi', "type name must not contain null characters"),
+                       ('A\udcdcB', "surrogates not allowed")]:
+            try:
+                Abc.__name__ = v
+            except ValueError as e:
+                assert err in str(e)
+            else:
+                assert False
+            assert Abc.__name__ == 'Def'
 
     def test_qualname(self):
         A = type('A', (), {'__qualname__': 'B.C'})
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -14,7 +14,7 @@
 from rpython.rlib.objectmodel import current_object_addr_as_int, compute_hash
 from rpython.rlib.objectmodel import we_are_translated, not_rpython
 from rpython.rlib.rarithmetic import intmask, r_uint
-from rpython.rlib.rutf8 import CheckError, check_utf8
+from rpython.rlib.rutf8 import CheckError, check_utf8, surrogate_in_utf8
 
 class MutableCell(W_Root):
     def unwrap_cell(self, space):
@@ -801,9 +801,12 @@
             return space.call_function(newfunc, w_winner, w_name, w_bases, w_dict)
         w_typetype = w_winner
 
-    name = space.text_w(w_name) # NB. CPython forbids surrogates here
+    name = space.text_w(w_name) 
     if '\x00' in name:
         raise oefmt(space.w_ValueError, "type name must not contain null characters")
+    if surrogate_in_utf8(name) >= 0:
+        raise oefmt(space.w_ValueError, "can't encode character %c in position "
+                    "%i, surrogates not allowed", name[pos], pos)
     dict_w = {}
     dictkeys_w = space.listview(w_dict)
     for w_key in dictkeys_w:
@@ -868,6 +871,10 @@
     name = space.text_w(w_value)
     if '\x00' in name:
         raise oefmt(space.w_ValueError, "type name must not contain null characters")
+    pos = surrogate_in_utf8(name)
+    if pos >= 0:
+        raise oefmt(space.w_ValueError, "can't encode character %s in position "
+                    "%d, surrogates not allowed", name[pos], pos)
     w_type.name = name
 
 def descr_get__qualname__(space, w_type):


More information about the pypy-commit mailing list