[pypy-svn] r14257 - in pypy/branch/dist-2.4.1/pypy/objspace/std: . test

ac at codespeak.net ac at codespeak.net
Mon Jul 4 22:13:59 CEST 2005


Author: ac
Date: Mon Jul  4 22:13:59 2005
New Revision: 14257

Modified:
   pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py
   pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_unicodeobject.py
   pypy/branch/dist-2.4.1/pypy/objspace/std/unicodeobject.py
   pypy/branch/dist-2.4.1/pypy/objspace/std/unicodetype.py
Log:
Add a fullchar argument to unicode.[lr]just



Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py	Mon Jul  4 22:13:59 2005
@@ -658,12 +658,6 @@
 
     return W_StringObject(space, u_centered)
 
-# This is so that str_center__String_ANY_Unicode in unicodeobject
-# won't convert a String fillchar
-def str_center__String_ANY_String(space, w_self, w_arg, w_fillchar):
-    return str_center__String_ANY_ANY(space, w_self, w_arg, w_fillchar)
-      
-      
 def str_count__String_String_ANY_ANY(space, w_self, w_arg, w_start, w_end): 
     u_self  = w_self._value
     u_arg   = w_arg._value

Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_unicodeobject.py	Mon Jul  4 22:13:59 2005
@@ -128,11 +128,41 @@
         assert u'abc'.center(6) == u' abc  '
         assert u'abc'.center(3) == u'abc'
         assert u'abc'.center(2) == u'abc'
-        assert u'abc'.center(5, u'*') == u'*abc*'     # Python 2.4
+        assert u'abc'.center(5, u'*') == u'*abc*'    # Python 2.4
         assert u'abc'.center(5, '*') == u'*abc*'     # Python 2.4
-        assert 'abc'.center(5, u'*') == u'*abc*'     # Python 2.4
+        raises(TypeError, 'abc'.center, 5, u'*')     # Python 2.4
         raises(TypeError, u'abc'.center, 4, u'cba')
 
+    def test_rjust(self):
+        s = u"abc"
+        assert s.rjust(2) == s
+        assert s.rjust(3) == s
+        assert s.rjust(4) == u" " + s
+        assert s.rjust(5) == u"  " + s
+        assert u'abc'.rjust(10) == u'       abc'
+        assert u'abc'.rjust(6) == u'   abc'
+        assert u'abc'.rjust(3) == u'abc'
+        assert u'abc'.rjust(2) == u'abc'
+        assert u'abc'.rjust(5, u'*') == u'**abc'    # Python 2.4
+        assert u'abc'.rjust(5, '*') == u'**abc'     # Python 2.4
+        raises(TypeError, 'abc'.rjust, 5, u'*')     # Python 2.4
+        raises(TypeError, u'abc'.rjust, 5, u'xx')
+
+    def test_ljust(self):
+        s = u"abc"
+        assert s.ljust(2) == s
+        assert s.ljust(3) == s
+        assert s.ljust(4) == s + u" "
+        assert s.ljust(5) == s + u"  "
+        assert u'abc'.ljust(10) == u'abc       '
+        assert u'abc'.ljust(6) == u'abc   '
+        assert u'abc'.ljust(3) == u'abc'
+        assert u'abc'.ljust(2) == u'abc'
+        assert u'abc'.ljust(5, u'*') == u'abc**'    # Python 2.4
+        assert u'abc'.ljust(5, '*') == u'abc**'     # Python 2.4
+        raises(TypeError, 'abc'.ljust, 5, u'*')     # Python 2.4
+        raises(TypeError, u'abc'.ljust, 6, u'')
+
     def test_long_from_unicode(self):
         assert long(u'12345678901234567890') == 12345678901234567890
         assert int(u'12345678901234567890') == 12345678901234567890

Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/unicodeobject.py	Mon Jul  4 22:13:59 2005
@@ -62,7 +62,7 @@
     return space.str_w(space.str(w_uni))
 
 def repr__Unicode(space, w_uni):
-    return space.wrap(repr(u''.join(w_uni._value)))
+    return space.wrap(repr(u''.join(w_uni._value))) # XXX: We don't support unicodestrings and certainly not repr on them.
 
 def str__Unicode(space, w_uni):
     return space.call_method(w_uni, 'encode')
@@ -154,6 +154,10 @@
     totlen = 0
     if len(list) == 0:
         return W_UnicodeObject(space, [])
+    if (len(list) == 1 and
+        space.is_true(space.is_(space.type(list[0]), space.w_unicode))):
+        return list[0]
+    
     values_list = [None] * len(list)
     values_list[0] = [u'\0']
     for i in range(len(list)):
@@ -505,23 +509,21 @@
             return space.w_False
     return space.w_True
 
