[pypy-svn] r42179 - in pypy/branch/pypy-string-formatting/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Thu Apr 19 18:23:06 CEST 2007


Author: arigo
Date: Thu Apr 19 18:23:06 2007
New Revision: 42179

Modified:
   pypy/branch/pypy-string-formatting/objspace/std/formatting.py
   pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py
Log:
More stuff...


Modified: pypy/branch/pypy-string-formatting/objspace/std/formatting.py
==============================================================================
--- pypy/branch/pypy-string-formatting/objspace/std/formatting.py	(original)
+++ pypy/branch/pypy-string-formatting/objspace/std/formatting.py	Thu Apr 19 18:23:06 2007
@@ -91,6 +91,47 @@
 
         return w_value
 
+    def peel_flags(self):
+        self.f_ljust = False
+        self.f_sign  = False
+        self.f_blank = False
+        self.f_alt   = False
+        self.f_zero  = False
+        while True:
+            c = self.peekchr()
+            if c == '-':
+                self.f_ljust = True
+            elif c == '+':
+                self.f_sign = True
+            elif c == ' ':
+                self.f_blank = True
+            elif c == '#':
+                self.f_alt = True
+            elif c == '0':
+                self.f_zero = True
+            else:
+                break
+            self.forward()
+
+    def peel_num(self):
+        space = self.space
+        c = self.peekchr()
+        if c == '*':
+            self.forward()
+            w_value = self.nextinputvalue()
+            return space.int_w(maybe_int(space, w_value))
+        result = 0
+        while '0' <= c <= '9':
+            n = ord(c) - ord('0')
+            try:
+                result = ovfcheck(ovfcheck(result * 10) + n)
+            except OverflowError:
+                raise OperationError(space.w_OverflowError,
+                                     space.wrap("precision too large"))
+            self.forward()
+            c = self.peekchr()
+        return result
+
     def format(self):
         result = []      # list of characters
         self.result = result
@@ -155,5 +196,57 @@
     formatter = StringFormatter(space, fmt, values_w, w_valuedict)
     return formatter.format()
 
+# ____________________________________________________________
+# Formatting helpers
 
-...
+def maybe_int(space, w_value):
+    # make sure that w_value is a wrapped integer
+    return space.int(w_value)
+
+def numeric_preprocess(v, flags):
+    # negative zeroes?
+    # * mwh giggles, falls over
+    # still, if we can recognize them, here's the place to do it.
+    import math
+    if v < 0 or v == 0 and isinstance(v, float) and math.atan2(0, v) != 0:
+        sign = '-'
+        v = -v
+    else:
+        if flags.f_sign:
+            sign = '+'
+        elif flags.f_blank:
+            sign = ' '
+        else:
+            sign = ''
+    return v, sign
+#numeric_preprocess._annspecialcase_ = 'specialize:argtype(0)'
+
+def format_num_helper_generator(fun_name, fun):
+    def format_num_helper(formatter, w_value):
+        w_value = maybe_int(formatter.space, w_value)
+        try:
+            value = space.int_w(w_value)
+            value, sign = numeric_preprocess(value, formatter)
+            return (fun(value), sign)
+        except OperationError, operr:
+            # XXX: Fix it, this is obviously inefficient
+            if not operr.match(space, space.w_OverflowError):
+                raise
+            if space.is_true(space.lt(v, space.wrap(0))):
+                sign = '-'
+                v = space.neg(v)
+            else:
+                if flags.f_sign:
+                    sign = '+'
+                elif flags.f_blank:
+                    sign = ' '
+                else:
+                    sign = ''
+            val = space.str_w(getattr(space, fun_name)(v))
+            return (val, sign)
+    format_num_helper.func_name = fun_name + '_num_helper'
+    return format_num_helper
+
+hex_num_helper = format_num_helper_generator('hex', hex)
+oct_num_helper = format_num_helper_generator('oct', oct)
+int_num_helper = format_num_helper_generator('str', str)

Modified: pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py	(original)
+++ pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py	Thu Apr 19 18:23:06 2007
@@ -84,6 +84,8 @@
         assert '0x0' == '%#x' % 0
         assert '23' == '%s' % 23
         assert '23' == '%r' % 23
+        assert ('%d' % (-sys.maxint-1,) == '-' + str(sys.maxint+1)
+                                        == '-%d' % (sys.maxint+1,))
 
     def test_format_list(self):
         assert '<[1, 2]>' == '<%s>' % [1,2]



More information about the Pypy-commit mailing list