[pypy-commit] pypy py3k: Implement the %a format code.

amauryfa noreply at buildbot.pypy.org
Mon Oct 22 00:34:13 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r58341:82b2f6045665
Date: 2012-10-21 10:17 +0200
http://bitbucket.org/pypy/pypy/changeset/82b2f6045665/

Log:	Implement the %a format code.

diff --git a/pypy/module/__builtin__/operation.py b/pypy/module/__builtin__/operation.py
--- a/pypy/module/__builtin__/operation.py
+++ b/pypy/module/__builtin__/operation.py
@@ -19,11 +19,8 @@
     object, but escape the non-ASCII characters in the string returned by
     repr() using \\x, \\u or \\U escapes.  This generates a string similar
     to that returned by repr() in Python 2."""
-    from pypy.objspace.std.unicodetype import decode_object, encode_object
-    # repr is guaranteed to be unicode
-    w_repr = space.repr(w_obj)
-    w_encoded = encode_object(space, w_repr, 'ascii', 'backslashreplace')
-    return decode_object(space, w_encoded, 'ascii', None)
+    from pypy.objspace.std.unicodetype import ascii_from_object
+    return ascii_from_object(space, w_obj)
 
 @unwrap_spec(code=int)
 def chr(space, code):
diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -2,7 +2,8 @@
 String formatting routines.
 """
 from pypy.interpreter.error import OperationError
-from pypy.objspace.std.unicodetype import unicode_from_object
+from pypy.objspace.std.unicodetype import (
+    unicode_from_object, ascii_from_object)
 from pypy.rlib import jit
 from pypy.rlib.rarithmetic import ovfcheck
 from pypy.rlib.rfloat import formatd, DTSF_ALT, isnan, isinf
@@ -449,6 +450,10 @@
         def fmt_r(self, w_value):
             self.std_wp(self.space.unicode_w(self.space.repr(w_value)))
 
+        def fmt_a(self, w_value):
+            w_value = ascii_from_object(self.space, w_value)
+            self.std_wp(self.space.unicode_w(w_value))
+
         def fmt_c(self, w_value):
             self.prec = -1     # just because
             space = self.space
diff --git a/pypy/objspace/std/test/test_stringformat.py b/pypy/objspace/std/test/test_stringformat.py
--- a/pypy/objspace/std/test/test_stringformat.py
+++ b/pypy/objspace/std/test/test_stringformat.py
@@ -281,3 +281,9 @@
     def test_invalid_char(self):
         f = 4
         raises(ValueError, '"%\u1234" % (f,)')
+
+    def test_ascii(self):
+        assert "<%a>" % "test" == "<'test'>"
+        assert "<%a>" % "\t\x80" == "<'\\t\\x80'>"
+        assert "<%r>" % "\xe9" == "<'\xe9'>"
+        assert "<%a>" % "\xe9" == "<'\\xe9'>"
diff --git a/pypy/objspace/std/unicodetype.py b/pypy/objspace/std/unicodetype.py
--- a/pypy/objspace/std/unicodetype.py
+++ b/pypy/objspace/std/unicodetype.py
@@ -265,6 +265,14 @@
     w_unicode_method = space.lookup(w_obj, "__str__")
     return space.repr(w_obj) if w_unicode_method is None else space.str(w_obj)
 
+def ascii_from_object(space, w_obj):
+    """Implements builtins.ascii()"""
+    # repr is guaranteed to be unicode
+    w_repr = space.repr(w_obj)
+    w_encoded = encode_object(space, w_repr, 'ascii', 'backslashreplace')
+    return decode_object(space, w_encoded, 'ascii', None)
+
+
 @unwrap_spec(w_object = WrappedDefault(u''))
 def descr_new_(space, w_unicodetype, w_object=None, w_encoding=None,
                w_errors=None):


More information about the pypy-commit mailing list