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

stephan at codespeak.net stephan at codespeak.net
Wed Feb 8 20:11:40 CET 2006


Author: stephan
Date: Wed Feb  8 20:11:38 2006
New Revision: 23157

Modified:
   pypy/dist/pypy/objspace/std/complextype.py
   pypy/dist/pypy/objspace/std/test/helper.py
   pypy/dist/pypy/objspace/std/test/test_complexobject.py
Log:
replaced the complex number parser (very, very low level code)


Modified: pypy/dist/pypy/objspace/std/complextype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/complextype.py	(original)
+++ pypy/dist/pypy/objspace/std/complextype.py	Wed Feb  8 20:11:38 2006
@@ -9,33 +9,80 @@
 ERR_MALFORMED = "complex() arg is a malformed string"
 
 def _split_complex(s):
-    s = s.replace(' ','')
     slen = len(s)
-    realnum = '0.0'
-    imagnum = '0.0'
-    pc = ''
+    if slen == 0:
+        raise ValueError('complex() arg is a malformed string')
+    realstart = 0
+    realstop = 0
+    imagstart = 0
+    imagstop = 0
+    imagsign = ' '
     i = 0
-    bp = 0
-    while i < slen:
-        c = s[i]
-        if c in ('+','-') and pc not in ('e','E'):
-            bp = i
+    # ignore whitespace
+    while i < slen and s[i] == ' ':
+        i += 1
+
+    # extract first number
+    realstart = i
+    pc = s[i]
+    while i < slen and s[i] != ' ': 
+        if s[i] in ('+','-') and pc not in ('e','E') and i != realstart:
             break
-        pc = c
+        pc = s[i]
         i += 1
-    if bp:
-        if s[-1] not in ['j','J']:
-            raise ValueError('complex() arg is a malformed string')
-        realnum = s[:bp]
-        imagnum = s[bp+1:-1]
-    else:
-        if s[-1] in ['j','J']:
-            imagnum = s[:-1]
+
+    realstop = i
+
+    # ignore whitespace
+    while i < slen and s[i] == ' ':
+        i += 1
+
+    # return appropriate strings is only one number is there
+    if i >= slen:
+        if s[realstop-1] in ('j','J'):
+            return '0.0',s[realstart:realstop - 1]
         else:
-            realnum = s
+            return s[realstart:realstop],'0.0'
+
+    # find sign for imaginary part
+    if s[i] == '-' or s[i] == '+':
+        imagsign = s[i]
+    if imagsign == ' ':
+        raise ValueError('complex() arg is a malformed string')
+
+    i+=1
+    # whitespace
+    while i < slen and s[i] == ' ':
+        i += 1
+    if i >= slen:
+        raise ValueError('complex() arg is a malformed string')
+
+    imagstart = i
+    pc = s[i]
+    while i < slen and s[i] != ' ':
+        if s[i] in ('+','-') and pc not in ('e','E'):
+            break
+        pc = s[i]
+        i += 1
 
-    return realnum, imagnum
+    imagstop = i - 1
+    if s[imagstop] not in ('j','J'):
+        raise ValueError('complex() arg is a malformed string')
+    if imagstop <= imagstart:
+        raise ValueError('complex() arg is a malformed string')
+
+    while i<slen and s[i] == ' ':
+        i += 1
+    if i <  slen:
+        raise ValueError('complex() arg is a malformed string')
+
+    realpart = s[realstart:realstop]
+    if imagsign == '-':
+        imagpart = imagsign + s[imagstart:imagstop]
+    else:
+        imagpart = s[imagstart:imagstop]
 
+    return realpart, imagpart
 
 
 def check_second_arg(space, w_c):

Modified: pypy/dist/pypy/objspace/std/test/helper.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/helper.py	(original)
+++ pypy/dist/pypy/objspace/std/test/helper.py	Wed Feb  8 20:11:38 2006
@@ -9,6 +9,9 @@
 def assertEqual(a, b):
     assert a == b
 
+def assertNotEqual(a, b):
+    assert a != b
+
 def assertAlmostEqual(a, b):
     if isinstance(a, complex):
         if isinstance(b, complex):

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	Wed Feb  8 20:11:38 2006
@@ -58,7 +58,7 @@
 
 
 class AppTestAppComplexTest:
-    def x_test_div(self):
+    def test_div(self):
         import helper as h
         simple_real = [float(i) for i in xrange(-5, 6)]
         simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
@@ -282,25 +282,26 @@
             x /= 3.0    # now check against floating point
             h.assertEqual(hash(x), hash(complex(x, 0.)))
 
-    def x_test_abs(self):
+    def test_abs(self):
         import helper as h
         nums = [complex(x/3., y/7.) for x in xrange(-9,9) for y in xrange(-9,9)]
         for num in nums:
-            self.assertAlmostEqual((num.real**2 + num.imag**2)  ** 0.5, abs(num))
+            h.assertAlmostEqual((num.real**2 + num.imag**2)  ** 0.5, abs(num))
 
-    def x_test_repr(self):
+    def test_repr(self):
         import helper as h
-        self.assertEqual(repr(1+6j), '(1+6j)')
-        self.assertEqual(repr(1-6j), '(1-6j)')
+        h.assertEqual(repr(1+6j), '(1+6j)')
+        h.assertEqual(repr(1-6j), '(1-6j)')
 
-        self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
+        h.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
 
-    def x_test_neg(self):
+    def test_neg(self):
         import helper as h
-        self.assertEqual(-(1+6j), -1-6j)
+        h.assertEqual(-(1+6j), -1-6j)
 
     def x_test_file(self):
         import helper as h
+        import os
         a = 3.33+4.43j
         b = 5.1+2.3j
 
@@ -310,7 +311,7 @@
             print >>fo, a, b
             fo.close()
             fo = open(test_support.TESTFN, "rb")
-            self.assertEqual(fo.read(), "%s %s\n" % (a, b))
+            h.assertEqual(fo.read(), "%s %s\n" % (a, b))
         finally:
             if (fo is not None) and (not fo.closed):
                 fo.close()



More information about the Pypy-commit mailing list