blob: bcd702c99377b55355bc09ca4f5178eb42f39db5 [file] [log] [blame]
Guido van Rossum2a862c62000-12-15 21:59:53 +00001"""Python part of the warnings subsystem."""
2
Mark Hammonda43fd0c2003-02-19 00:33:33 +00003# Note: function level imports should *not* be used
4# in this module as it may cause import lock deadlock.
5# See bug 683658.
Mark Hammonda43fd0c2003-02-19 00:33:33 +00006import linecache
Christian Heimes33fe8092008-04-13 13:53:33 +00007import sys
8import types
Guido van Rossum2a862c62000-12-15 21:59:53 +00009
Skip Montanaro40fc1602001-03-01 04:27:19 +000010__all__ = ["warn", "showwarning", "formatwarning", "filterwarnings",
11 "resetwarnings"]
12
Guido van Rossum2a862c62000-12-15 21:59:53 +000013
Christian Heimes33fe8092008-04-13 13:53:33 +000014def showwarning(message, category, filename, lineno, file=None, line=None):
Guido van Rossum2a862c62000-12-15 21:59:53 +000015 """Hook to write a warning to a file; replace if you like."""
16 if file is None:
17 file = sys.stderr
Mark Hammond51a0ae32002-09-11 13:22:35 +000018 try:
Christian Heimes33fe8092008-04-13 13:53:33 +000019 file.write(formatwarning(message, category, filename, lineno, line))
Mark Hammond51a0ae32002-09-11 13:22:35 +000020 except IOError:
21 pass # the file (probably stderr) is invalid - this warning gets lost.
Guido van Rossum2a862c62000-12-15 21:59:53 +000022
Christian Heimes33fe8092008-04-13 13:53:33 +000023def formatwarning(message, category, filename, lineno, line=None):
Guido van Rossum9464a7d2001-01-14 14:08:40 +000024 """Function to format a warning the standard way."""
Guido van Rossum2a862c62000-12-15 21:59:53 +000025 s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
Christian Heimes33fe8092008-04-13 13:53:33 +000026 line = linecache.getline(filename, lineno) if line is None else line
Guido van Rossum2a862c62000-12-15 21:59:53 +000027 if line:
Christian Heimes33fe8092008-04-13 13:53:33 +000028 line = line.strip()
29 s += " %s\n" % line
Guido van Rossum2a862c62000-12-15 21:59:53 +000030 return s
31
Guido van Rossum9464a7d2001-01-14 14:08:40 +000032def filterwarnings(action, message="", category=Warning, module="", lineno=0,
33 append=0):
Guido van Rossum2a862c62000-12-15 21:59:53 +000034 """Insert an entry into the list of warnings filters (at the front).
35
36 Use assertions to check that all arguments have the right type."""
Skip Montanarod8f21202003-05-14 17:33:53 +000037 import re
Guido van Rossum2a862c62000-12-15 21:59:53 +000038 assert action in ("error", "ignore", "always", "default", "module",
Walter Dörwald70a6b492004-02-12 17:35:32 +000039 "once"), "invalid action: %r" % (action,)
Guido van Rossum3172c5d2007-10-16 18:12:55 +000040 assert isinstance(message, str), "message must be a string"
Guido van Rossum13257902007-06-07 23:15:56 +000041 assert isinstance(category, type), "category must be a class"
Guido van Rossum2a862c62000-12-15 21:59:53 +000042 assert issubclass(category, Warning), "category must be a Warning subclass"
Guido van Rossum3172c5d2007-10-16 18:12:55 +000043 assert isinstance(module, str), "module must be a string"
Walter Dörwald65230a22002-06-03 15:58:32 +000044 assert isinstance(lineno, int) and lineno >= 0, \
Guido van Rossum2a862c62000-12-15 21:59:53 +000045 "lineno must be an int >= 0"
Guido van Rossum9464a7d2001-01-14 14:08:40 +000046 item = (action, re.compile(message, re.I), category,
47 re.compile(module), lineno)
48 if append:
49 filters.append(item)
50 else:
51 filters.insert(0, item)
Guido van Rossum2a862c62000-12-15 21:59:53 +000052
Jeremy Hylton85014662003-07-11 15:37:59 +000053def simplefilter(action, category=Warning, lineno=0, append=0):
54 """Insert a simple entry into the list of warnings filters (at the front).
55
56 A simple filter matches all modules and messages.
57 """
58 assert action in ("error", "ignore", "always", "default", "module",
Walter Dörwald70a6b492004-02-12 17:35:32 +000059 "once"), "invalid action: %r" % (action,)
Jeremy Hylton85014662003-07-11 15:37:59 +000060 assert isinstance(lineno, int) and lineno >= 0, \
61 "lineno must be an int >= 0"
62 item = (action, None, category, None, lineno)
63 if append:
64 filters.append(item)
65 else:
66 filters.insert(0, item)
67
Guido van Rossum2a862c62000-12-15 21:59:53 +000068def resetwarnings():
Tim Petersd0cc4f02002-04-16 01:51:25 +000069 """Clear the list of warning filters, so that no filters are active."""
Guido van Rossum2a862c62000-12-15 21:59:53 +000070 filters[:] = []
71
72class _OptionError(Exception):
73 """Exception used by option processing helpers."""
74 pass
75
76# Helper to process -W options passed via sys.warnoptions
77def _processoptions(args):
78 for arg in args:
79 try:
80 _setoption(arg)
Guido van Rossumb940e112007-01-10 16:19:56 +000081 except _OptionError as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000082 print("Invalid -W option ignored:", msg, file=sys.stderr)
Guido van Rossum2a862c62000-12-15 21:59:53 +000083
84# Helper for _processoptions()
85def _setoption(arg):
Skip Montanarod8f21202003-05-14 17:33:53 +000086 import re
Tim Peterse1190062001-01-15 03:34:38 +000087 parts = arg.split(':')
88 if len(parts) > 5:
Walter Dörwald70a6b492004-02-12 17:35:32 +000089 raise _OptionError("too many fields (max 5): %r" % (arg,))
Tim Peterse1190062001-01-15 03:34:38 +000090 while len(parts) < 5:
91 parts.append('')
92 action, message, category, module, lineno = [s.strip()
93 for s in parts]
94 action = _getaction(action)
95 message = re.escape(message)
96 category = _getcategory(category)
97 module = re.escape(module)
98 if module:
99 module = module + '$'
100 if lineno:
101 try:
102 lineno = int(lineno)
103 if lineno < 0:
104 raise ValueError
105 except (ValueError, OverflowError):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000106 raise _OptionError("invalid lineno %r" % (lineno,))
Tim Peterse1190062001-01-15 03:34:38 +0000107 else:
108 lineno = 0
109 filterwarnings(action, message, category, module, lineno)
Guido van Rossum2a862c62000-12-15 21:59:53 +0000110
111# Helper for _setoption()
112def _getaction(action):
113 if not action:
114 return "default"
115 if action == "all": return "always" # Alias
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000116 for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
Guido van Rossum2a862c62000-12-15 21:59:53 +0000117 if a.startswith(action):
118 return a
Walter Dörwald70a6b492004-02-12 17:35:32 +0000119 raise _OptionError("invalid action: %r" % (action,))
Guido van Rossum2a862c62000-12-15 21:59:53 +0000120
121# Helper for _setoption()
122def _getcategory(category):
Skip Montanarod8f21202003-05-14 17:33:53 +0000123 import re
Guido van Rossum2a862c62000-12-15 21:59:53 +0000124 if not category:
125 return Warning
126 if re.match("^[a-zA-Z0-9_]+$", category):
127 try:
128 cat = eval(category)
Guido van Rossumd1db30b2000-12-19 03:04:50 +0000129 except NameError:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000130 raise _OptionError("unknown warning category: %r" % (category,))
Guido van Rossum2a862c62000-12-15 21:59:53 +0000131 else:
132 i = category.rfind(".")
133 module = category[:i]
134 klass = category[i+1:]
Guido van Rossumd1db30b2000-12-19 03:04:50 +0000135 try:
136 m = __import__(module, None, None, [klass])
137 except ImportError:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000138 raise _OptionError("invalid module name: %r" % (module,))
Guido van Rossumd1db30b2000-12-19 03:04:50 +0000139 try:
140 cat = getattr(m, klass)
141 except AttributeError:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000142 raise _OptionError("unknown warning category: %r" % (category,))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 if not issubclass(cat, Warning):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000144 raise _OptionError("invalid warning category: %r" % (category,))
Guido van Rossum2a862c62000-12-15 21:59:53 +0000145 return cat
146
Christian Heimes33fe8092008-04-13 13:53:33 +0000147
148# Code typically replaced by _warnings
149def warn(message, category=None, stacklevel=1):
150 """Issue a warning, or maybe ignore it or raise an exception."""
151 # Check if message is already a Warning object
152 if isinstance(message, Warning):
153 category = message.__class__
154 # Check category argument
155 if category is None:
156 category = UserWarning
157 assert issubclass(category, Warning)
158 # Get context information
159 try:
160 caller = sys._getframe(stacklevel)
161 except ValueError:
162 globals = sys.__dict__
163 lineno = 1
164 else:
165 globals = caller.f_globals
166 lineno = caller.f_lineno
167 if '__name__' in globals:
168 module = globals['__name__']
169 else:
170 module = "<string>"
171 filename = globals.get('__file__')
172 if filename:
173 fnl = filename.lower()
174 if fnl.endswith((".pyc", ".pyo")):
175 filename = filename[:-1]
176 else:
177 if module == "__main__":
178 try:
179 filename = sys.argv[0]
180 except AttributeError:
181 # embedded interpreters don't have sys.argv, see bug #839151
182 filename = '__main__'
183 if not filename:
184 filename = module
185 registry = globals.setdefault("__warningregistry__", {})
186 warn_explicit(message, category, filename, lineno, module, registry,
187 globals)
188
189def warn_explicit(message, category, filename, lineno,
190 module=None, registry=None, module_globals=None):
Brett Cannondb734912008-06-27 00:52:15 +0000191 lineno = int(lineno)
Christian Heimes33fe8092008-04-13 13:53:33 +0000192 if module is None:
193 module = filename or "<unknown>"
194 if module[-3:].lower() == ".py":
195 module = module[:-3] # XXX What about leading pathname?
196 if registry is None:
197 registry = {}
198 if isinstance(message, Warning):
199 text = str(message)
200 category = message.__class__
201 else:
202 text = message
203 message = category(message)
204 key = (text, category, lineno)
205 # Quick test for common case
206 if registry.get(key):
207 return
208 # Search the filters
209 for item in filters:
210 action, msg, cat, mod, ln = item
211 if ((msg is None or msg.match(text)) and
212 issubclass(category, cat) and
213 (mod is None or mod.match(module)) and
214 (ln == 0 or lineno == ln)):
215 break
216 else:
217 action = defaultaction
218 # Early exit actions
219 if action == "ignore":
220 registry[key] = 1
221 return
222
223 # Prime the linecache for formatting, in case the
224 # "file" is actually in a zipfile or something.
225 linecache.getlines(filename, module_globals)
226
227 if action == "error":
228 raise message
229 # Other actions
230 if action == "once":
231 registry[key] = 1
232 oncekey = (text, category)
233 if onceregistry.get(oncekey):
234 return
235 onceregistry[oncekey] = 1
236 elif action == "always":
237 pass
238 elif action == "module":
239 registry[key] = 1
240 altkey = (text, category, 0)
241 if registry.get(altkey):
242 return
243 registry[altkey] = 1
244 elif action == "default":
245 registry[key] = 1
246 else:
247 # Unrecognized actions are errors
248 raise RuntimeError(
249 "Unrecognized action (%r) in warnings.filters:\n %s" %
250 (action, item))
Christian Heimes8dc226f2008-05-06 23:45:46 +0000251 if not hasattr(showwarning, "__call__"):
252 raise TypeError("warnings.showwarning() must be set to a "
253 "function or method")
Christian Heimes33fe8092008-04-13 13:53:33 +0000254 # Print message and context
255 showwarning(message, category, filename, lineno)
256
257
258# filters contains a sequence of filter 5-tuples
259# The components of the 5-tuple are:
260# - an action: error, ignore, always, default, module, or once
261# - a compiled regex that must match the warning message
262# - a class representing the warning category
263# - a compiled regex that must match the module that is being warned
264# - a line number for the line being warning, or 0 to mean any line
265# If either if the compiled regexs are None, match anything.
266_warnings_defaults = False
267try:
268 from _warnings import (filters, default_action, once_registry,
269 warn, warn_explicit)
270 defaultaction = default_action
271 onceregistry = once_registry
272 _warnings_defaults = True
273except ImportError:
274 filters = []
275 defaultaction = "default"
276 onceregistry = {}
277
278
Guido van Rossum2a862c62000-12-15 21:59:53 +0000279# Module initialization
Tim Peters66025202004-03-21 17:06:20 +0000280_processoptions(sys.warnoptions)
Christian Heimes33fe8092008-04-13 13:53:33 +0000281if not _warnings_defaults:
282 simplefilter("ignore", category=PendingDeprecationWarning, append=1)
283 simplefilter("ignore", category=ImportWarning, append=1)
284 bytes_warning = sys.flags.bytes_warning
285 if bytes_warning > 1:
286 bytes_action = "error"
287 elif bytes_warning:
288 bytes_action = "default"
289 else:
290 bytes_action = "ignore"
291 simplefilter(bytes_action, category=BytesWarning, append=1)
292del _warnings_defaults