]> granicus.if.org Git - python/commitdiff
Add tests for the filename.
authorWalter Dörwald <walter@livinglogic.de>
Tue, 3 Apr 2007 16:53:43 +0000 (16:53 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Tue, 3 Apr 2007 16:53:43 +0000 (16:53 +0000)
Test that the stacklevel is handled correctly.

Lib/test/test_warnings.py
Lib/test/warning_tests.py [new file with mode: 0644]

index 671ed02a6129ce828d2d135701f96b3cdb6d78fd..f6c933962fae05837c44eccfdda968300a9e183f 100644 (file)
@@ -3,6 +3,8 @@ import os
 import unittest
 from test import test_support
 
+import warning_tests
+
 # The warnings module isn't easily tested, because it relies on module
 # globals to store configuration information.  setUp() and tearDown()
 # preserve the current settings to avoid bashing them while running tests.
@@ -97,6 +99,28 @@ class TestModule(CatchWarningTest):
         warnings._setoption('error::Warning::0')
         self.assertRaises(UserWarning, warnings.warn, 'convert to error')
 
+    def test_filename(self):
+        warning_tests.inner("spam1")
+        self.assertEqual(msg.filename, "warning_tests.py")
+        warning_tests.outer("spam2")
+        self.assertEqual(msg.filename, "warning_tests.py")
+
+    def test_stacklevel(self):
+        # Test stacklevel argument
+        # make sure all messages are different, so the warning won't be skipped
+        warning_tests.inner("spam3", stacklevel=1)
+        self.assertEqual(msg.filename, "warning_tests.py")
+        warning_tests.outer("spam4", stacklevel=1)
+        self.assertEqual(msg.filename, "warning_tests.py")
+
+        warning_tests.inner("spam5", stacklevel=2)
+        self.assertEqual(msg.filename, "test_warnings.py")
+        warning_tests.outer("spam6", stacklevel=2)
+        self.assertEqual(msg.filename, "warning_tests.py")
+
+        warning_tests.inner("spam7", stacklevel=9999)
+        self.assertEqual(msg.filename, "sys")
+
 
 def test_main(verbose=None):
     # Obscure hack so that this test passes after reloads or repeated calls
diff --git a/Lib/test/warning_tests.py b/Lib/test/warning_tests.py
new file mode 100644 (file)
index 0000000..d0519ef
--- /dev/null
@@ -0,0 +1,9 @@
+# Helper module for testing the skipmodules argument of warnings.warn()
+
+import warnings
+
+def outer(message, stacklevel=1):
+    inner(message, stacklevel)
+
+def inner(message, stacklevel=1):
+    warnings.warn(message, stacklevel=stacklevel)