[pypy-svn] r12041 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 7 00:57:12 CEST 2005


Author: arigo
Date: Sat May  7 00:57:12 2005
New Revision: 12041

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/stringtype.py
   pypy/dist/pypy/objspace/std/test/test_stringobject.py
Log:
Added missing default arguments in string methods.
Also added new Python 2.4 default arguments.
Removed the outdated docstring.



Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Sat May  7 00:57:12 2005
@@ -1,78 +1,4 @@
 # -*- Coding: Latin-1 -*-
-"""
-stringobject.py
-
-this is here:
-    to not confuse python-mode
-
-Synopsis of implemented methods (* marks work in progress)
-
-Py                PyPy
-
-                  def _is_generic(w_self, fun):
-                  def mod__String_ANY(space, w_str, w_item):def mod__String_Tuple(space, w_str, w_tuple):def mod_str_tuple(space, w_format, w_args):
-                  def ord__String(space, w_str):
-                  def string_richcompare(space, w_str1, w_str2, op):
-                  def str_w__String(space, w_str):
-__add__           def add__String_String(space, w_left, w_right):
-__class__
-__contains__
-__delattr__
-__doc__
-__eq__            def eq__String_String(space, w_str1, w_str2):
-__ge__            def ge__String_String(space, w_str1, w_str2):
-__getattribute__
-__getitem__       def getitem__String_ANY(space, w_str, w_int): def getitem__String_Slice(space, w_str, w_slice):
-__getslice__
-__gt__            def gt__String_String(space, w_str1, w_str2):
-__hash__          def hash__String(space, w_str):
-__init__
-__le__            def le__String_String(space, w_str1, w_str2):
-__len__           def len__String(space, w_str):
-__lt__            def lt__String_String(space, w_str1, w_str2):
-__mul__
-__ne__            def ne__String_String(space, w_str1, w_str2):
-__new__
-__reduce__
-__repr__          def repr__String(space, w_str):
-__rmul__
-__setattr__
-__str__           def str__String(space, w_str):
-capitalize        def str_capitalize__String(space, w_self):
-center            def str_center__String_ANY(space, w_self):
-count             def str_count__String_String_ANY_ANY(space, w_self): [optional arguments not supported now]
-decode            !Unicode not supported now
-encode            !Unicode not supported now
-endswith          str_endswith__String_String    [optional arguments not supported now]
-expandtabs        str_expandtabs__String_ANY
-find              OK
-index             OK
-isalnum           def str_isalnum__String(space, w_self): def _isalnum(ch):
-isalpha           def str_isalpha__String(space, w_self): def _isalpha(ch):
-isdigit           def str_isdigit__String(space, w_self): def _isdigit(ch):
-islower           def str_islower__String(space, w_self): def _islower(ch):
-isspace           def str_isspace__String(space, w_self): def _isspace(ch):
-istitle           def str_istitle(space, w_self):
-isupper           def str_isupper__String(space, w_self): def _isupper(ch):
-join              def str_join__String_ANY(space, w_self, w_list):
-ljust             def str_ljust__String_ANY(space, w_self, w_arg):
-lower             OK
-lstrip            def str_lstrip__String_String(space, w_self, w_chars):
-replace           OK
-rfind             OK
-rindex            OK
-rjust             def str_rjust__String_ANY(space, w_self, w_arg):
-rstrip            def str_rstrip__String_String(space, w_self, w_chars):
-split             def str_split__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):def str_split__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
-splitlines        def str_splitlines__String_String(space, w_self, w_keepends):
-startswith        str_startswith__String_String    [optional arguments not supported now]
-strip             def str_strip__String_String(space, w_self, w_chars):
-swapcase          OK
-title             def str_title__String(space, w_self):
-translate         OK
-upper             def str_upper__String(space, w_self):
-zfill             OK
-"""
 
 from pypy.objspace.std.objspace import *
 from pypy.interpreter import gateway
@@ -376,26 +302,35 @@
         return space.wrap("")
 
 
-def str_rjust__String_ANY(space, w_self, w_arg):
+def str_rjust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
 
     u_arg = space.int_w(w_arg)
     u_self = w_self._value
