[pypy-svn] r23236 - in pypy/dist/pypy/objspace/std: . test

stephan at codespeak.net stephan at codespeak.net
Sat Feb 11 19:46:58 CET 2006


Author: stephan
Date: Sat Feb 11 19:46:56 2006
New Revision: 23236

Modified:
   pypy/dist/pypy/objspace/std/complextype.py
   pypy/dist/pypy/objspace/std/test/test_complexobject.py
Log:
fixed some more bugs in objspace/std/complextype.py.
most tests are passed now.


Modified: pypy/dist/pypy/objspace/std/complextype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/complextype.py	(original)
+++ pypy/dist/pypy/objspace/std/complextype.py	Sat Feb 11 19:46:56 2006
@@ -44,7 +44,11 @@
         if newstop < 0:
             raise ValueError('complex() arg is a malformed string')
         if s[newstop] in ('j','J'):
-            return '0.0',s[realstart:newstop]
+            if realstart == newstop:
+                imagpart = '1.0'
+            else:
+                imagpart = s[realstart:newstop]
+            return '0.0', imagpart
         else:
             return s[realstart:realstop],'0.0'
 
@@ -74,7 +78,7 @@
         raise ValueError('complex() arg is a malformed string')
     if s[imagstop] not in ('j','J'):
         raise ValueError('complex() arg is a malformed string')
-    if imagstop <= imagstart:
+    if imagstop < imagstart:
         raise ValueError('complex() arg is a malformed string')
 
     while i<slen and s[i] == ' ':
@@ -83,26 +87,46 @@
         raise ValueError('complex() arg is a malformed string')
 
     realpart = s[realstart:realstop]
-    if imagsign == '-':
-        imagpart = imagsign + s[imagstart:imagstop]
+    if imagstart == imagstop:
+        imagpart = '1.0'
     else:
         imagpart = s[imagstart:imagstop]
+    if imagsign == '-':
+        imagpart = imagsign + imagpart
 
     return realpart, imagpart
 
 
 def check_second_arg(space, w_c):
+    """check, if second 'complex' argument is a string"""
     if space.is_true(space.isinstance(w_c, space.w_str)):
         raise TypeError()
     return True
 
+def simple_arg_check(space, w_r, w_c):
+    """check, if there is a second argument, if first is a string"""
+    if space.is_true(space.isinstance(w_r, space.w_str)) or \
+            space.is_true(space.isinstance(w_r, space.w_unicode)):
+        if not space.eq_w(w_c,space.w_None):
+            raise TypeError
+
 def descr__new__(space, w_complextype, w_real=0.0, w_imag=None):
     from pypy.objspace.std.complexobject import W_ComplexObject
     try:
         check_second_arg(space, w_imag)
     except TypeError:
         raise OperationError(space.w_TypeError,space.wrap("complex() second arg can't be a string"))
-
+    try:
+        simple_arg_check(space, w_real, w_imag)
+    except TypeError:
+        raise OperationError(space.w_TypeError, space.wrap(ERR_WRONG_SECOND))
+    # if arguments can be cast to a float, do it
+    try:
+        w_real = cast_float(space,w_real)
+    except:pass
+    try:
+        w_imag = cast_float(space,w_imag)
+    except:pass
     w_complex_first = extract_complex(space, w_real)
     if not space.eq_w(w_complex_first, space.w_None):
         w_real = w_complex_first
@@ -115,9 +139,6 @@
         return space.add(w_real,w_imag)
     if space.is_true(space.isinstance(w_real, space.w_str)) or \
             space.is_true(space.isinstance(w_real, space.w_unicode)):
-        if not space.eq_w(w_imag,space.w_None):
-            raise OperationError(space.w_ValueError, 
-                    space.wrap(ERR_WRONG_SECOND))
         try:
             realstr, imagstr = _split_complex(space.str_w(w_real))
 
@@ -132,6 +153,7 @@
         if space.eq_w(w_imag,space.w_None):
             w_imag = space.wrap(0.0)
         try:
+
             realval = space.float_w(w_real)
             imagval = space.float_w(w_imag)
         except ValueError, e:
@@ -145,17 +167,18 @@
 def extract_complex(num):
     if not hasattr(num,'__complex__'):
         return None
-    try:
-        cnum = num.__complex__()
-    except:
-        return None
+    cnum = num.__complex__()
     if isinstance(cnum,complex):
         return cnum
     else:
         return None
+
+def cast_float(num):
+    return float(num)
 """, filename=__file__)
 
 extract_complex = app.interphook('extract_complex')
+cast_float = app.interphook('cast_float')
 
 def descr_conjugate(space, w_self):
     from pypy.objspace.std.complexobject import W_ComplexObject

Modified: pypy/dist/pypy/objspace/std/test/test_complexobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_complexobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_complexobject.py	Sat Feb 11 19:46:56 2006
@@ -60,8 +60,8 @@
         c = cobj.W_ComplexObject(self.space,0.0,1.0)
         p = cobj.W_ComplexObject(self.space,2.0,0.0)
         r = cobj.pow__Complex_Complex_ANY(self.space,c,p,self.space.wrap(None))
-        assert r._real == -1.0
-        assert r._imag == 0.0
+        assert r.realval == -1.0
+        assert r.imagval == 0.0
 
 
 class AppTestAppComplexTest:
@@ -188,7 +188,7 @@
         h = self.helper
         h.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
 
-    def x_test_constructor(self):
+    def test_constructor(self):
         h = self.helper
         class OS:
             def __init__(self, value): self.value = value



More information about the Pypy-commit mailing list