1 """Python part of the warnings subsystem."""
5 __all__ = [
"warn",
"showwarning",
"formatwarning",
"filterwarnings",
8 defaultaction =
"default"
12 def warn(message, category=None, stacklevel=1):
13 """Issue a warning, or maybe ignore it or raise an exception."""
16 category = UserWarning
17 assert issubclass(category, Warning)
20 caller = sys._getframe(stacklevel)
22 globals = sys.__dict__
25 globals = caller.f_globals
26 lineno = caller.f_lineno
27 if globals.has_key(
'__name__'):
28 module = globals[
'__name__']
31 filename = globals.get(
'__file__')
33 fnl = filename.lower()
34 if fnl.endswith(
".pyc")
or fnl.endswith(
".pyo"):
35 filename = filename[:-1]
37 if module ==
"__main__":
38 filename = sys.argv[0]
41 registry = globals.setdefault(
"__warningregistry__", {})
42 warn_explicit(message, category, filename, lineno, module, registry)
45 module=
None, registry=
None):
48 if module[-3:].
lower() ==
".py":
52 key = (message, category, lineno)
58 action, msg, cat, mod, ln = item
59 if (msg.match(message)
and
60 issubclass(category, cat)
and
62 (ln == 0
or lineno == ln)):
65 action = defaultaction
67 if action ==
"ignore":
71 raise category(message)
75 oncekey = (message, category)
76 if onceregistry.get(oncekey):
78 onceregistry[oncekey] = 1
79 elif action ==
"always":
81 elif action ==
"module":
83 altkey = (message, category, 0)
84 if registry.get(altkey):
87 elif action ==
"default":
92 "Unrecognized action (%s) in warnings.filters:\n %s" %
93 (`action`,
str(item)))
97 def showwarning(message, category, filename, lineno, file=None):
98 """Hook to write a warning to a file; replace if you like."""
101 file.write(
formatwarning(message, category, filename, lineno))
104 """Function to format a warning the standard way."""
106 s =
"%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
109 s = s +
" " + line +
"\n"
112 def filterwarnings(action, message="", category=Warning, module="", lineno=0,
114 """Insert an entry into the list of warnings filters (at the front).
116 Use assertions to check that all arguments have the right type."""
133 """Reset the list of warnings filters to its default state."""
137 """Exception used by option processing helpers."""
141 def _processoptions(args):
145 except _OptionError, msg:
146 print >>sys.stderr,
"Invalid -W option ignored:", msg
150 parts = arg.split(
':')
152 raise _OptionError(
"too many fields (max 5): %s" % `arg`)
153 while len(parts) < 5:
155 action, message, category, module, lineno = [s.strip()
157 action = _getaction(action)
158 message = re.escape(message)
159 category = _getcategory(category)
160 module = re.escape(module)
162 module = module +
'$'
168 except (ValueError, OverflowError):
169 raise _OptionError(
"invalid lineno %s" % `lineno`)
175 def _getaction(action):
178 if action ==
"all":
return "always"
179 for a
in [
'default',
'always',
'ignore',
'module',
'once',
'error']:
180 if a.startswith(action):
182 raise _OptionError(
"invalid action: %s" % `action`)
185 def _getcategory(category):
188 if re.match(
"^[a-zA-Z0-9_]+$", category):
192 raise _OptionError(
"unknown warning category: %s" % `category`)
194 i = category.rfind(
".")
195 module = category[:i]
196 klass = category[i+1:]
198 m = __import__(module,
None,
None, [klass])
200 raise _OptionError(
"invalid module name: %s" % `module`)
202 cat = getattr(m, klass)
203 except AttributeError:
204 raise _OptionError(
"unknown warning category: %s" % `category`)
205 if (
not isinstance(cat, types.ClassType)
or
206 not issubclass(cat, Warning)):
207 raise _OptionError(
"invalid warning category: %s" % `category`)
216 except getopt.error, msg:
217 print >>sys.stderr, msg
220 testoptions.append(a)
222 _processoptions(testoptions)
223 except _OptionError, msg:
224 print >>sys.stderr, msg
226 for item
in filters:
print item
227 hello =
"hello world"
229 warn(hello, UserWarning)
230 warn(hello, DeprecationWarning)
236 except Exception, msg:
237 print "Caught", msg.__class__.__name__ +
":", msg
243 except Exception, msg:
244 print "Caught", msg.__class__.__name__ +
":", msg
249 if __name__ ==
"__main__":
251 sys.modules[
'warnings'] = __main__
254 _processoptions(sys.warnoptions)