[pypy-commit] pypy py3k: Add alternate float formatting styles to new-style formatting to PyPy Py3k.

soareschen noreply at buildbot.pypy.org
Sun Jul 8 14:38:37 CEST 2012


Author: soareschen
Branch: py3k
Changeset: r55994:3e7a6cf89b48
Date: 2012-07-07 15:58 +0200
http://bitbucket.org/pypy/pypy/changeset/3e7a6cf89b48/

Log:	Add alternate float formatting styles to new-style formatting to
	PyPy Py3k. Based on Python issue #7094. Also switch complex number
	formatting implementation to use rfloat.double_to_string instead of
	formatd.

	Contributors: Soares Chen <soares.chen at gmail.com> Tomasz Rybak
	<tomasz.rybak at post.pl>

diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py
--- a/pypy/objspace/std/newformat.py
+++ b/pypy/objspace/std/newformat.py
@@ -927,8 +927,8 @@
             flags = 0
             default_precision = 6
             if self._alternate:
-                msg = "alternate form not allowed in float formats"
-                raise OperationError(space.w_ValueError, space.wrap(msg))
+               flags |= rfloat.DTSF_ALT
+
             tp = self._type
             self._get_locale(tp)
             if tp == "\0":
@@ -990,6 +990,7 @@
             self._unknown_presentation("float")
 
         def _format_complex(self, w_complex):
+            flags = 0
             space = self.space
             tp = self._type
             self._get_locale(tp)
@@ -1004,10 +1005,8 @@
                 msg = "Zero padding is not allowed in complex format specifier"
                 raise OperationError(space.w_ValueError, space.wrap(msg))
             if self._alternate:
-                #alternate is invalid
-                msg = "Alternate form %s not allowed in complex format specifier"
-                raise OperationError(space.w_ValueError,
-                                     space.wrap(msg % (self._alternate)))
+                flags |= rfloat.DTSF_ALT
+
             skip_re = 0
             add_parens = 0
             if tp == "\0":
@@ -1029,10 +1028,9 @@
             if self._precision == -1:
                 self._precision = default_precision
 
-            #might want to switch to double_to_string from formatd
             #in CPython it's named 're' - clashes with re module
-            re_num = formatd(w_complex.realval, tp, self._precision)
-            im_num = formatd(w_complex.imagval, tp, self._precision)
+            re_num, special = rfloat.double_to_string(w_complex.realval, tp, self._precision, flags)
+            im_num, special = rfloat.double_to_string(w_complex.imagval, tp, self._precision, flags)
             n_re_digits = len(re_num)
             n_im_digits = len(im_num)
 
diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -272,7 +272,8 @@
         cls.space = gettestobjspace(usemodules=('_locale',))
 
     def test_alternate(self):
-        raises(ValueError, format, 1.0, "#")
+        assert format(1.0, "#.0e") == "1.e+00"
+        assert format(1+1j, '#.0e') == '1.e+00+1.e+00j'
 
     def test_simple(self):
         assert format(0.0, "f") == "0.000000"


More information about the pypy-commit mailing list