warnings.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. """Python part of the warnings subsystem."""
  2. # Note: function level imports should *not* be used
  3. # in this module as it may cause import lock deadlock.
  4. # See bug 683658.
  5. import linecache
  6. import sys
  7. import types
  8. __all__ = ["warn", "warn_explicit", "showwarning",
  9. "formatwarning", "filterwarnings", "simplefilter",
  10. "resetwarnings", "catch_warnings"]
  11. def warnpy3k(message, category=None, stacklevel=1):
  12. """Issue a deprecation warning for Python 3.x related changes.
  13. Warnings are omitted unless Python is started with the -3 option.
  14. """
  15. if sys.py3kwarning:
  16. if category is None:
  17. category = DeprecationWarning
  18. warn(message, category, stacklevel+1)
  19. def _show_warning(message, category, filename, lineno, file=None, line=None):
  20. """Hook to write a warning to a file; replace if you like."""
  21. if file is None:
  22. file = sys.stderr
  23. if file is None:
  24. # sys.stderr is None - warnings get lost
  25. return
  26. try:
  27. file.write(formatwarning(message, category, filename, lineno, line))
  28. except (IOError, UnicodeError):
  29. pass # the file (probably stderr) is invalid - this warning gets lost.
  30. # Keep a working version around in case the deprecation of the old API is
  31. # triggered.
  32. showwarning = _show_warning
  33. def formatwarning(message, category, filename, lineno, line=None):
  34. """Function to format a warning the standard way."""
  35. try:
  36. unicodetype = unicode
  37. except NameError:
  38. unicodetype = ()
  39. try:
  40. message = str(message)
  41. except UnicodeEncodeError:
  42. pass
  43. s = "%s: %s: %s\n" % (lineno, category.__name__, message)
  44. line = linecache.getline(filename, lineno) if line is None else line
  45. if line:
  46. line = line.strip()
  47. if isinstance(s, unicodetype) and isinstance(line, str):
  48. line = unicode(line, 'latin1')
  49. s += " %s\n" % line
  50. if isinstance(s, unicodetype) and isinstance(filename, str):
  51. enc = sys.getfilesystemencoding()
  52. if enc:
  53. try:
  54. filename = unicode(filename, enc)
  55. except UnicodeDecodeError:
  56. pass
  57. s = "%s:%s" % (filename, s)
  58. return s
  59. def filterwarnings(action, message="", category=Warning, module="", lineno=0,
  60. append=0):
  61. """Insert an entry into the list of warnings filters (at the front).
  62. 'action' -- one of "error", "ignore", "always", "default", "module",
  63. or "once"
  64. 'message' -- a regex that the warning message must match
  65. 'category' -- a class that the warning must be a subclass of
  66. 'module' -- a regex that the module name must match
  67. 'lineno' -- an integer line number, 0 matches all warnings
  68. 'append' -- if true, append to the list of filters
  69. """
  70. import re
  71. assert action in ("error", "ignore", "always", "default", "module",
  72. "once"), "invalid action: %r" % (action,)
  73. assert isinstance(message, basestring), "message must be a string"
  74. assert isinstance(category, (type, types.ClassType)), \
  75. "category must be a class"
  76. assert issubclass(category, Warning), "category must be a Warning subclass"
  77. assert isinstance(module, basestring), "module must be a string"
  78. assert isinstance(lineno, int) and lineno >= 0, \
  79. "lineno must be an int >= 0"
  80. item = (action, re.compile(message, re.I), category,
  81. re.compile(module), lineno)
  82. if append:
  83. filters.append(item)
  84. else:
  85. filters.insert(0, item)
  86. def simplefilter(action, category=Warning, lineno=0, append=0):
  87. """Insert a simple entry into the list of warnings filters (at the front).
  88. A simple filter matches all modules and messages.
  89. 'action' -- one of "error", "ignore", "always", "default", "module",
  90. or "once"
  91. 'category' -- a class that the warning must be a subclass of
  92. 'lineno' -- an integer line number, 0 matches all warnings
  93. 'append' -- if true, append to the list of filters
  94. """
  95. assert action in ("error", "ignore", "always", "default", "module",
  96. "once"), "invalid action: %r" % (action,)
  97. assert isinstance(lineno, int) and lineno >= 0, \
  98. "lineno must be an int >= 0"
  99. item = (action, None, category, None, lineno)
  100. if append:
  101. filters.append(item)
  102. else:
  103. filters.insert(0, item)
  104. def resetwarnings():
  105. """Clear the list of warning filters, so that no filters are active."""
  106. filters[:] = []
  107. class _OptionError(Exception):
  108. """Exception used by option processing helpers."""
  109. pass
  110. # Helper to process -W options passed via sys.warnoptions
  111. def _processoptions(args):
  112. for arg in args:
  113. try:
  114. _setoption(arg)
  115. except _OptionError, msg:
  116. print >>sys.stderr, "Invalid -W option ignored:", msg
  117. # Helper for _processoptions()
  118. def _setoption(arg):
  119. import re
  120. parts = arg.split(':')
  121. if len(parts) > 5:
  122. raise _OptionError("too many fields (max 5): %r" % (arg,))
  123. while len(parts) < 5:
  124. parts.append('')
  125. action, message, category, module, lineno = [s.strip()
  126. for s in parts]
  127. action = _getaction(action)
  128. message = re.escape(message)
  129. category = _getcategory(category)
  130. module = re.escape(module)
  131. if module:
  132. module = module + '$'
  133. if lineno:
  134. try:
  135. lineno = int(lineno)
  136. if lineno < 0:
  137. raise ValueError
  138. except (ValueError, OverflowError):
  139. raise _OptionError("invalid lineno %r" % (lineno,))
  140. else:
  141. lineno = 0
  142. filterwarnings(action, message, category, module, lineno)
  143. # Helper for _setoption()
  144. def _getaction(action):
  145. if not action:
  146. return "default"
  147. if action == "all": return "always" # Alias
  148. for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
  149. if a.startswith(action):
  150. return a
  151. raise _OptionError("invalid action: %r" % (action,))
  152. # Helper for _setoption()
  153. def _getcategory(category):
  154. import re
  155. if not category:
  156. return Warning
  157. if re.match("^[a-zA-Z0-9_]+$", category):
  158. try:
  159. cat = eval(category)
  160. except NameError:
  161. raise _OptionError("unknown warning category: %r" % (category,))
  162. else:
  163. i = category.rfind(".")
  164. module = category[:i]
  165. klass = category[i+1:]
  166. try:
  167. m = __import__(module, None, None, [klass])
  168. except ImportError:
  169. raise _OptionError("invalid module name: %r" % (module,))
  170. try:
  171. cat = getattr(m, klass)
  172. except AttributeError:
  173. raise _OptionError("unknown warning category: %r" % (category,))
  174. if not issubclass(cat, Warning):
  175. raise _OptionError("invalid warning category: %r" % (category,))
  176. return cat
  177. # Code typically replaced by _warnings
  178. def warn(message, category=None, stacklevel=1):
  179. """Issue a warning, or maybe ignore it or raise an exception."""
  180. # Check if message is already a Warning object
  181. if isinstance(message, Warning):
  182. category = message.__class__
  183. # Check category argument
  184. if category is None:
  185. category = UserWarning
  186. assert issubclass(category, Warning)
  187. # Get context information
  188. try:
  189. caller = sys._getframe(stacklevel)
  190. except ValueError:
  191. globals = sys.__dict__
  192. lineno = 1
  193. else:
  194. globals = caller.f_globals
  195. lineno = caller.f_lineno
  196. if '__name__' in globals:
  197. module = globals['__name__']
  198. else:
  199. module = "<string>"
  200. filename = globals.get('__file__')
  201. if filename:
  202. fnl = filename.lower()
  203. if fnl.endswith((".pyc", ".pyo")):
  204. filename = filename[:-1]
  205. else:
  206. if module == "__main__":
  207. try:
  208. filename = sys.argv[0]
  209. except AttributeError:
  210. # embedded interpreters don't have sys.argv, see bug #839151
  211. filename = '__main__'
  212. if not filename:
  213. filename = module
  214. registry = globals.setdefault("__warningregistry__", {})
  215. warn_explicit(message, category, filename, lineno, module, registry,
  216. globals)
  217. def warn_explicit(message, category, filename, lineno,
  218. module=None, registry=None, module_globals=None):
  219. lineno = int(lineno)
  220. if module is None:
  221. module = filename or "<unknown>"
  222. if module[-3:].lower() == ".py":
  223. module = module[:-3] # XXX What about leading pathname?
  224. if registry is None:
  225. registry = {}
  226. if isinstance(message, Warning):
  227. text = str(message)
  228. category = message.__class__
  229. else:
  230. text = message
  231. message = category(message)
  232. key = (text, category, lineno)
  233. # Quick test for common case
  234. if registry.get(key):
  235. return
  236. # Search the filters
  237. for item in filters:
  238. action, msg, cat, mod, ln = item
  239. if ((msg is None or msg.match(text)) and
  240. issubclass(category, cat) and
  241. (mod is None or mod.match(module)) and
  242. (ln == 0 or lineno == ln)):
  243. break
  244. else:
  245. action = defaultaction
  246. # Early exit actions
  247. if action == "ignore":
  248. registry[key] = 1
  249. return
  250. # Prime the linecache for formatting, in case the
  251. # "file" is actually in a zipfile or something.
  252. linecache.getlines(filename, module_globals)
  253. if action == "error":
  254. raise message
  255. # Other actions
  256. if action == "once":
  257. registry[key] = 1
  258. oncekey = (text, category)
  259. if onceregistry.get(oncekey):
  260. return
  261. onceregistry[oncekey] = 1
  262. elif action == "always":
  263. pass
  264. elif action == "module":
  265. registry[key] = 1
  266. altkey = (text, category, 0)
  267. if registry.get(altkey):
  268. return
  269. registry[altkey] = 1
  270. elif action == "default":
  271. registry[key] = 1
  272. else:
  273. # Unrecognized actions are errors
  274. raise RuntimeError(
  275. "Unrecognized action (%r) in warnings.filters:\n %s" %
  276. (action, item))
  277. # Print message and context
  278. showwarning(message, category, filename, lineno)
  279. class WarningMessage(object):
  280. """Holds the result of a single showwarning() call."""
  281. _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
  282. "line")
  283. def __init__(self, message, category, filename, lineno, file=None,
  284. line=None):
  285. local_values = locals()
  286. for attr in self._WARNING_DETAILS:
  287. setattr(self, attr, local_values[attr])
  288. self._category_name = category.__name__ if category else None
  289. def __str__(self):
  290. return ("{message : %r, category : %r, filename : %r, lineno : %s, "
  291. "line : %r}" % (self.message, self._category_name,
  292. self.filename, self.lineno, self.line))
  293. class catch_warnings(object):
  294. """A context manager that copies and restores the warnings filter upon
  295. exiting the context.
  296. The 'record' argument specifies whether warnings should be captured by a
  297. custom implementation of warnings.showwarning() and be appended to a list
  298. returned by the context manager. Otherwise None is returned by the context
  299. manager. The objects appended to the list are arguments whose attributes
  300. mirror the arguments to showwarning().
  301. The 'module' argument is to specify an alternative module to the module
  302. named 'warnings' and imported under that name. This argument is only useful
  303. when testing the warnings module itself.
  304. """
  305. def __init__(self, record=False, module=None):
  306. """Specify whether to record warnings and if an alternative module
  307. should be used other than sys.modules['warnings'].
  308. For compatibility with Python 3.0, please consider all arguments to be
  309. keyword-only.
  310. """
  311. self._record = record
  312. self._module = sys.modules['warnings'] if module is None else module
  313. self._entered = False
  314. def __repr__(self):
  315. args = []
  316. if self._record:
  317. args.append("record=True")
  318. if self._module is not sys.modules['warnings']:
  319. args.append("module=%r" % self._module)
  320. name = type(self).__name__
  321. return "%s(%s)" % (name, ", ".join(args))
  322. def __enter__(self):
  323. if self._entered:
  324. raise RuntimeError("Cannot enter %r twice" % self)
  325. self._entered = True
  326. self._filters = self._module.filters
  327. self._module.filters = self._filters[:]
  328. self._showwarning = self._module.showwarning
  329. if self._record:
  330. log = []
  331. def showwarning(*args, **kwargs):
  332. log.append(WarningMessage(*args, **kwargs))
  333. self._module.showwarning = showwarning
  334. return log
  335. else:
  336. return None
  337. def __exit__(self, *exc_info):
  338. if not self._entered:
  339. raise RuntimeError("Cannot exit %r without entering first" % self)
  340. self._module.filters = self._filters
  341. self._module.showwarning = self._showwarning
  342. # filters contains a sequence of filter 5-tuples
  343. # The components of the 5-tuple are:
  344. # - an action: error, ignore, always, default, module, or once
  345. # - a compiled regex that must match the warning message
  346. # - a class representing the warning category
  347. # - a compiled regex that must match the module that is being warned
  348. # - a line number for the line being warning, or 0 to mean any line
  349. # If either if the compiled regexs are None, match anything.
  350. _warnings_defaults = False
  351. try:
  352. from _warnings import (filters, default_action, once_registry,
  353. warn, warn_explicit)
  354. defaultaction = default_action
  355. onceregistry = once_registry
  356. _warnings_defaults = True
  357. except ImportError:
  358. filters = []
  359. defaultaction = "default"
  360. onceregistry = {}
  361. # Module initialization
  362. _processoptions(sys.warnoptions)
  363. if not _warnings_defaults:
  364. silence = [ImportWarning, PendingDeprecationWarning]
  365. # Don't silence DeprecationWarning if -3 or -Q was used.
  366. if not sys.py3kwarning and not sys.flags.division_warning:
  367. silence.append(DeprecationWarning)
  368. for cls in silence:
  369. simplefilter("ignore", category=cls)
  370. bytes_warning = sys.flags.bytes_warning
  371. if bytes_warning > 1:
  372. bytes_action = "error"
  373. elif bytes_warning:
  374. bytes_action = "default"
  375. else:
  376. bytes_action = "ignore"
  377. simplefilter(bytes_action, category=BytesWarning, append=1)
  378. del _warnings_defaults