Author: Brian Kearns <[email protected]>
Branch: py3k
Changeset: r71312:9401f74194b9
Date: 2014-05-05 22:43 -0400
http://bitbucket.org/pypy/pypy/changeset/9401f74194b9/

Log:    merge default

diff --git a/pypy/module/_cffi_backend/cdataobj.py 
b/pypy/module/_cffi_backend/cdataobj.py
--- a/pypy/module/_cffi_backend/cdataobj.py
+++ b/pypy/module/_cffi_backend/cdataobj.py
@@ -442,6 +442,7 @@
 
 W_CData.typedef = TypeDef(
     '_cffi_backend.CData',
+    __module__ = '_cffi_backend',
     __name__ = '<cdata>',
     __repr__ = interp2app(W_CData.repr),
     __bool__ = interp2app(W_CData.bool),
diff --git a/pypy/module/_io/interp_io.py b/pypy/module/_io/interp_io.py
--- a/pypy/module/_io/interp_io.py
+++ b/pypy/module/_io/interp_io.py
@@ -27,7 +27,7 @@
         self.written = written
 
 W_BlockingIOError.typedef = TypeDef(
-    '_io.BlockingIOError', W_IOError.typedef,
+    'BlockingIOError', W_IOError.typedef,
     __doc__ = ("Exception raised when I/O would block on a non-blocking "
                "I/O stream"),
     __new__  = generic_new_descr(W_BlockingIOError),
diff --git a/pypy/module/_io/test/test_io.py b/pypy/module/_io/test/test_io.py
--- a/pypy/module/_io/test/test_io.py
+++ b/pypy/module/_io/test/test_io.py
@@ -383,4 +383,10 @@
         import _io
         typemods = dict((t, t.__module__) for t in vars(_io).values()
                         if isinstance(t, type))
-        assert all(mod in ('io', '_io') for mod in typemods.values()), typemods
+        for t, mod in typemods.items():
+            if t is _io.BlockingIOError:
+                assert mod == '__builtin__'
+            elif t is _io.UnsupportedOperation:
+                assert mod == 'io'
+            else:
+                assert mod == '_io'
diff --git a/pypy/module/_io/test/test_stringio.py 
b/pypy/module/_io/test/test_stringio.py
--- a/pypy/module/_io/test/test_stringio.py
+++ b/pypy/module/_io/test/test_stringio.py
@@ -142,11 +142,6 @@
         exc_info = raises(TypeError, sio.write, 3)
         assert "int" in exc_info.value.args[0]
 
-    def test_module(self):
-        import io
-
-        assert io.StringIO.__module__ == "_io"
-
     def test_newline_none(self):
         import io
 
diff --git a/pypy/module/pypyjit/test_pypy_c/test_buffers.py 
b/pypy/module/pypyjit/test_pypy_c/test_buffers.py
--- a/pypy/module/pypyjit/test_pypy_c/test_buffers.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_buffers.py
@@ -56,7 +56,7 @@
             guard_false(i99, descr=...)
             i100 = int_lshift(i98, 24)
             i101 = int_or(i97, i100)
-            i102 = getfield_raw(50657056, descr=<FieldS 
pypysig_long_struct.c_value 0>)
+            i102 = getfield_raw(\d+, descr=<FieldS pypysig_long_struct.c_value 
0>)
             i103 = int_lt(i102, 0)
             guard_false(i103, descr=...)
         """)
diff --git a/pypy/module/pypyjit/test_pypy_c/test_misc.py 
b/pypy/module/pypyjit/test_pypy_c/test_misc.py
--- a/pypy/module/pypyjit/test_pypy_c/test_misc.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_misc.py
@@ -348,51 +348,6 @@
         loop, = log.loops_by_id("globalread", is_entry_bridge=True)
         assert len(loop.ops_by_id("globalread")) == 0
 
-    def test_struct_module(self):
-        def main():
-            import struct
-            i = 1
-            while i < 1000:
-                x = struct.unpack("i", struct.pack("i", i))[0] # ID: struct
-                i += x / i
-            return i
-
-        log = self.run(main)
-        assert log.result == main()
-
-        loop, = log.loops_by_id("struct")
-        if sys.maxint == 2 ** 63 - 1:
-            extra = """
-            i8 = int_ge(i4, -2147483648)
-            guard_true(i8, descr=...)
-            """
-        else:
-            extra = ""
-        # This could, of course stand some improvement, to remove all these
-        # arithmatic ops, but we've removed all the core overhead.
-        assert loop.match_by_id("struct", """
-            guard_not_invalidated(descr=...)
-            # struct.pack
-            %(32_bit_only)s
-            i11 = int_and(i4, 255)
-            i13 = int_rshift(i4, 8)
-            i14 = int_and(i13, 255)
-            i16 = int_rshift(i13, 8)
-            i17 = int_and(i16, 255)
-            i19 = int_rshift(i16, 8)
-            i20 = int_and(i19, 255)
-
-            # struct.unpack
-            i22 = int_lshift(i14, 8)
-            i23 = int_or(i11, i22)
-            i25 = int_lshift(i17, 16)
-            i26 = int_or(i23, i25)
-            i28 = int_ge(i20, 128)
-            guard_false(i28, descr=...)
-            i30 = int_lshift(i20, 24)
-            i31 = int_or(i26, i30)
-        """ % {"32_bit_only": extra})
-
     def test_eval(self):
         def main():
             i = 1
diff --git a/pypy/module/pypyjit/test_pypy_c/test_struct.py 
b/pypy/module/pypyjit/test_pypy_c/test_struct.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/pypyjit/test_pypy_c/test_struct.py
@@ -0,0 +1,84 @@
+from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
+
+
+class TestStruct(BaseTestPyPyC):
+    def test_struct_function(self):
+        def main(n):
+            import struct
+            i = 1
+            while i < n:
+                x = struct.unpack("i", struct.pack("i", i))[0]  # ID: struct
+                i += x / i
+            return i
+
+        log = self.run(main, [1000])
+        assert log.result == main(1000)
+
+        loop, = log.loops_by_filename(self.filepath)
+        # This could, of course stand some improvement, to remove all these
+        # arithmatic ops, but we've removed all the core overhead.
+        assert loop.match_by_id("struct", """
+            guard_not_invalidated(descr=...)
+            # struct.pack
+            i8 = int_ge(i4, -2147483648)
+            guard_true(i8, descr=...)
+            i9 = int_le(i4, 2147483647)
+            guard_true(i9, descr=...)
+            i11 = int_and(i4, 255)
+            i13 = int_rshift(i4, 8)
+            i14 = int_and(i13, 255)
+            i16 = int_rshift(i13, 8)
+            i17 = int_and(i16, 255)
+            i19 = int_rshift(i16, 8)
+            i20 = int_and(i19, 255)
+
+            # struct.unpack
+            i22 = int_lshift(i14, 8)
+            i23 = int_or(i11, i22)
+            i25 = int_lshift(i17, 16)
+            i26 = int_or(i23, i25)
+            i28 = int_ge(i20, 128)
+            guard_false(i28, descr=...)
+            i30 = int_lshift(i20, 24)
+            i31 = int_or(i26, i30)
+        """)
+
+    def test_struct_object(self):
+        def main(n):
+            import struct
+            s = struct.Struct("i")
+            i = 1
+            while i < n:
+                x = s.unpack(s.pack(i))[0]  # ID: struct
+                i += x / i
+            return i
+
+        log = self.run(main, [1000])
+        assert log.result == main(1000)
+
+        loop, = log.loops_by_filename(self.filepath)
+        assert loop.match_by_id('struct', """
+            guard_not_invalidated(descr=...)
+            # struct.pack
+            i8 = int_ge(i4, -2147483648)
+            guard_true(i8, descr=...)
+            i9 = int_le(i4, 2147483647)
+            guard_true(i9, descr=...)
+            i11 = int_and(i4, 255)
+            i13 = int_rshift(i4, 8)
+            i14 = int_and(i13, 255)
+            i16 = int_rshift(i13, 8)
+            i17 = int_and(i16, 255)
+            i19 = int_rshift(i16, 8)
+            i20 = int_and(i19, 255)
+
+            # struct.unpack
+            i22 = int_lshift(i14, 8)
+            i23 = int_or(i11, i22)
+            i25 = int_lshift(i17, 16)
+            i26 = int_or(i23, i25)
+            i28 = int_ge(i20, 128)
+            guard_false(i28, descr=...)
+            i30 = int_lshift(i20, 24)
+            i31 = int_or(i26, i30)
+        """)
diff --git a/pypy/module/struct/__init__.py b/pypy/module/struct/__init__.py
--- a/pypy/module/struct/__init__.py
+++ b/pypy/module/struct/__init__.py
@@ -48,6 +48,8 @@
     applevel_name = '_struct'
 
     interpleveldefs = {
+        'error': 'interp_struct.get_error(space)',
+
         'calcsize': 'interp_struct.calcsize',
         'pack': 'interp_struct.pack',
         'pack_into': 'interp_struct.pack_into',
@@ -59,5 +61,4 @@
     }
 
     appleveldefs = {
-        'error': 'app_struct.error',
     }
diff --git a/pypy/module/struct/app_struct.py b/pypy/module/struct/app_struct.py
deleted file mode 100644
--- a/pypy/module/struct/app_struct.py
+++ /dev/null
@@ -1,9 +0,0 @@
-# NOT_RPYTHON
-"""
-Application-level definitions for the struct module.
-"""
-
-
-class error(Exception):
-    """Exception raised on various occasions; argument is a string
-    describing what is wrong."""
diff --git a/pypy/module/struct/interp_struct.py 
b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -2,7 +2,6 @@
 from rpython.rlib.buffer import SubBuffer
 from rpython.rlib.rstruct.error import StructError, StructOverflowError
 from rpython.rlib.rstruct.formatiterator import CalcSizeFormatIterator
-from rpython.tool.sourcetools import func_with_new_name
 
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.gateway import interp2app, unwrap_spec
@@ -13,6 +12,15 @@
 )
 
 
+class Cache:
+    def __init__(self, space):
+        self.error = space.new_exception_class("struct.error", 
space.w_Exception)
+
+
+def get_error(space):
+    return space.fromcache(Cache).error
+
+
 @unwrap_spec(format=str)
 def calcsize(space, format):
     return space.wrap(_calcsize(space, format))
@@ -25,9 +33,7 @@
     except StructOverflowError, e:
         raise OperationError(space.w_OverflowError, space.wrap(e.msg))
     except StructError, e:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise OperationError(w_error, space.wrap(e.msg))
+        raise OperationError(get_error(space), space.wrap(e.msg))
     return fmtiter.totalsize
 
 
@@ -43,24 +49,20 @@
     except StructOverflowError, e:
         raise OperationError(space.w_OverflowError, space.wrap(e.msg))
     except StructError, e:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise OperationError(w_error, space.wrap(e.msg))
+        raise OperationError(get_error(space), space.wrap(e.msg))
     return space.wrapbytes(fmtiter.result.build())
 
 
 # XXX inefficient
 @unwrap_spec(format=str, offset=int)
-def pack_into(space, format, w_buf, offset, args_w):
+def pack_into(space, format, w_buffer, offset, args_w):
     res = pack(space, format, args_w).bytes_w(space)
-    buf = space.writebuf_w(w_buf)
+    buf = space.writebuf_w(w_buffer)
     if offset < 0:
         offset += buf.getlength()
     size = len(res)
     if offset < 0 or (buf.getlength() - offset) < size:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise oefmt(w_error,
+        raise oefmt(get_error(space),
                     "pack_into requires a buffer of at least %d bytes",
                     size)
     buf.setslice(offset, res)
@@ -73,9 +75,7 @@
     except StructOverflowError, e:
         raise OperationError(space.w_OverflowError, space.wrap(e.msg))
     except StructError, e:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise OperationError(w_error, space.wrap(e.msg))
+        raise OperationError(get_error(space), space.wrap(e.msg))
     return space.newtuple(fmtiter.result_w[:])
 
 def clearcache(space):
@@ -96,9 +96,7 @@
     if offset < 0:
         offset += buf.getlength()
     if offset < 0 or (buf.getlength() - offset) < size:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise oefmt(w_error,
+        raise oefmt(get_error(space),
                     "unpack_from requires a buffer of at least %d bytes",
                     size)
     buf = SubBuffer(buf, offset, size)
@@ -118,21 +116,19 @@
         W_Struct.__init__(self, space, format)
         return self
 
-    def wrap_struct_method(name):
-        def impl(self, space, __args__):
-            w_module = space.getbuiltinmodule('struct')
-            w_method = space.getattr(w_module, space.wrap(name))
-            return space.call_obj_args(
-                w_method, space.wrap(self.format), __args__
-            )
+    def descr_pack(self, space, args_w):
+        return pack(space, jit.promote_string(self.format), args_w)
 
-        return func_with_new_name(impl, 'descr_' + name)
+    @unwrap_spec(offset=int)
+    def descr_pack_into(self, space, w_buffer, offset, args_w):
+        return pack_into(space, jit.promote_string(self.format), w_buffer, 
offset, args_w)
 
-    descr_pack = wrap_struct_method("pack")
-    descr_unpack = wrap_struct_method("unpack")
-    descr_pack_into = wrap_struct_method("pack_into")
-    descr_unpack_from = wrap_struct_method("unpack_from")
+    def descr_unpack(self, space, w_str):
+        return unpack(space, jit.promote_string(self.format), w_str)
 
+    @unwrap_spec(offset=int)
+    def descr_unpack_from(self, space, w_buffer, offset=0):
+        return unpack_from(space, jit.promote_string(self.format), w_buffer, 
offset)
 
 W_Struct.typedef = TypeDef("Struct",
     __new__=interp2app(W_Struct.descr__new__.im_func),
diff --git a/pypy/module/struct/test/test_struct.py 
b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -24,6 +24,10 @@
         struct.error should be an exception class.
         """
         assert issubclass(self.struct.error, Exception)
+        assert self.struct.error.__mro__ == (self.struct.error, Exception,
+                                             BaseException, object)
+        assert self.struct.error.__name__ == "error"
+        assert self.struct.error.__module__ == "struct"
 
     def test_calcsize_standard(self):
         """
@@ -396,6 +400,11 @@
         assert type(obj2) is float
         assert obj2 == 42.3
 
+    def test_struct_object(self):
+        s = self.struct.Struct('i')
+        assert s.unpack(s.pack(42)) == (42,)
+        assert s.unpack_from(memoryview(s.pack(42))) == (42,)
+
     def test_trailing_counter(self):
         import array
         store = array.array('b', b' '*100)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to