[pypy-svn] r78822 - in pypy/branch/fast-forward/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 16:48:00 CET 2010


Author: arigo
Date: Sun Nov  7 16:47:59 2010
New Revision: 78822

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/stringobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_stringobject.py
Log:
str.translate(None, 'delete-chars')


Modified: pypy/branch/fast-forward/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/stringobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/stringobject.py	Sun Nov  7 16:47:59 2010
@@ -919,7 +919,9 @@
 
     return space.wrap(buf.build())
 
-   
+
+DEFAULT_NOOP_TABLE = ''.join([chr(i) for i in range(256)])
+
 def str_translate__String_ANY_ANY(space, w_string, w_table, w_deletechars=''):
     """charfilter - unicode handling is not implemented
     
@@ -929,11 +931,14 @@
     which must be a string of length 256"""
 
     # XXX CPython accepts buffers, too, not sure what we should do
-    table = space.str_w(w_table)
-    if len(table) != 256:
-        raise OperationError(
-            space.w_ValueError,
-            space.wrap("translation table must be 256 characters long"))
+    if space.is_w(w_table, space.w_None):
+        table = DEFAULT_NOOP_TABLE
+    else:
+        table = space.str_w(w_table)
+        if len(table) != 256:
+            raise OperationError(
+                space.w_ValueError,
+                space.wrap("translation table must be 256 characters long"))
 
     string = w_string._value
     chars = []

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_stringobject.py	Sun Nov  7 16:47:59 2010
@@ -594,6 +594,8 @@
         raises(ValueError, 'xyz'.translate, 'too short')
         raises(ValueError, 'xyz'.translate, 'too long'*33)
 
+        assert 'yz' == 'xyz'.translate(None, 'x')     # 2.6
+
     def test_iter(self):
         l=[]
         for i in iter("42"):



More information about the Pypy-commit mailing list