+def _to_unichar_w(space, w_char):
+    try:
+        w_unichar = unicodetype.unicode_from_object(space, w_char)
+    except OperationError:
+        raise OperationError(space.w_TypeError, space.wrap('The fill character cannot be converted to Unicode'))
+
+    if space.int_w(space.len(w_unichar)) != 1:
+        raise OperationError(space.w_TypeError, space.wrap('The fill character must be exactly one character long'))
+    unichar = unichr(space.int_w(space.ord(w_unichar)))
+    return unichar
+
 def unicode_center__Unicode_ANY_ANY(space, w_self, w_width, w_fillchar):
     self = w_self._value
     width = space.int_w(w_width)
-
-    if space.is_true(space.isinstance(w_fillchar, space.w_str)):
-        fillchar = space.str_w(w_fillchar)
-        if len(fillchar) != 1:
-            raise OperationError(
-                space.w_TypeError,
-                space.wrap("center() argument 2 must be a single character"))
-    elif space.is_true(space.isinstance(w_fillchar, space.w_unicode)):
-        if len(w_fillchar._value) != 1:
-            raise OperationError(
-                space.w_TypeError,
-                space.wrap("center() argument 2 must be a single character"))
-        fillchar = w_fillchar._value[0]
-
+    fillchar = _to_unichar_w(space, w_fillchar)
     padding = width - len(self)
     if padding < 0:
         return space.call_function(space.w_unicode, w_self)
@@ -531,24 +533,26 @@
         result[leftpad + i] = self[i]
     return W_UnicodeObject(space, result)
 
-def unicode_ljust__Unicode_ANY(space, w_self, w_width):
+def unicode_ljust__Unicode_ANY_ANY(space, w_self, w_width, w_fillchar):
     self = w_self._value
     width = space.int_w(w_width)
+    fillchar = _to_unichar_w(space, w_fillchar)
     padding = width - len(self)
     if padding < 0:
         return space.call_function(space.w_unicode, w_self)
-    result = [u' '] * width
+    result = [fillchar] * width
     for i in range(len(self)):
         result[i] = self[i]
     return W_UnicodeObject(space, result)
 
-def unicode_rjust__Unicode_ANY(space, w_self, w_width):
+def unicode_rjust__Unicode_ANY_ANY(space, w_self, w_width, w_fillchar):
     self = w_self._value
     width = space.int_w(w_width)
+    fillchar = _to_unichar_w(space, w_fillchar)
     padding = width - len(self)
     if padding < 0:
         return space.call_function(space.w_unicode, w_self)
-    result = [u' '] * width
+    result = [fillchar] * width
     for i in range(len(self)):
         result[padding + i] = self[i]
     return W_UnicodeObject(space, result)
@@ -794,7 +798,6 @@
     return space.call_method(w_new, 'join', w_parts)
     
 
-'translate'
 app = gateway.applevel(r'''
 import sys
 
@@ -883,7 +886,6 @@
     def str_lstrip__String_Unicode(space, w_self, w_chars):
         return space.call_method(space.call_function(space.w_unicode, w_self),
                                  'lstrip', w_chars)
-        self = w_self._value
     def str_rstrip__String_Unicode(space, w_self, w_chars):
         return space.call_method(space.call_function(space.w_unicode, w_self),
                                  'rstrip', w_chars)
@@ -915,9 +917,4 @@
         return space.call_method(space.call_function(space.w_unicode, w_self),
                                  'rsplit', w_delim, w_maxsplit)
 
-    def str_center__String_ANY_Unicode(space, w_self, w_width, w_fillchar):
-        print 'Centering a string with unicode.'
-        return space.call_method(space.call_function(space.w_unicode, w_self),
-                                 'center', w_width, w_fillchar)
-
     register_all(vars(), stringtype)

Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/unicodetype.py	Mon Jul  4 22:13:59 2005
@@ -22,13 +22,13 @@
 unicode_istitle    = MultiMethod('istitle', 1)
 unicode_isupper    = MultiMethod('isupper', 1)
 unicode_join       = MultiMethod('join', 2)
-unicode_ljust      = MultiMethod('ljust', 2)
+unicode_ljust      = MultiMethod('ljust', 3, defaults=(' ',))
 unicode_lower      = MultiMethod('lower', 1)
 unicode_lstrip     = MultiMethod('lstrip', 2, defaults=(None,))
 unicode_replace    = MultiMethod('replace', 4, defaults=(-1,))
 unicode_rfind      = MultiMethod('rfind', 4, defaults=(0, maxint))
 unicode_rindex     = MultiMethod('rindex', 4, defaults=(0, maxint))
-unicode_rjust      = MultiMethod('rjust', 2)
+unicode_rjust      = MultiMethod('rjust', 3, defaults=(' ',))
 unicode_rstrip     = MultiMethod('rstrip', 2, defaults=(None,))
 unicode_rsplit     = MultiMethod('rsplit', 3, defaults=(None,-1))
 unicode_split      = MultiMethod('split', 3, defaults=(None,-1))



More information about the Pypy-commit mailing list