+    fillchar = space.str_w(w_fillchar)
+    if len(fillchar) != 1:
+        raise OperationError(space.w_TypeError,
+            space.wrap("rjust() argument 2 must be a single character"))
+
     
     d = u_arg - len(u_self)
     if d>0:
-        u_self = d * ' ' + u_self
+        u_self = d * fillchar + u_self
         
     return space.wrap(u_self)
 
 
-def str_ljust__String_ANY(space, w_self, w_arg):
+def str_ljust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
 
     u_self = w_self._value
     u_arg = space.int_w(w_arg)
+    fillchar = space.str_w(w_fillchar)
+    if len(fillchar) != 1:
+        raise OperationError(space.w_TypeError,
+            space.wrap("ljust() argument 2 must be a single character"))
 
     d = u_arg - len(u_self)
     if d>0:
-        u_self += d * ' '
+        u_self += d * fillchar
         
     return space.wrap(u_self)
 
@@ -601,14 +536,18 @@
     return _strip_none(space, w_self, left=1, right=0)
 
 
-def str_center__String_ANY(space, w_self, w_arg):
+def str_center__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
     u_self = w_self._value
     u_arg  = space.int_w(w_arg)
+    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"))
 
     d = u_arg - len(u_self) 
     if d>0:
         offset = d//2
-        u_centered = offset * ' ' + u_self + (d - offset) * ' ' 
+        u_centered = offset * fillchar + u_self + (d - offset) * fillchar
     else:
         u_centered = u_self
 
@@ -636,36 +575,28 @@
     return W_IntObject(space, count)
 
 
-#[optional arguments not supported now]    
-def str_endswith__String_String(space, w_self, w_end): 
-    u_self = w_self._value
-    u_end  = w_end._value
-    
-    found = 0
-    if u_end:
-        endlen = len(u_end)
-        if endlen <= len(u_self):
-           found = (u_end == u_self[-endlen:]) 
-    else:
-        found = 1
-        
-    return space.newbool(found)
-    
+def str_endswith__String_String_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
+    (u_self, suffix, start, end) = _convert_idx_params(space, w_self,
+                                                       w_suffix, w_start, w_end)
+    begin = end - len(suffix)
+    if begin < start:
+        return space.w_False
+    for i in range(len(suffix)):
+        if u_self[begin+i] != suffix[i]:
+            return space.w_False
+    return space.w_True
     
-def str_startswith__String_String_ANY(space, w_self, w_prefix, w_start):
-    u_self = w_self._value
-    u_prefix  = w_prefix._value
-    u_start = space.int_w(w_start)
     
-    found = 0
-    if u_prefix:
-        plen = len(u_prefix)
-        if u_start + plen <= len(u_self):
-           found = (u_prefix == u_self[u_start:u_start+plen])
-    else:
-        found = 1
-        
-    return space.newbool(found)    
+def str_startswith__String_String_ANY_ANY(space, w_self, w_prefix, w_start, w_end):
+    (u_self, prefix, start, end) = _convert_idx_params(space, w_self,
+                                                       w_prefix, w_start, w_end)
+    stop = start + len(prefix)
+    if stop > end:
+        return space.w_False
+    for i in range(len(prefix)):
+        if u_self[start+i] != prefix[i]:
+            return space.w_False
+    return space.w_True
     
     
 def _tabindent(u_token, u_tabsize):
@@ -740,8 +671,6 @@
     if len(input) >= width:
         return w_self
 
-    b = width - len(input)
-
     buf = [' '] * width
     if len(input) > 0 and (input[0] == '+' or input[0] == '-'):
         buf[0] = input[0]
