[pypy-commit] pypy py3k: Issue1262 in-progress: add unicode.isprintable().

amauryfa noreply at buildbot.pypy.org
Mon Sep 24 23:46:57 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r57529:19c10384d3e7
Date: 2012-09-23 17:21 +0200
http://bitbucket.org/pypy/pypy/changeset/19c10384d3e7/

Log:	Issue1262 in-progress: add unicode.isprintable(). One test stil
	fails, until we use Unicode version 6.0. Patch by arielby.

diff --git a/pypy/objspace/std/test/test_unicodeobject.py b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -194,6 +194,21 @@
         assert ('\u019b\u1d00\u1d86\u0221\u1fb7'.capitalize() ==
                 '\u019b\u1d00\u1d86\u0221\u1fb7')
 
+    def test_isprintable(self):
+        assert "".isprintable()
+        assert " ".isprintable()
+        assert "abcdefg".isprintable()
+        assert not "abcdefg\n".isprintable()
+        # some defined Unicode character
+        assert "\u0374".isprintable()
+        # undefined character
+        assert not "\u0378".isprintable()
+        # single surrogate character
+        assert not "\ud800".isprintable()
+
+        assert '\U0001F46F'.isprintable()  # Since unicode 6.0
+        assert not '\U000E0020'.isprintable()
+
     def test_rjust(self):
         s = "abc"
         assert s.rjust(2) == s
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -315,6 +315,12 @@
             return space.w_False
     return space.w_True
 
+def unicode_isprintable__Unicode(space, w_unicode):
+    for uchar in w_unicode._value:
+        if not unicodedb.isprintable(ord(uchar)):
+            return space.w_False
+    return space.w_True
+
 def _strip(space, w_self, w_chars, left, right):
     "internal function called by str_xstrip methods"
     u_self = w_self._value
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
@@ -111,6 +111,10 @@
                          doc='S.isidentifier() -> bool\n\nReturn True if S is'
                              ' a valid identifier according\nto the language'
                              ' definition.')
+unicode_isprintable  = SMM('isprintable', 1,
+                           doc='S.isprintable() -> bool\n\nReturn True if all'
+                               ' characters in S are considered printable in'
+                               ' repr or S is empty, False otherwise')
 unicode_join       = SMM('join', 2,
                          doc='S.join(sequence) -> unicode\n\nReturn a string'
                              ' which is the concatenation of the strings in'


More information about the pypy-commit mailing list