]> granicus.if.org Git - python/commitdiff
Fix incorrect stacklevel for DeprecationWarnings originating from the struct module.
authorMark Dickinson <dickinsm@gmail.com>
Fri, 5 Mar 2010 14:36:20 +0000 (14:36 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Fri, 5 Mar 2010 14:36:20 +0000 (14:36 +0000)
Also clean up related tests in test_struct.
The stacklevel fix should be backported to 2.6 once that branch is unfrozen.

Lib/test/test_struct.py
Modules/_struct.c

index fc48c11e6b3b63e1180c80300bc511e8d1ce091b..c315d8a3152670ed6af9b7b8df2e726fcabfab3c 100644 (file)
@@ -23,13 +23,6 @@ except struct.error:
 else:
     HAVE_LONG_LONG = True
 
-try:
-    import _struct
-except ImportError:
-    PY_STRUCT_FLOAT_COERCE = 2
-else:
-    PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0)
-
 def string_reverse(s):
     return "".join(reversed(s))
 
@@ -39,39 +32,25 @@ def bigendian_to_native(value):
     else:
         return string_reverse(value)
 
-def with_warning_restore(func):
-    @wraps(func)
-    def decorator(*args, **kw):
-        with warnings.catch_warnings():
-            # We need this function to warn every time, so stick an
-            # unqualifed 'always' at the head of the filter list
-            warnings.simplefilter("always")
-            warnings.filterwarnings("error", category=DeprecationWarning)
-            return func(*args, **kw)
-    return decorator
-
 class StructTest(unittest.TestCase):
 
-    @with_warning_restore
     def check_float_coerce(self, format, number):
         # SF bug 1530559. struct.pack raises TypeError where it used to convert.
-        if PY_STRUCT_FLOAT_COERCE == 2:
-            # Test for pre-2.5 struct module
-            packed = struct.pack(format, number)
-            floored = struct.unpack(format, packed)[0]
-            self.assertEqual(floored, int(number),
-                             "did not correcly coerce float to int")
-            return
-        try:
-            struct.pack(format, number)
-        except struct.error:
-            if PY_STRUCT_FLOAT_COERCE:
-                self.fail("expected DeprecationWarning for float coerce")
-        except DeprecationWarning:
-            if not PY_STRUCT_FLOAT_COERCE:
-                self.fail("expected to raise struct.error for float coerce")
-        else:
-            self.fail("did not raise error for float coerce")
+        with warnings.catch_warnings():
+            warnings.filterwarnings(
+                "ignore",
+                category=DeprecationWarning,
+                message=".*integer argument expected, got float",
+                module=__name__)
+            self.assertEqual(struct.pack(format, number), struct.pack(format, int(number)))
+
+        with warnings.catch_warnings():
+            warnings.filterwarnings(
+                "error",
+                category=DeprecationWarning,
+                message=".*integer argument expected, got float",
+                module="unittest")
+            self.assertRaises(DeprecationWarning, struct.pack, format, number)
 
     def test_isbigendian(self):
         self.assertEqual((struct.pack('=i', 1)[0] == chr(0)), ISBIGENDIAN)
index 27f8881fc0cd385dc92562f13be2f1adea556cd7..d7cc77fb0e1fb9994fb9472553cbf88438613d2b 100644 (file)
@@ -121,7 +121,7 @@ get_pylong(PyObject *v)
        }
 #ifdef PY_STRUCT_FLOAT_COERCE
        if (PyFloat_Check(v)) {
-               if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2)<0)
+               if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 1)<0)
                        return NULL;
                return PyNumber_Long(v);
        }