[pypy-commit] pypy py3k: In some functions an int was accepted in place of a bytes string
amauryfa
noreply at buildbot.pypy.org
Sun Dec 2 00:17:03 CET 2012
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r59203:b201dd344465
Date: 2012-12-02 00:16 +0100
http://bitbucket.org/pypy/pypy/changeset/b201dd344465/
Log: In some functions an int was accepted in place of a bytes string
(and processed like 5 -> b'\0\0\0\0\0')
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -13,7 +13,7 @@
str_expandtabs, str_ljust, str_rjust, str_center, str_zfill,
str_join, str_split, str_rsplit, str_partition, str_rpartition,
str_splitlines, str_translate)
-from pypy.objspace.std.stringtype import descr_maketrans, makebytesdata_w
+from pypy.objspace.std.stringtype import descr_maketrans, newbytesdata_w
from pypy.objspace.std.listtype import (
list_append, list_extend)
@@ -65,7 +65,7 @@
def descr__init__(space, w_self, w_source=None, encoding=None, errors=None):
from pypy.objspace.std.bytearrayobject import W_BytearrayObject
assert isinstance(w_self, W_BytearrayObject)
- data = makebytesdata_w(space, w_source, encoding, errors)
+ data = newbytesdata_w(space, w_source, encoding, errors)
w_self.data = data
diff --git a/pypy/objspace/std/stringtype.py b/pypy/objspace/std/stringtype.py
--- a/pypy/objspace/std/stringtype.py
+++ b/pypy/objspace/std/stringtype.py
@@ -271,7 +271,7 @@
"byte must be in range(0, 256)"))
return chr(value)
-def makebytesdata_w(space, w_source, encoding=None, errors=None):
+def newbytesdata_w(space, w_source, encoding, errors):
# None value
if w_source is None:
if encoding is not None or errors is not None:
@@ -301,6 +301,10 @@
w_source = encode_object(space, w_source, encoding, errors)
# and continue with the encoded string
+ return makebytesdata_w(space, w_source)
+
+def makebytesdata_w(space, w_source):
+
# String-like argument
try:
string = space.bufferstr_new_w(w_source)
@@ -335,7 +339,7 @@
if (w_source and space.is_w(space.type(w_source), space.w_bytes) and
space.is_w(w_stringtype, space.w_bytes)):
return w_source
- value = ''.join(makebytesdata_w(space, w_source, encoding, errors))
+ value = ''.join(newbytesdata_w(space, w_source, encoding, errors))
if space.config.objspace.std.withrope:
from pypy.objspace.std.ropeobject import rope, W_RopeObject
w_obj = space.allocate_instance(W_RopeObject, w_stringtype)
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -370,6 +370,7 @@
b.extend(c for c in b'hello')
assert b == bytearray(b'worldhello')
+ raises(TypeError, b.extend, 3)
raises(TypeError, b.extend, [b'fish'])
raises(ValueError, b.extend, [256])
raises(TypeError, b.extend, object())
diff --git a/pypy/objspace/std/test/test_stringobject.py b/pypy/objspace/std/test/test_stringobject.py
--- a/pypy/objspace/std/test/test_stringobject.py
+++ b/pypy/objspace/std/test/test_stringobject.py
@@ -395,13 +395,13 @@
assert b'x\t\t'.expandtabs(-1) == b'x'
assert b'x\t\t'.expandtabs(0) == b'x'
- raises(OverflowError, b"t\tt\t".expandtabs, sys.maxint)
+ raises(OverflowError, b"t\tt\t".expandtabs, sys.maxsize)
def test_expandtabs_overflows_gracefully(self):
import sys
- if sys.maxint > (1 << 32):
+ if sys.maxsize > (1 << 32):
skip("Wrong platform")
- raises((MemoryError, OverflowError), b't\tt\t'.expandtabs, sys.maxint)
+ raises((MemoryError, OverflowError), b't\tt\t'.expandtabs, sys.maxsize)
def test_splitlines(self):
s = b""
@@ -436,12 +436,12 @@
raises(TypeError, b'abcdef'.find, b'd', 1.0)
def test_index(self):
- from sys import maxint
+ from sys import maxsize
assert b'abcdefghiabc'.index(b'') == 0
assert b'abcdefghiabc'.index(b'def') == 3
assert b'abcdefghiabc'.index(b'abc') == 0
assert b'abcdefghiabc'.index(b'abc', 1) == 9
- assert b'abcdefghiabc'.index(b'def', -4*maxint, 4*maxint) == 3
+ assert b'abcdefghiabc'.index(b'def', -4*maxsize, 4*maxsize) == 3
assert b'abcdefgh'.index(b'def', 2, None) == 3
assert b'abcdefgh'.index(b'def', None, None) == 3
raises(ValueError, b'abcdefghiabc'.index, b'hib')
@@ -462,12 +462,12 @@
assert b'abcdefgh'.rfind(b'def', 2, None) == 3
def test_rindex(self):
- from sys import maxint
+ from sys import maxsize
assert b'abcdefghiabc'.rindex(b'') == 12
assert b'abcdefghiabc'.rindex(b'def') == 3
assert b'abcdefghiabc'.rindex(b'abc') == 9
assert b'abcdefghiabc'.rindex(b'abc', 0, -1) == 0
- assert b'abcdefghiabc'.rindex(b'abc', -4*maxint, 4*maxint) == 9
+ assert b'abcdefghiabc'.rindex(b'abc', -4*maxsize, 4*maxsize) == 9
raises(ValueError, b'abcdefghiabc'.rindex, b'hib')
raises(ValueError, b'defghiabc'.rindex, b'def', 1)
raises(ValueError, b'defghiabc'.rindex, b'abc', 0, -1)
@@ -762,6 +762,7 @@
def test_maketrans(self):
table = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
assert bytes.maketrans(b'abc', b'xyz') == table
+ raises(TypeError, bytes.maketrans, 5, 5)
def test_compatibility(self):
#a whole bunch of methods should accept bytearray/memoryview without complaining...
More information about the pypy-commit
mailing list