@@ -981,8 +910,6 @@
 
    
 app = gateway.applevel(r'''
-    import codecs
-    
     def str_translate__String_ANY_ANY(s, table, deletechars=''):
         """charfilter - unicode handling is not implemented
         
@@ -1017,6 +944,7 @@
         return repr
 
     def str_decode__String_ANY_ANY(str, encoding=None, errors=None):
+        import codecs
         if encoding is None and errors is None:
             return unicode(str)
         elif errors is None:

Modified: pypy/dist/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringtype.py	(original)
+++ pypy/dist/pypy/objspace/std/stringtype.py	Sat May  7 00:57:12 2005
@@ -12,8 +12,8 @@
 str_islower    = MultiMethod('islower', 1)
 str_istitle    = MultiMethod('istitle', 1)
 str_isalnum    = MultiMethod('isalnum', 1)
-str_ljust      = MultiMethod('ljust', 2)
-str_rjust      = MultiMethod('rjust', 2)
+str_ljust      = MultiMethod('ljust', 3, defaults=(' ',))
+str_rjust      = MultiMethod('rjust', 3, defaults=(' ',))
 str_upper      = MultiMethod('upper', 1)
 str_lower      = MultiMethod('lower', 1)
 str_swapcase   = MultiMethod('swapcase', 1)
@@ -28,12 +28,12 @@
 str_strip      = MultiMethod('strip',  2, defaults=(None,))
 str_rstrip     = MultiMethod('rstrip', 2, defaults=(None,))
 str_lstrip     = MultiMethod('lstrip', 2, defaults=(None,))
-str_center     = MultiMethod('center', 2, )
+str_center     = MultiMethod('center', 3, defaults=(' ',))
 str_count      = MultiMethod('count', 4, defaults=(0, maxint))      
-str_endswith   = MultiMethod('endswith', 2)   #[optional arguments not supported now]
+str_endswith   = MultiMethod('endswith', 4, defaults=(0, maxint))
 str_expandtabs = MultiMethod('expandtabs', 2, defaults=(8,))
 str_splitlines = MultiMethod('splitlines', 2, defaults=(0,))
-str_startswith = MultiMethod('startswith', 3, defaults=(0,))
+str_startswith = MultiMethod('startswith', 4, defaults=(0, maxint))
 str_translate  = MultiMethod('translate', 3, defaults=('',)) #unicode mimic not supported now
 str_decode     = MultiMethod('decode', 3, defaults=(None, None))
 

Modified: pypy/dist/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stringobject.py	Sat May  7 00:57:12 2005
@@ -181,6 +181,8 @@
         assert 'abc'.rjust(6) == '   abc'
         assert 'abc'.rjust(3) == 'abc'
         assert 'abc'.rjust(2) == 'abc'
+        assert 'abc'.rjust(5, '*') == '**abc'     # Python 2.4
+        raises(TypeError, 'abc'.rjust, 5, 'xx')
 
     def test_ljust(self):
         s = "abc"
@@ -192,6 +194,8 @@
         assert 'abc'.ljust(6) == 'abc   '
         assert 'abc'.ljust(3) == 'abc'
         assert 'abc'.ljust(2) == 'abc'
+        assert 'abc'.ljust(5, '*') == 'abc**'     # Python 2.4
+        raises(TypeError, 'abc'.ljust, 6, '')
 
     def test_replace(self):
         assert 'one!two!three!'.replace('!', '@', 1) == 'one at two!three!'
@@ -253,6 +257,8 @@
         assert 'abc'.center(6) == ' abc  '
         assert 'abc'.center(3) == 'abc'
         assert 'abc'.center(2) == 'abc'
+        assert 'abc'.center(5, '*') == '*abc*'     # Python 2.4
+        raises(TypeError, 'abc'.center, 4, 'cba')
 
         
     def test_count(self):
@@ -282,6 +288,8 @@
         assert 'ab'.startswith('a', 0) is True
         assert 'ab'.startswith('a', 1) is False
         assert 'ab'.startswith('b', 1) is True
+        assert 'abc'.startswith('bc', 1, 2) is False
+        assert 'abc'.startswith('c', -1, 4) is True
 
     def test_endswith(self):
         assert 'ab'.endswith('ab') is True
@@ -293,6 +301,12 @@
         assert ''.endswith('a') is False
         assert 'x'.endswith('xx') is False
         assert 'y'.endswith('xx') is False
+
+    def test_endswith_more(self):
+        assert 'abc'.endswith('ab', 0, 2) is True
+        assert 'abc'.endswith('bc', 1) is True
+        assert 'abc'.endswith('bc', 2) is False
+        assert 'abc'.endswith('b', -3, -1) is True
       
     def test_expandtabs(self):
         assert 'abc\rab\tdef\ng\thi'.expandtabs() ==    'abc\rab      def\ng       hi'



More information about the Pypy-commit mailing list