Author: Maciej Fijalkowski <fij...@gmail.com> Branch: Changeset: r58567:ff663b39bd39 Date: 2012-10-29 11:20 +0100 http://bitbucket.org/pypy/pypy/changeset/ff663b39bd39/
Log: merge diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py --- a/pypy/interpreter/buffer.py +++ b/pypy/interpreter/buffer.py @@ -74,8 +74,11 @@ elif step == 1: length = stop - start if length != len(newstring): - msg = "buffer slice assignment is wrong size" - raise OperationError(space.w_ValueError, space.wrap(msg)) + if length < 0 and len(newstring) == 0: + pass # ok anyway + else: + msg = "right operand length must match slice length" + raise OperationError(space.w_ValueError, space.wrap(msg)) self.setslice(start, newstring) else: raise OperationError(space.w_ValueError, diff --git a/pypy/jit/metainterp/optimizeopt/rewrite.py b/pypy/jit/metainterp/optimizeopt/rewrite.py --- a/pypy/jit/metainterp/optimizeopt/rewrite.py +++ b/pypy/jit/metainterp/optimizeopt/rewrite.py @@ -429,15 +429,17 @@ source_start_box = self.get_constant_box(op.getarg(3)) dest_start_box = self.get_constant_box(op.getarg(4)) length = self.get_constant_box(op.getarg(5)) + extrainfo = op.getdescr().get_extra_info() if (source_start_box and dest_start_box and length and (dest_value.is_virtual() or length.getint() <= 8) and - (source_value.is_virtual() or length.getint() <= 8)): + (source_value.is_virtual() or length.getint() <= 8) and + len(extrainfo.write_descrs_arrays) == 1): # <-sanity check from pypy.jit.metainterp.optimizeopt.virtualize import VArrayValue source_start = source_start_box.getint() dest_start = dest_start_box.getint() + # XXX fish fish fish + arraydescr = extrainfo.write_descrs_arrays[0] for index in range(length.getint()): - # XXX fish fish fish - arraydescr = op.getdescr().get_extra_info().write_descrs_arrays[0] if source_value.is_virtual(): assert isinstance(source_value, VArrayValue) val = source_value.getitem(index + source_start) diff --git a/pypy/module/__builtin__/test/test_buffer.py b/pypy/module/__builtin__/test/test_buffer.py --- a/pypy/module/__builtin__/test/test_buffer.py +++ b/pypy/module/__builtin__/test/test_buffer.py @@ -117,6 +117,8 @@ b[:] = '12345' assert a.tostring() == 'hello 12345' raises(IndexError, 'b[5] = "."') + b[4:2] = '' + assert a.tostring() == 'hello 12345' b = buffer(b, 2) assert len(b) == 3 diff --git a/pypy/module/_cffi_backend/cbuffer.py b/pypy/module/_cffi_backend/cbuffer.py --- a/pypy/module/_cffi_backend/cbuffer.py +++ b/pypy/module/_cffi_backend/cbuffer.py @@ -1,6 +1,7 @@ from pypy.interpreter.error import operationerrfmt from pypy.interpreter.buffer import RWBuffer -from pypy.interpreter.gateway import unwrap_spec +from pypy.interpreter.gateway import unwrap_spec, interp2app +from pypy.interpreter.typedef import TypeDef from pypy.rpython.lltypesystem import rffi from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray @@ -34,6 +35,16 @@ for i in range(len(string)): raw_cdata[i] = string[i] +LLBuffer.typedef = TypeDef( + "buffer", + __module__ = "_cffi_backend", + __len__ = interp2app(RWBuffer.descr_len), + __getitem__ = interp2app(RWBuffer.descr_getitem), + __setitem__ = interp2app(RWBuffer.descr_setitem), + __buffer__ = interp2app(RWBuffer.descr__buffer__), + ) +LLBuffer.typedef.acceptable_as_base_class = False + @unwrap_spec(cdata=cdataobj.W_CData, size=int) def buffer(space, cdata, size=-1): diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py --- a/pypy/module/_cffi_backend/test/_backend_test_c.py +++ b/pypy/module/_cffi_backend/test/_backend_test_c.py @@ -5,8 +5,6 @@ type_or_class = "type" mandatory_b_prefix = '' mandatory_u_prefix = 'u' - readbuf = str - bufchar = lambda x: x bytechr = chr class U(object): def __add__(self, other): @@ -20,11 +18,6 @@ unichr = chr mandatory_b_prefix = 'b' mandatory_u_prefix = '' - readbuf = lambda buf: buf.tobytes() - if sys.version_info < (3, 3): - bufchar = lambda x: bytes([ord(x)]) - else: - bufchar = ord bytechr = lambda n: bytes([n]) u = "" @@ -1811,36 +1804,76 @@ assert (p < s) ^ (p > s) def test_buffer(): + import __builtin__ BShort = new_primitive_type("short") s = newp(new_pointer_type(BShort), 100) assert sizeof(s) == size_of_ptr() assert sizeof(BShort) == 2 - assert len(readbuf(buffer(s))) == 2 + assert len(buffer(s)) == 2 # BChar = new_primitive_type("char") BCharArray = new_array_type(new_pointer_type(BChar), None) c = newp(BCharArray, b"hi there") + # buf = buffer(c) - assert readbuf(buf) == b"hi there\x00" + assert str(buf).startswith('<_cffi_backend.buffer object at 0x') + # --mb_length-- assert len(buf) == len(b"hi there\x00") - assert buf[0] == bufchar('h') - assert buf[2] == bufchar(' ') - assert list(buf) == list(map(bufchar, "hi there\x00")) - buf[2] = bufchar('-') - assert c[2] == b'-' - assert readbuf(buf) == b"hi-there\x00" - c[2] = b'!' - assert buf[2] == bufchar('!') - assert readbuf(buf) == b"hi!there\x00" - c[2] = b'-' - buf[:2] = b'HI' - assert string(c) == b'HI-there' - if sys.version_info < (3,) or sys.version_info >= (3, 3): - assert buf[:4:2] == b'H-' - if '__pypy__' not in sys.builtin_module_names: - # XXX pypy doesn't support the following assignment so far - buf[:4:2] = b'XY' - assert string(c) == b'XIYthere' + # --mb_item-- + for i in range(-12, 12): + try: + expected = b"hi there\x00"[i] + except IndexError: + py.test.raises(IndexError, "buf[i]") + else: + assert buf[i] == expected + # --mb_slice-- + assert buf[:] == b"hi there\x00" + for i in range(-12, 12): + assert buf[i:] == b"hi there\x00"[i:] + assert buf[:i] == b"hi there\x00"[:i] + for j in range(-12, 12): + assert buf[i:j] == b"hi there\x00"[i:j] + # --misc-- + assert list(buf) == list(b"hi there\x00") + # --mb_as_buffer-- + py.test.raises(TypeError, __builtin__.buffer, c) + bf1 = __builtin__.buffer(buf) + assert len(bf1) == len(buf) and bf1[3] == "t" + if hasattr(__builtin__, 'memoryview'): # Python >= 2.7 + py.test.raises(TypeError, memoryview, c) + mv1 = memoryview(buf) + assert len(mv1) == len(buf) and mv1[3] == "t" + # --mb_ass_item-- + expected = list(b"hi there\x00") + for i in range(-12, 12): + try: + expected[i] = chr(i & 0xff) + except IndexError: + py.test.raises(IndexError, "buf[i] = chr(i & 0xff)") + else: + buf[i] = chr(i & 0xff) + assert list(buf) == expected + # --mb_ass_slice-- + buf[:] = b"hi there\x00" + assert list(buf) == list(c) == list(b"hi there\x00") + py.test.raises(ValueError, 'buf[:] = b"shorter"') + py.test.raises(ValueError, 'buf[:] = b"this is much too long!"') + buf[4:2] = b"" # no effect, but should work + assert buf[:] == b"hi there\x00" + expected = list(b"hi there\x00") + x = 0 + for i in range(-12, 12): + for j in range(-12, 12): + start = i if i >= 0 else i + len(buf) + stop = j if j >= 0 else j + len(buf) + start = max(0, min(len(buf), start)) + stop = max(0, min(len(buf), stop)) + sample = chr(x & 0xff) * (stop - start) + x += 1 + buf[i:j] = sample + expected[i:j] = sample + assert list(buf) == expected def test_getcname(): BUChar = new_primitive_type("unsigned char") diff --git a/pypy/module/_multiprocessing/test/test_connection.py b/pypy/module/_multiprocessing/test/test_connection.py --- a/pypy/module/_multiprocessing/test/test_connection.py +++ b/pypy/module/_multiprocessing/test/test_connection.py @@ -10,7 +10,8 @@ class AppTestBufferTooShort: def setup_class(cls): - space = gettestobjspace(usemodules=('_multiprocessing', 'thread', 'signal')) + space = gettestobjspace(usemodules=('_multiprocessing', 'thread', + 'signal', 'itertools')) cls.space = space if option.runappdirect: diff --git a/pypy/module/_multiprocessing/test/test_memory.py b/pypy/module/_multiprocessing/test/test_memory.py --- a/pypy/module/_multiprocessing/test/test_memory.py +++ b/pypy/module/_multiprocessing/test/test_memory.py @@ -3,7 +3,8 @@ class AppTestMemory: def setup_class(cls): space = gettestobjspace( - usemodules=('_multiprocessing', 'mmap', '_rawffi', '_ffi')) + usemodules=('_multiprocessing', 'mmap', '_rawffi', '_ffi', + 'itertools')) cls.space = space def test_address_of(self): diff --git a/pypy/module/bz2/interp_bz2.py b/pypy/module/bz2/interp_bz2.py --- a/pypy/module/bz2/interp_bz2.py +++ b/pypy/module/bz2/interp_bz2.py @@ -357,8 +357,8 @@ buffering = 1024 # minimum amount of compressed data read at once self.buffering = buffering - def close(self): - self.stream.close() + def close1(self, closefileno): + self.stream.close1(closefileno) def tell(self): return self.readlength @@ -479,9 +479,9 @@ self.compressor = W_BZ2Compressor(space, compresslevel) self.writtenlength = 0 - def close(self): + def close1(self, closefileno): self.stream.write(self.space.str_w(self.compressor.flush())) - self.stream.close() + self.stream.close1(closefileno) def write(self, data): self.stream.write(self.space.str_w(self.compressor.compress(data))) diff --git a/pypy/module/cpyext/dictobject.py b/pypy/module/cpyext/dictobject.py --- a/pypy/module/cpyext/dictobject.py +++ b/pypy/module/cpyext/dictobject.py @@ -198,8 +198,8 @@ @specialize.memo() def make_frozendict(space): return space.appexec([], '''(): - import collections - class FrozenDict(collections.Mapping): + import _abcoll + class FrozenDict(_abcoll.Mapping): def __init__(self, *args, **kwargs): self._d = dict(*args, **kwargs) def __iter__(self): diff --git a/pypy/module/cpyext/test/test_import.py b/pypy/module/cpyext/test/test_import.py --- a/pypy/module/cpyext/test/test_import.py +++ b/pypy/module/cpyext/test/test_import.py @@ -4,9 +4,9 @@ class TestImport(BaseApiTest): def test_import(self, space, api): - pdb = api.PyImport_Import(space.wrap("pdb")) - assert pdb - assert space.getattr(pdb, space.wrap("pm")) + stat = api.PyImport_Import(space.wrap("stat")) + assert stat + assert space.getattr(stat, space.wrap("S_IMODE")) def test_addmodule(self, space, api): with rffi.scoped_str2charp("sys") as modname: @@ -32,10 +32,10 @@ assert space.is_true(space.contains(w_dict, space.wrap(testmod))) def test_reload(self, space, api): - pdb = api.PyImport_Import(space.wrap("pdb")) - space.delattr(pdb, space.wrap("set_trace")) - pdb = api.PyImport_ReloadModule(pdb) - assert space.getattr(pdb, space.wrap("set_trace")) + stat = api.PyImport_Import(space.wrap("stat")) + space.delattr(stat, space.wrap("S_IMODE")) + stat = api.PyImport_ReloadModule(stat) + assert space.getattr(stat, space.wrap("S_IMODE")) class AppTestImportLogic(AppTestCpythonExtensionBase): def test_import_logic(self): diff --git a/pypy/module/imp/test/test_app.py b/pypy/module/imp/test/test_app.py --- a/pypy/module/imp/test/test_app.py +++ b/pypy/module/imp/test/test_app.py @@ -3,6 +3,8 @@ class AppTestImpModule: def setup_class(cls): + from pypy.conftest import gettestobjspace + cls.space = gettestobjspace(usemodules=('imp', 'itertools')) cls.w_imp = cls.space.getbuiltinmodule('imp') cls.w_file_module = cls.space.wrap(__file__) diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py --- a/pypy/module/imp/test/test_import.py +++ b/pypy/module/imp/test/test_import.py @@ -38,7 +38,7 @@ test_reload = "def test():\n raise ValueError\n", infinite_reload = "import infinite_reload; reload(infinite_reload)", del_sys_module = "import sys\ndel sys.modules['del_sys_module']\n", - itertools = "hello_world = 42\n", + _md5 = "hello_world = 42\n", gc = "should_never_be_seen = 42\n", ) root.ensure("notapackage", dir=1) # empty, no __init__.py @@ -148,7 +148,7 @@ class AppTestImport: def setup_class(cls): # interpreter-level - cls.space = gettestobjspace(usemodules=['itertools']) + cls.space = gettestobjspace(usemodules=['_md5']) cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect) cls.saved_modules = _setup(cls.space) #XXX Compile class @@ -597,34 +597,34 @@ def test_shadow_extension_1(self): if self.runappdirect: skip("hard to test: module is already imported") - # 'import itertools' is supposed to find itertools.py if there is + # 'import _md5' is supposed to find _md5.py if there is # one in sys.path. import sys - assert 'itertools' not in sys.modules - import itertools - assert hasattr(itertools, 'hello_world') - assert not hasattr(itertools, 'count') - assert '(built-in)' not in repr(itertools) - del sys.modules['itertools'] + assert '_md5' not in sys.modules + import _md5 + assert hasattr(_md5, 'hello_world') + assert not hasattr(_md5, 'count') + assert '(built-in)' not in repr(_md5) + del sys.modules['_md5'] def test_shadow_extension_2(self): if self.runappdirect: skip("hard to test: module is already imported") - # 'import itertools' is supposed to find the built-in module even + # 'import _md5' is supposed to find the built-in module even # if there is also one in sys.path as long as it is *after* the # special entry '.../lib_pypy/__extensions__'. (Note that for now - # there is one in lib_pypy/itertools.py, which should not be seen + # there is one in lib_pypy/_md5.py, which should not be seen # either; hence the (built-in) test below.) import sys - assert 'itertools' not in sys.modules + assert '_md5' not in sys.modules sys.path.append(sys.path.pop(0)) try: - import itertools - assert not hasattr(itertools, 'hello_world') - assert hasattr(itertools, 'izip') - assert '(built-in)' in repr(itertools) + import _md5 + assert not hasattr(_md5, 'hello_world') + assert hasattr(_md5, 'digest_size') + assert '(built-in)' in repr(_md5) finally: sys.path.insert(0, sys.path.pop()) - del sys.modules['itertools'] + del sys.modules['_md5'] def test_invalid_pathname(self): import imp @@ -1005,7 +1005,7 @@ class AppTestImportHooks(object): def setup_class(cls): - space = cls.space = gettestobjspace(usemodules=('struct',)) + space = cls.space = gettestobjspace(usemodules=('struct', 'itertools')) mydir = os.path.dirname(__file__) cls.w_hooktest = space.wrap(os.path.join(mydir, 'hooktest')) space.appexec([space.wrap(mydir)], """ diff --git a/pypy/module/math/test/test_math.py b/pypy/module/math/test/test_math.py --- a/pypy/module/math/test/test_math.py +++ b/pypy/module/math/test/test_math.py @@ -6,7 +6,7 @@ class AppTestMath: def setup_class(cls): - cls.space = gettestobjspace(usemodules=['math', 'struct']) + cls.space = gettestobjspace(usemodules=['math', 'struct', 'itertools']) cls.w_cases = cls.space.wrap(test_direct.MathTests.TESTCASES) cls.w_consistent_host = cls.space.wrap(test_direct.consistent_host) diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py --- a/pypy/module/micronumpy/interp_boxes.py +++ b/pypy/module/micronumpy/interp_boxes.py @@ -275,14 +275,6 @@ arr.storage[i] = arg[i] return W_StringBox(arr, 0, arr.dtype) - # Running entire test suite needs this function to succeed, - # running single test_stringarray succeeds without it. - # With convert_to() test_ztranslation fails since - # W_CharacterBox is not a W_GenericBox. - # Why is it needed for multiple tests? - #def convert_to(self, dtype): - # xxx - class W_UnicodeBox(W_CharacterBox): def descr__new__unicode_box(space, w_subtype, w_arg): from pypy.module.micronumpy.interp_dtype import new_unicode_dtype diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py --- a/pypy/rlib/rbigint.py +++ b/pypy/rlib/rbigint.py @@ -119,6 +119,17 @@ self.size = size or len(digits) self.sign = sign + # __eq__ and __ne__ method exist for testingl only, they are not RPython! + def __eq__(self, other): + # NOT_RPYTHON + if not isinstance(other, rbigint): + return NotImplemented + return self.eq(other) + + def __ne__(self, other): + # NOT_RPYTHON + return not (self == other) + def digit(self, x): """Return the x'th digit, as an int.""" return self._digits[x] diff --git a/pypy/rlib/test/test_rbigint.py b/pypy/rlib/test/test_rbigint.py --- a/pypy/rlib/test/test_rbigint.py +++ b/pypy/rlib/test/test_rbigint.py @@ -1,14 +1,19 @@ from __future__ import division + +import operator +import sys +from random import random, randint, sample + import py -import operator, sys, array -from random import random, randint, sample -from pypy.rlib.rbigint import rbigint, SHIFT, MASK, KARATSUBA_CUTOFF -from pypy.rlib.rbigint import _store_digit, _mask_digit -from pypy.rlib.rfloat import NAN + from pypy.rlib import rbigint as lobj from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong, intmask +from pypy.rlib.rbigint import (rbigint, SHIFT, MASK, KARATSUBA_CUTOFF, + _store_digit, _mask_digit) +from pypy.rlib.rfloat import NAN from pypy.rpython.test.test_llinterp import interpret + class TestRLong(object): def test_simple(self): for op1 in [-2, -1, 0, 1, 2, 50]: @@ -112,6 +117,17 @@ rl = rbigint.fromint(sys.maxint).add(rbigint.fromint(42)) assert rl.touint() == result + def test_eq_ne_operators(self): + a1 = rbigint.fromint(12) + a2 = rbigint.fromint(12) + a3 = rbigint.fromint(123) + + assert a1 == a2 + assert a1 != a3 + assert not (a1 != a2) + assert not (a1 == a3) + + def gen_signs(l): for s in l: if s == 0: _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit