[pypy-commit] pypy default: move _struct.error to interp level, test/fix its __module__

bdkearns noreply at buildbot.pypy.org
Tue May 6 04:11:36 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r71305:40142188f76a
Date: 2014-05-05 17:24 -0400
http://bitbucket.org/pypy/pypy/changeset/40142188f76a/

Log:	move _struct.error to interp level, test/fix its __module__

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
@@ -46,6 +46,8 @@
 The variable struct.error is an exception raised on errors."""
 
     interpleveldefs = {
+        'error': 'interp_struct.get_error(space)',
+
         'calcsize': 'interp_struct.calcsize',
         'pack': 'interp_struct.pack',
         'pack_into': 'interp_struct.pack_into',
@@ -56,5 +58,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
@@ -12,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))
@@ -24,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
 
 
@@ -42,9 +49,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.wrap(fmtiter.result.build())
 
 
@@ -57,9 +62,7 @@
         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)
@@ -72,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[:])
 
 
@@ -89,15 +90,11 @@
     size = _calcsize(space, format)
     buf = space.getarg_w('z*', w_buffer)
     if buf is None:
-        w_module = space.getbuiltinmodule('struct')
-        w_error = space.getattr(w_module, space.wrap('error'))
-        raise oefmt(w_error, "unpack_from requires a buffer argument")
+        raise oefmt(get_error(space), "unpack_from requires a buffer argument")
     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)
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):
         """


More information about the pypy-commit mailing list