[pypy-commit] pypy py3k: allow special unicode decimal digits and spaces in complex ctor

antocuni noreply at buildbot.pypy.org
Wed Oct 17 18:55:56 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r58181:487439d3c669
Date: 2012-10-17 18:48 +0200
http://bitbucket.org/pypy/pypy/changeset/487439d3c669/

Log:	allow special unicode decimal digits and spaces in complex ctor

diff --git a/pypy/objspace/std/complextype.py b/pypy/objspace/std/complextype.py
--- a/pypy/objspace/std/complextype.py
+++ b/pypy/objspace/std/complextype.py
@@ -6,6 +6,7 @@
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.stdtypedef import GetSetProperty, StdTypeDef
 from pypy.objspace.std.stdtypedef import StdObjSpaceMultiMethod
+from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
 
 # ERRORCODES
 
@@ -130,15 +131,15 @@
                and space.is_w(space.type(w_real), space.w_complex)):
         return w_real
 
-    if space.isinstance_w(w_real, space.w_str) or \
-            space.isinstance_w(w_real, space.w_unicode):
+    if space.isinstance_w(w_real, space.w_unicode):
         # a string argument
         if not noarg2:
             raise OperationError(space.w_TypeError,
                                  space.wrap("complex() can't take second arg"
                                             " if first is a string"))
+        unistr = unicode_to_decimal_w(space, w_real)
         try:
-            realstr, imagstr = _split_complex(space.unicode_w(w_real))
+            realstr, imagstr = _split_complex(unistr)
         except ValueError:
             raise OperationError(space.w_ValueError, space.wrap(ERR_MALFORMED))
         try:
diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -1,3 +1,4 @@
+# -*- encoding: utf-8 -*-
 from __future__ import print_function
 
 import py
@@ -87,6 +88,8 @@
 
 
 class AppTestAppComplexTest:
+    spaceconfig = {'usemodules': ('unicodedata',)}
+    
     def w_check_div(self, x, y):
         """Compute complex z=x*y, and check that z/x==y and z/y==x."""
         z = x * y
@@ -254,6 +257,7 @@
 
         raises(TypeError, complex, OS(None))
         raises(TypeError, complex, NS(None))
+        raises(TypeError, complex, b'10')
 
         # -- The following cases are not supported by CPython, but they
         # -- are supported by PyPy, which is most probably ok
@@ -349,6 +353,13 @@
         assert self.almost_equal(complex(real=float2(17.), imag=float2(23.)), 17+23j)
         raises(TypeError, complex, float2(None))
 
+    def test_constructor_unicode(self):
+        b1 = '\N{MATHEMATICAL BOLD DIGIT ONE}' # 𝟏
+        b2 = '\N{MATHEMATICAL BOLD DIGIT TWO}' # 𝟐
+        s = '{0} + {1}j'.format(b1, b2)
+        assert complex(s) == 1+2j
+        assert complex('\N{EM SPACE}(1+1j)')
+
     def test_hash(self):
         for x in range(-30, 30):
             assert hash(x) == hash(complex(x, 0))


More information about the pypy-commit mailing list