[pypy-commit] pypy py3k: Implement {:a} format spec for str.format().

amauryfa noreply at buildbot.pypy.org
Mon Oct 22 08:42:23 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r58354:4e789b0f78b2
Date: 2012-10-22 08:41 +0200
http://bitbucket.org/pypy/pypy/changeset/4e789b0f78b2/

Log:	Implement {:a} format spec for str.format().

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
@@ -7,6 +7,7 @@
 from pypy.rlib.objectmodel import specialize
 from pypy.rlib.rfloat import copysign, formatd
 from pypy.tool import sourcetools
+from pypy.objspace.std.unicodetype import ascii_from_object
 
 
 @specialize.argtype(1)
@@ -312,6 +313,8 @@
                 if self.is_unicode:
                     return space.call_function(space.w_unicode, w_obj)
                 return space.str(w_obj)
+            elif conv == "a":
+                return ascii_from_object(space, w_obj)
             else:
                 raise OperationError(self.space.w_ValueError,
                                      self.space.wrap("invalid conversion"))
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
@@ -80,6 +80,8 @@
         assert self.s('{0!s:15s}').format('Hello') == 'Hello          '
         assert self.s('{0!r}').format('Hello') == "'Hello'"
         assert self.s('{0!r:}').format('Hello') == "'Hello'"
+        assert self.s('{0!r}').format('Caf\xe9') == "'Caf\xe9'"
+        assert self.s('{0!a}').format('Caf\xe9') == "'Caf\\xe9'"
 
     def test_invalid_conversion(self):
         raises(ValueError, self.s("{!x}").format, 3)


More information about the pypy-commit mailing list