Mercurial > cpython-lru_cache-weakref
annotate Lib/warnings.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children |
| 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. |
|
63603
fd249750cbae
Merged revisions 78757 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
49146
diff
changeset
|
32 # Keep a working version around in case the deprecation of the old API is |
|
46667
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 |
|
56693
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
49 'action' -- one of "error", "ignore", "always", "default", "module", |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
50 or "once" |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
51 'message' -- a regex that the warning message must match |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
52 'category' -- a class that the warning must be a subclass of |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
53 'module' -- a regex that the module name must match |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
54 'lineno' -- an integer line number, 0 matches all warnings |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
55 'append' -- if true, append to the list of filters |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
56 """ |
|
29114
4b0220260f62
defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents:
27958
diff
changeset
|
57 import re |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 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
|
59 "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
|
60 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
|
61 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
|
62 "category must be a class" |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 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
|
64 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
|
65 assert isinstance(lineno, int) and lineno >= 0, \ |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 "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
|
67 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
|
68 re.compile(module), lineno) |
|
a84b53ba90c4
- Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents:
15611
diff
changeset
|
69 if append: |
|
a84b53ba90c4
- Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents:
15611
diff
changeset
|
70 filters.append(item) |
|
a84b53ba90c4
- Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents:
15611
diff
changeset
|
71 else: |
|
a84b53ba90c4
- Added keyword argument 'append' to filterwarnings(); if true, this
Guido van Rossum <guido@python.org>
parents:
15611
diff
changeset
|
72 filters.insert(0, item) |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
29813
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
74 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
|
75 """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
|
76 |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
77 A simple filter matches all modules and messages. |
|
56693
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
78 'action' -- one of "error", "ignore", "always", "default", "module", |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
79 or "once" |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
80 'category' -- a class that the warning must be a subclass of |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
81 'lineno' -- an integer line number, 0 matches all warnings |
|
f38dabf240cb
Improve some docstrings in the 'warnings' module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
52125
diff
changeset
|
82 'append' -- if true, append to the list of filters |
|
29813
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
83 """ |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
84 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
|
85 "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
|
86 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
|
87 "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
|
88 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
|
89 if append: |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
90 filters.append(item) |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
91 else: |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
92 filters.insert(0, item) |
|
7be24cc6c28d
Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29114
diff
changeset
|
93 |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 def resetwarnings(): |
|
23076
0fce55675cbc
resetwarnings(): Remove extra space from docstring guts.
Tim Peters <tim.peters@gmail.com>
parents:
23075
diff
changeset
|
95 """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
|
96 filters[:] = [] |
|
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 class _OptionError(Exception): |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 """Exception used by option processing helpers.""" |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 pass |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 # 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
|
103 def _processoptions(args): |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 for arg in args: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 try: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 _setoption(arg) |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 except _OptionError, msg: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 print >>sys.stderr, "Invalid -W option ignored:", msg |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 # Helper for _processoptions() |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 def _setoption(arg): |
|
29114
4b0220260f62
defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents:
27958
diff
changeset
|
112 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
|
113 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
|
114 if len(parts) > 5: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 try: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15853
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 except (ValueError, OverflowError): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
132 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
|
133 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15853
diff
changeset
|
134 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
|
135 filterwarnings(action, message, category, module, lineno) |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 # Helper for _setoption() |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 def _getaction(action): |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 if not action: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 return "default" |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 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
|
142 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
|
143 if a.startswith(action): |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 return a |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
145 raise _OptionError("invalid action: %r" % (action,)) |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 # Helper for _setoption() |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 def _getcategory(category): |
|
29114
4b0220260f62
defer re module imports to help improve interpreter startup
Skip Montanaro <skip@pobox.com>
parents:
27958
diff
changeset
|
149 import re |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 if not category: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 return Warning |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 if re.match("^[a-zA-Z0-9_]+$", category): |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 try: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 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
|
155 except NameError: |
|
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,)) |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 else: |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 i = category.rfind(".") |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 module = category[:i] |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 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
|
161 try: |
|
2fe7427591ea
Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents:
15592
diff
changeset
|
162 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
|
163 except ImportError: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
164 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
|
165 try: |
|
2fe7427591ea
Improve error messages for invalid warning arguments; don't raise
Guido van Rossum <guido@python.org>
parents:
15592
diff
changeset
|
166 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
|
167 except AttributeError: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
168 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
|
169 if not issubclass(cat, Warning): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29813
diff
changeset
|
170 raise _OptionError("invalid warning category: %r" % (category,)) |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 return cat |
|
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
173 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
174 # 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
|
175 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
|
176 """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
|
177 # 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
|
178 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
|
179 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
|
180 # 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
|
181 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
|
182 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
|
183 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
|
184 # 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
|
185 try: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
186 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
|
187 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
|
188 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
|
189 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
|
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 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
|
192 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
|
193 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
|
194 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
|
195 else: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 else: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
203 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
|
204 try: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
205 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
|
206 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
|
207 # 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 globals) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
214 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 registry = {} |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
224 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
|
225 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
|
226 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
|
227 else: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 # 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
|
232 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
|
233 return |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
234 # 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 (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
|
240 (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
|
241 break |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
242 else: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
243 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
|
244 # 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
|
245 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
|
246 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
|
247 return |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
248 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
249 # 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
|
250 # "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
|
251 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
|
252 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
253 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
|
254 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
|
255 # 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 return |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
261 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
|
262 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
|
263 pass |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 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
|
268 return |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 else: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
273 # 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
|
274 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
|
275 "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
|
276 (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
|
277 # 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
|
278 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
|
279 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
280 |
|
48948
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
281 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
|
282 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
283 """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
|
284 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
285 _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
|
286 "line") |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
287 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
288 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
|
289 line=None): |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 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
|
294 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
295 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
|
296 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
|
297 "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
|
298 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
|
299 |
|
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 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
|
302 |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
49028
diff
changeset
|
303 """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
|
304 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
|
305 |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
49028
diff
changeset
|
306 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
|
307 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
|
308 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
|
309 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
|
310 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
|
311 |
|
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 '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
|
313 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
|
314 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
|
315 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
316 """ |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
317 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
318 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
|
319 """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
|
320 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
|
321 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
322 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
|
323 keyword-only. |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
324 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
325 """ |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
49028
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 |
|
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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 log = [] |
|
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
49028
diff
changeset
|
348 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
|
349 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
|
350 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
|
351 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
|
352 else: |
|
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
49028
diff
changeset
|
353 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
|
354 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
355 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
|
356 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
|
357 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
|
358 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
|
359 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
|
360 |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
47835
diff
changeset
|
361 |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
362 # 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
|
363 # 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
|
364 # - 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
|
365 # - 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
|
366 # - 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
|
367 # - 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
|
368 # - 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
|
369 # 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
|
370 _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
|
371 try: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
372 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
|
373 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
|
374 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
|
375 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
|
376 _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
|
377 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
|
378 filters = [] |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
379 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
|
380 onceregistry = {} |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
381 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
382 |
|
15592
52756f613c18
Python part of the warnings subsystem.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 # Module initialization |
|
31737
57965b2db20c
SF bug 917108: warnings.py does not define _test().
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
384 _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
|
385 if not _warnings_defaults: |
|
58331
d3bea1669f2d
The silencing of DeprecationWarning was not taking -3 into consideration. Since
Brett Cannon <bcannon@gmail.com>
parents:
58231
diff
changeset
|
386 silence = [ImportWarning, PendingDeprecationWarning] |
|
60973
8c7179125803
When DeprecationWarning was silenced by default, it also silenced any use of -Q
Brett Cannon <bcannon@gmail.com>
parents:
59431
diff
changeset
|
387 # Don't silence DeprecationWarning if -3 or -Q was used. |
|
8c7179125803
When DeprecationWarning was silenced by default, it also silenced any use of -Q
Brett Cannon <bcannon@gmail.com>
parents:
59431
diff
changeset
|
388 if not sys.py3kwarning and not sys.flags.division_warning: |
|
58331
d3bea1669f2d
The silencing of DeprecationWarning was not taking -3 into consideration. Since
Brett Cannon <bcannon@gmail.com>
parents:
58231
diff
changeset
|
389 silence.append(DeprecationWarning) |
|
d3bea1669f2d
The silencing of DeprecationWarning was not taking -3 into consideration. Since
Brett Cannon <bcannon@gmail.com>
parents:
58231
diff
changeset
|
390 for cls in silence: |
|
d3bea1669f2d
The silencing of DeprecationWarning was not taking -3 into consideration. Since
Brett Cannon <bcannon@gmail.com>
parents:
58231
diff
changeset
|
391 simplefilter("ignore", category=cls) |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 else: |
|
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 = "ignore" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44038
diff
changeset
|
399 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
|
400 del _warnings_defaults |
