[pypy-commit] pypy py3k: builtins.ascii

pjenvey noreply at buildbot.pypy.org
Thu Nov 10 03:00:02 CET 2011


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r49072:e0b22cef21b6
Date: 2011-11-09 17:53 -0800
http://bitbucket.org/pypy/pypy/changeset/e0b22cef21b6/

Log:	builtins.ascii

diff --git a/pypy/module/__builtin__/__init__.py b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -50,6 +50,7 @@
 
         # interp-level function definitions
         'abs'           : 'operation.abs',
+        'ascii'         : 'operation.ascii',
         'chr'           : 'operation.chr',
         'len'           : 'operation.len',
         'ord'           : 'operation.ord',
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
@@ -15,6 +15,17 @@
     "abs(number) -> number\n\nReturn the absolute value of the argument."
     return space.abs(w_val)
 
+def ascii(space, w_obj):
+    """"ascii(object) -> string
+
+    As repr(), return a string containing a printable representation of an
+    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."""
+    # repr is guaranteed to be unicode
+    repr = space.unwrap(space.repr(w_obj))
+    return space.wrap(repr.encode('ascii', 'backslashreplace').decode('ascii'))
+
 @unwrap_spec(code=int)
 def chr(space, code):
     "Return a Unicode string of one character with the given ordinal."
diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -1,3 +1,4 @@
+# coding: utf-8
 import autopath
 import sys
 from pypy import conftest
@@ -37,6 +38,41 @@
         raises(ImportError, __import__, 'spamspam')
         raises(TypeError, __import__, 1, 2, 3, 4)
 
+    def test_ascii(self):
+        assert ascii('') == '\'\''
+        assert ascii(0) == '0'
+        assert ascii(()) == '()'
+        assert ascii([]) == '[]'
+        assert ascii({}) == '{}'
+        a = []
+        a.append(a)
+        assert ascii(a) == '[[...]]'
+        a = {}
+        a[0] = a
+        assert ascii(a) == '{0: {...}}'
+        # Advanced checks for unicode strings
+        def _check_uni(s):
+            assert ascii(s) == repr(s)
+        _check_uni("'")
+        _check_uni('"')
+        _check_uni('"\'')
+        _check_uni('\0')
+        _check_uni('\r\n\t .')
+        # Unprintable non-ASCII characters
+        _check_uni('\x85')
+        _check_uni('\u1fff')
+        _check_uni('\U00012fff')
+        # Lone surrogates
+        _check_uni('\ud800')
+        _check_uni('\udfff')
+        # Issue #9804: surrogates should be joined even for printable
+        # wide characters (UCS-2 builds).
+        assert ascii('\U0001d121') == "'\\U0001d121'"
+        # All together
+        s = "'\0\"\n\r\t abcd\x85&#233;\U00012fff\uD800\U0001D121xxx."
+        assert ascii(s) == \
+            r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'"""
+
     def test_bin(self):
         assert bin(0) == "0b0"
         assert bin(-1) == "-0b1"


More information about the pypy-commit mailing list