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

ignas at codespeak.net ignas at codespeak.net
Sat Jul 2 14:34:16 CEST 2005


Author: ignas
Date: Sat Jul  2 14:34:15 2005
New Revision: 14091

Modified:
   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:
Make center on unicode string behave in the smae manner as center on siple strings does.


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	Sat Jul  2 14:34:15 2005
@@ -112,6 +112,27 @@
         raises(ValueError, u'abc'.rsplit, '')
         raises(ValueError, 'abc'.rsplit, u'')
 
+    def test_center(self):
+        s=u"a b"
+        assert s.center(0) == u"a b"
+        assert s.center(1) == u"a b"
+        assert s.center(2) == u"a b"
+        assert s.center(3) == u"a b"
+        assert s.center(4) == u"a b "
+        assert s.center(5) == u" a b "
+        assert s.center(6) == u" a b  "
+        assert s.center(7) == u"  a b  "
+        assert s.center(8) == u"  a b   "
+        assert s.center(9) == u"   a b   "
+        assert u'abc'.center(10) == u'   abc    '
+        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'*abc*'     # Python 2.4
+        assert 'abc'.center(5, u'*') == u'*abc*'     # Python 2.4
+        raises(TypeError, u'abc'.center, 4, u'cba')
+
     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	Sat Jul  2 14:34:15 2005
@@ -505,19 +505,32 @@
             return space.w_False
     return space.w_True
 
-def unicode_center__Unicode_ANY(space, w_self, w_width):
+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]
+
     padding = width - len(self)
     if padding < 0:
         return space.call_function(space.w_unicode, w_self)
     leftpad = padding // 2 + (padding & width & 1)
-    result = [u' '] * width
+    result = [fillchar] * width
     for i in range(len(self)):
         result[leftpad + i] = self[i]
     return W_UnicodeObject(space, result)
 
-
 def unicode_ljust__Unicode_ANY(space, w_self, w_width):
     self = w_self._value
     width = space.int_w(w_width)
@@ -902,4 +915,8 @@
         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):
+        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	Sat Jul  2 14:34:15 2005
@@ -5,7 +5,7 @@
 from sys import maxint
 
 unicode_capitalize = MultiMethod('capitalize', 1)
-unicode_center     = MultiMethod('center', 2, )
+unicode_center     = MultiMethod('center', 3, defaults=(' ',))
 unicode_count      = MultiMethod('count', 4, defaults=(0, maxint))      
 unicode_encode     = MultiMethod('encode', 3, defaults=(None, None))
 unicode_endswith   = MultiMethod('endswith', 4, defaults=(0,maxint))



More information about the Pypy-commit mailing list