summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_warnings.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2003-07-11 15:37:59 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2003-07-11 15:37:59 (GMT)
commit8501466c7f0843e68ee6fb91e3d8221d5eb1a019 (patch)
tree97403c9fde25171a8e37a3a9a4a44bd3eabe3836 /Lib/test/test_warnings.py
parent1e5fc55c4dcddcd97951d54c04b321c0a284affa (diff)
downloadcpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.zip
cpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.tar.gz
cpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.tar.bz2
Change warnings to avoid importing re module during startup.
Add API function simplefilter() that does not create or install regular expressions to match message or module. Extend the filters data structure to store None as an alternative to re.compile(""). Move the _test() function to test_warnings and add some code to try and avoid disturbing the global state of the warnings module.
Diffstat (limited to 'Lib/test/test_warnings.py')
-rw-r--r--Lib/test/test_warnings.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
new file mode 100644
index 0000000..9cd3c30
--- /dev/null
+++ b/Lib/test/test_warnings.py
@@ -0,0 +1,53 @@
+import warnings
+
+# The warnings module isn't easily tested, because it relies on module
+# globals to store configuration information. We need to extract the
+# current settings to avoid bashing them while running tests.
+
+_filters = []
+_showwarning = None
+
+def showwarning(message, category, filename, lineno, file=None):
+ i = filename.find("Lib/")
+ filename = filename[i:]
+ print "%s:%s: %s: %s" % (filename, lineno, category.__name__, message)
+
+def monkey():
+ global _filters, _showwarning
+ _filters = warnings.filters[:]
+ _showwarning = warnings.showwarning
+ warnings.showwarning = showwarning
+
+def unmonkey():
+ warnings.filters = _filters[:]
+ warnings.showwarning = _showwarning
+
+def test():
+ for item in warnings.filters:
+ print (item[0], item[1] is None, item[2].__name__, item[3] is None,
+ item[4])
+ hello = "hello world"
+ for i in range(4):
+ warnings.warn(hello)
+ warnings.warn(hello, UserWarning)
+ warnings.warn(hello, DeprecationWarning)
+ for i in range(3):
+ warnings.warn(hello)
+ warnings.filterwarnings("error", "", Warning, "", 0)
+ try:
+ warnings.warn(hello)
+ except Exception, msg:
+ print "Caught", msg.__class__.__name__ + ":", msg
+ else:
+ print "No exception"
+ warnings.resetwarnings()
+ try:
+ warnings.filterwarnings("booh", "", Warning, "", 0)
+ except Exception, msg:
+ print "Caught", msg.__class__.__name__ + ":", msg
+ else:
+ print "No exception"
+
+monkey()
+test()
+unmonkey()