annotate Lib/warnings.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents f28b999a69f8
children fd249750cbae a60facc9f7cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Python part of the warnings subsystem."""
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 25791
diff changeset
3 # Note: function level imports should *not* be used
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 25791
diff changeset
4 # in this module as it may cause import lock deadlock.
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 25791
diff changeset
5 # See bug 683658.
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 25791
diff changeset
6 import linecache
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
7 import sys
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
8 import types
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16858
diff changeset
10 __all__ = ["warn", "showwarning", "formatwarning", "filterwarnings",
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
11 "resetwarnings", "catch_warnings"]
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16858
diff changeset
12
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
44038
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
14 def warnpy3k(message, category=None, stacklevel=1):
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
15 """Issue a deprecation warning for Python 3.x related changes.
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
16
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
17 Warnings are omitted unless Python is started with the -3 option.
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
18 """
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
19 if sys.py3kwarning:
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
20 if category is None:
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
21 category = DeprecationWarning
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
22 warn(message, category, stacklevel+1)
d44fffda2cd0 Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504
Christian Heimes <christian@cheimes.de>
parents: 39024
diff changeset
23
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
24 def _show_warning(message, category, filename, lineno, file=None, line=None):
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 """Hook to write a warning to a file; replace if you like."""
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if file is None:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 file = sys.stderr
25204
2e7fe55c0e11 Ignore IOError exceptions when writing the message.
Mark Hammond <mhammond@skippinet.com.au>
parents: 23645
diff changeset
28 try:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
29 file.write(formatwarning(message, category, filename, lineno, line))
25204
2e7fe55c0e11 Ignore IOError exceptions when writing the message.
Mark Hammond <mhammond@skippinet.com.au>
parents: 23645
diff changeset
30 except IOError:
2e7fe55c0e11 Ignore IOError exceptions when writing the message.
Mark Hammond <mhammond@skippinet.com.au>
parents: 23645
diff changeset
31 pass # the file (probably stderr) is invalid - this warning gets lost.
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
32 # Keep a worrking version around in case the deprecation of the old API is
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
33 # triggered.
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
34 showwarning = _show_warning
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
36 def formatwarning(message, category, filename, lineno, line=None):
15853
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
37 """Function to format a warning the standard way."""
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
39 line = linecache.getline(filename, lineno) if line is None else line
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 if line:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
41 line = line.strip()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
42 s += " %s\n" % line
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return s
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
15853
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
45 def filterwarnings(action, message="", category=Warning, module="", lineno=0,
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
46 append=0):
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 """Insert an entry into the list of warnings filters (at the front).
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 Use assertions to check that all arguments have the right type."""
29114
4b0220260f62 defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents: 27958
diff changeset
50 import re
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 assert action in ("error", "ignore", "always", "default", "module",
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
52 "once"), "invalid action: %r" % (action,)
25791
31b2fb448514 Allow Unicode strings as message and module name.
Martin v. Löwis <martin@v.loewis.de>
parents: 25204
diff changeset
53 assert isinstance(message, basestring), "message must be a string"
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36277
diff changeset
54 assert isinstance(category, (type, types.ClassType)), \
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36277
diff changeset
55 "category must be a class"
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 assert issubclass(category, Warning), "category must be a Warning subclass"
25791
31b2fb448514 Allow Unicode strings as message and module name.
Martin v. Löwis <martin@v.loewis.de>
parents: 25204
diff changeset
57 assert isinstance(module, basestring), "module must be a string"
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23620
diff changeset
58 assert isinstance(lineno, int) and lineno >= 0, \
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 "lineno must be an int >= 0"
15853
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
60 item = (action, re.compile(message, re.I), category,
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
61 re.compile(module), lineno)
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
62 if append:
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
63 filters.append(item)
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
64 else:
a84b53ba90c4 - Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents: 15611
diff changeset
65 filters.insert(0, item)
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
67 def simplefilter(action, category=Warning, lineno=0, append=0):
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
68 """Insert a simple entry into the list of warnings filters (at the front).
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
69
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
70 A simple filter matches all modules and messages.
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
71 """
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
72 assert action in ("error", "ignore", "always", "default", "module",
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
73 "once"), "invalid action: %r" % (action,)
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
74 assert isinstance(lineno, int) and lineno >= 0, \
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
75 "lineno must be an int >= 0"
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
76 item = (action, None, category, None, lineno)
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
77 if append:
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
78 filters.append(item)
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
79 else:
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
80 filters.insert(0, item)
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29114
diff changeset
81
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def resetwarnings():
23076
0fce55675cbc resetwarnings(): Remove extra space from docstring guts.
Tim Peters <tim.peters@gmail.com>
parents: 23075
diff changeset
83 """Clear the list of warning filters, so that no filters are active."""
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 filters[:] = []
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 class _OptionError(Exception):
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 """Exception used by option processing helpers."""
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 pass
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 # Helper to process -W options passed via sys.warnoptions
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def _processoptions(args):
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 for arg in args:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 try:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 _setoption(arg)
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 except _OptionError, msg:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 print >>sys.stderr, "Invalid -W option ignored:", msg
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 # Helper for _processoptions()
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def _setoption(arg):
29114
4b0220260f62 defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents: 27958
diff changeset
100 import re
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
101 parts = arg.split(':')
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
102 if len(parts) > 5:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
103 raise _OptionError("too many fields (max 5): %r" % (arg,))
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
104 while len(parts) < 5:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
105 parts.append('')
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
106 action, message, category, module, lineno = [s.strip()
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
107 for s in parts]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
108 action = _getaction(action)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
109 message = re.escape(message)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
110 category = _getcategory(category)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
111 module = re.escape(module)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
112 if module:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
113 module = module + '$'
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
114 if lineno:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
115 try:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
116 lineno = int(lineno)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
117 if lineno < 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
118 raise ValueError
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
119 except (ValueError, OverflowError):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
120 raise _OptionError("invalid lineno %r" % (lineno,))
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
121 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
122 lineno = 0
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15853
diff changeset
123 filterwarnings(action, message, category, module, lineno)
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # Helper for _setoption()
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 def _getaction(action):
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if not action:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return "default"
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if action == "all": return "always" # Alias
34545
9d66fba78444 Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents: 34278
diff changeset
130 for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if a.startswith(action):
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return a
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
133 raise _OptionError("invalid action: %r" % (action,))
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # Helper for _setoption()
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 def _getcategory(category):
29114
4b0220260f62 defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents: 27958
diff changeset
137 import re
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if not category:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 return Warning
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 if re.match("^[a-zA-Z0-9_]+$", category):
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 try:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 cat = eval(category)
15611
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
143 except NameError:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
144 raise _OptionError("unknown warning category: %r" % (category,))
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 else:
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 i = category.rfind(".")
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 module = category[:i]
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 klass = category[i+1:]
15611
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
149 try:
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
150 m = __import__(module, None, None, [klass])
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
151 except ImportError:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
152 raise _OptionError("invalid module name: %r" % (module,))
15611
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
153 try:
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
154 cat = getattr(m, klass)
2fe7427591ea Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents: 15592
diff changeset
155 except AttributeError:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
156 raise _OptionError("unknown warning category: %r" % (category,))
38912
58034084ef44 'warning's was improperly requiring that a command-line Warning category be
Brett Cannon <bcannon@gmail.com>
parents: 38704
diff changeset
157 if not issubclass(cat, Warning):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29813
diff changeset
158 raise _OptionError("invalid warning category: %r" % (category,))
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return cat
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
161
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
162 # Code typically replaced by _warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
163 def warn(message, category=None, stacklevel=1):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
164 """Issue a warning, or maybe ignore it or raise an exception."""
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
165 # Check if message is already a Warning object
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
166 if isinstance(message, Warning):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
167 category = message.__class__
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
168 # Check category argument
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
169 if category is None:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
170 category = UserWarning
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
171 assert issubclass(category, Warning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
172 # Get context information
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
173 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
174 caller = sys._getframe(stacklevel)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
175 except ValueError:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
176 globals = sys.__dict__
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
177 lineno = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
178 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
179 globals = caller.f_globals
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
180 lineno = caller.f_lineno
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
181 if '__name__' in globals:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
182 module = globals['__name__']
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
183 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
184 module = "<string>"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
185 filename = globals.get('__file__')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
186 if filename:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
187 fnl = filename.lower()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
188 if fnl.endswith((".pyc", ".pyo")):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
189 filename = filename[:-1]
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
190 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
191 if module == "__main__":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
192 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
193 filename = sys.argv[0]
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
194 except AttributeError:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
195 # embedded interpreters don't have sys.argv, see bug #839151
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
196 filename = '__main__'
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
197 if not filename:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
198 filename = module
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
199 registry = globals.setdefault("__warningregistry__", {})
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
200 warn_explicit(message, category, filename, lineno, module, registry,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
201 globals)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
202
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
203 def warn_explicit(message, category, filename, lineno,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
204 module=None, registry=None, module_globals=None):
47835
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46720
diff changeset
205 lineno = int(lineno)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
206 if module is None:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
207 module = filename or "<unknown>"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
208 if module[-3:].lower() == ".py":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
209 module = module[:-3] # XXX What about leading pathname?
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
210 if registry is None:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
211 registry = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
212 if isinstance(message, Warning):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
213 text = str(message)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
214 category = message.__class__
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
215 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
216 text = message
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
217 message = category(message)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
218 key = (text, category, lineno)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
219 # Quick test for common case
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
220 if registry.get(key):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
221 return
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
222 # Search the filters
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
223 for item in filters:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
224 action, msg, cat, mod, ln = item
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
225 if ((msg is None or msg.match(text)) and
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
226 issubclass(category, cat) and
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
227 (mod is None or mod.match(module)) and
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
228 (ln == 0 or lineno == ln)):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
229 break
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
230 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
231 action = defaultaction
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
232 # Early exit actions
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
233 if action == "ignore":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
234 registry[key] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
235 return
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
236
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
237 # Prime the linecache for formatting, in case the
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
238 # "file" is actually in a zipfile or something.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
239 linecache.getlines(filename, module_globals)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
240
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
241 if action == "error":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
242 raise message
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
243 # Other actions
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
244 if action == "once":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
245 registry[key] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
246 oncekey = (text, category)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
247 if onceregistry.get(oncekey):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
248 return
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
249 onceregistry[oncekey] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
250 elif action == "always":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
251 pass
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
252 elif action == "module":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
253 registry[key] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
254 altkey = (text, category, 0)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
255 if registry.get(altkey):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
256 return
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
257 registry[altkey] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
258 elif action == "default":
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
259 registry[key] = 1
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
260 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
261 # Unrecognized actions are errors
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
262 raise RuntimeError(
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
263 "Unrecognized action (%r) in warnings.filters:\n %s" %
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
264 (action, item))
46669
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
265 # Warn if showwarning() does not support the 'line' argument.
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
266 # Don't use 'inspect' as it relies on an extension module, which break the
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
267 # build thanks to 'warnings' being imported by setup.py.
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
268 fxn_code = None
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
269 if hasattr(showwarning, 'func_code'):
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
270 fxn_code = showwarning.func_code
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
271 elif hasattr(showwarning, '__func__'):
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
272 fxn_code = showwarning.__func__.func_code
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
273 if fxn_code:
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
274 args = fxn_code.co_varnames[:fxn_code.co_argcount]
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
275 CO_VARARGS = 0x4
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
276 if 'line' not in args and not fxn_code.co_flags & CO_VARARGS:
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
277 showwarning_msg = ("functions overriding warnings.showwarning() "
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
278 "must support the 'line' argument")
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
279 if message == showwarning_msg:
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
280 _show_warning(message, category, filename, lineno)
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
281 else:
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
282 warn(showwarning_msg, DeprecationWarning)
46669
9b10bf64f124 Remove the use of 'inspect' from 'warnings' for detected deprecated use of the
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
283 # Print message and context
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
284 showwarning(message, category, filename, lineno)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
285
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
286
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
287 class WarningMessage(object):
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
288
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
289 """Holds the result of a single showwarning() call."""
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
290
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
291 _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
292 "line")
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
293
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
294 def __init__(self, message, category, filename, lineno, file=None,
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
295 line=None):
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
296 local_values = locals()
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
297 for attr in self._WARNING_DETAILS:
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
298 setattr(self, attr, local_values[attr])
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
299 self._category_name = category.__name__ if category else None
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
300
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
301 def __str__(self):
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
302 return ("{message : %r, category : %r, filename : %r, lineno : %s, "
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
303 "line : %r}" % (self.message, self._category_name,
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
304 self.filename, self.lineno, self.line))
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
305
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
306
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
307 class catch_warnings(object):
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
308
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
309 """A context manager that copies and restores the warnings filter upon
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
310 exiting the context.
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
311
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
312 The 'record' argument specifies whether warnings should be captured by a
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
313 custom implementation of warnings.showwarning() and be appended to a list
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
314 returned by the context manager. Otherwise None is returned by the context
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
315 manager. The objects appended to the list are arguments whose attributes
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
316 mirror the arguments to showwarning().
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
317
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
318 The 'module' argument is to specify an alternative module to the module
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
319 named 'warnings' and imported under that name. This argument is only useful
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
320 when testing the warnings module itself.
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
321
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
322 """
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
323
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
324 def __init__(self, record=False, module=None):
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
325 """Specify whether to record warnings and if an alternative module
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
326 should be used other than sys.modules['warnings'].
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
327
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
328 For compatibility with Python 3.0, please consider all arguments to be
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
329 keyword-only.
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
330
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
331 """
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
332 self._record = record
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
333 self._module = sys.modules['warnings'] if module is None else module
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
334 self._entered = False
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
335
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
336 def __repr__(self):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
337 args = []
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
338 if self._record:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
339 args.append("record=True")
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
340 if self._module is not sys.modules['warnings']:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
341 args.append("module=%r" % self._module)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
342 name = type(self).__name__
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
343 return "%s(%s)" % (name, ", ".join(args))
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
344
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
345 def __enter__(self):
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
346 if self._entered:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
347 raise RuntimeError("Cannot enter %r twice" % self)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
348 self._entered = True
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
349 self._filters = self._module.filters
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
350 self._module.filters = self._filters[:]
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
351 self._showwarning = self._module.showwarning
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
352 if self._record:
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
353 log = []
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
354 def showwarning(*args, **kwargs):
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
355 log.append(WarningMessage(*args, **kwargs))
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
356 self._module.showwarning = showwarning
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
357 return log
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
358 else:
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
359 return None
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
360
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
361 def __exit__(self, *exc_info):
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
362 if not self._entered:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
363 raise RuntimeError("Cannot exit %r without entering first" % self)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
364 self._module.filters = self._filters
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
365 self._module.showwarning = self._showwarning
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
366
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47835
diff changeset
367
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
368 # filters contains a sequence of filter 5-tuples
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
369 # The components of the 5-tuple are:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
370 # - an action: error, ignore, always, default, module, or once
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
371 # - a compiled regex that must match the warning message
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
372 # - a class representing the warning category
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
373 # - a compiled regex that must match the module that is being warned
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
374 # - a line number for the line being warning, or 0 to mean any line
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
375 # If either if the compiled regexs are None, match anything.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
376 _warnings_defaults = False
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
377 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
378 from _warnings import (filters, default_action, once_registry,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
379 warn, warn_explicit)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
380 defaultaction = default_action
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
381 onceregistry = once_registry
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
382 _warnings_defaults = True
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
383 except ImportError:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
384 filters = []
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
385 defaultaction = "default"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
386 onceregistry = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
387
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
388
15592
52756f613c18 Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 # Module initialization
31737
57965b2db20c SF bug 917108: warnings.py does not define _test().
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
390 _processoptions(sys.warnoptions)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
391 if not _warnings_defaults:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
392 simplefilter("ignore", category=PendingDeprecationWarning, append=1)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
393 simplefilter("ignore", category=ImportWarning, append=1)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
394 bytes_warning = sys.flags.bytes_warning
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
395 if bytes_warning > 1:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
396 bytes_action = "error"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
397 elif bytes_warning:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
398 bytes_action = "default"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
399 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
400 bytes_action = "ignore"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
401 simplefilter(bytes_action, category=BytesWarning, append=1)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44038
diff changeset
402 del _warnings_defaults