warnings.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. """Python part of the warnings subsystem."""
  2. import sys
  3. __all__ = ["warn", "warn_explicit", "showwarning",
  4. "formatwarning", "filterwarnings", "simplefilter",
  5. "resetwarnings", "catch_warnings"]
  6. def showwarning(message, category, filename, lineno, file=None, line=None):
  7. """Hook to write a warning to a file; replace if you like."""
  8. if file is None:
  9. file = sys.stderr
  10. if file is None:
  11. # sys.stderr is None when run with pythonw.exe - warnings get lost
  12. return
  13. try:
  14. file.write(formatwarning(message, category, filename, lineno, line))
  15. except OSError:
  16. pass # the file (probably stderr) is invalid - this warning gets lost.
  17. def formatwarning(message, category, filename, lineno, line=None):
  18. """Function to format a warning the standard way."""
  19. s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
  20. if line is None:
  21. try:
  22. import linecache
  23. line = linecache.getline(filename, lineno)
  24. except Exception:
  25. # When a warning is logged during Python shutdown, linecache
  26. # and the import machinery don't work anymore
  27. line = None
  28. if line:
  29. line = line.strip()
  30. s += " %s\n" % line
  31. return s
  32. def filterwarnings(action, message="", category=Warning, module="", lineno=0,
  33. append=False):
  34. """Insert an entry into the list of warnings filters (at the front).
  35. 'action' -- one of "error", "ignore", "always", "default", "module",
  36. or "once"
  37. 'message' -- a regex that the warning message must match
  38. 'category' -- a class that the warning must be a subclass of
  39. 'module' -- a regex that the module name must match
  40. 'lineno' -- an integer line number, 0 matches all warnings
  41. 'append' -- if true, append to the list of filters
  42. """
  43. import re
  44. assert action in ("error", "ignore", "always", "default", "module",
  45. "once"), "invalid action: %r" % (action,)
  46. assert isinstance(message, str), "message must be a string"
  47. assert isinstance(category, type), "category must be a class"
  48. assert issubclass(category, Warning), "category must be a Warning subclass"
  49. assert isinstance(module, str), "module must be a string"
  50. assert isinstance(lineno, int) and lineno >= 0, \
  51. "lineno must be an int >= 0"
  52. _add_filter(action, re.compile(message, re.I), category,
  53. re.compile(module), lineno, append=append)
  54. def simplefilter(action, category=Warning, lineno=0, append=False):
  55. """Insert a simple entry into the list of warnings filters (at the front).
  56. A simple filter matches all modules and messages.
  57. 'action' -- one of "error", "ignore", "always", "default", "module",
  58. or "once"
  59. 'category' -- a class that the warning must be a subclass of
  60. 'lineno' -- an integer line number, 0 matches all warnings
  61. 'append' -- if true, append to the list of filters
  62. """
  63. assert action in ("error", "ignore", "always", "default", "module",
  64. "once"), "invalid action: %r" % (action,)
  65. assert isinstance(lineno, int) and lineno >= 0, \
  66. "lineno must be an int >= 0"
  67. _add_filter(action, None, category, None, lineno, append=append)
  68. def _add_filter(*item, append):
  69. # Remove possible duplicate filters, so new one will be placed
  70. # in correct place. If append=True and duplicate exists, do nothing.
  71. if not append:
  72. try:
  73. filters.remove(item)
  74. except ValueError:
  75. pass
  76. filters.insert(0, item)
  77. else:
  78. if item not in filters:
  79. filters.append(item)
  80. _filters_mutated()
  81. def resetwarnings():
  82. """Clear the list of warning filters, so that no filters are active."""
  83. filters[:] = []
  84. _filters_mutated()
  85. class _OptionError(Exception):
  86. """Exception used by option processing helpers."""
  87. pass
  88. # Helper to process -W options passed via sys.warnoptions
  89. def _processoptions(args):
  90. for arg in args:
  91. try:
  92. _setoption(arg)
  93. except _OptionError as msg:
  94. print("Invalid -W option ignored:", msg, file=sys.stderr)
  95. # Helper for _processoptions()
  96. def _setoption(arg):
  97. import re
  98. parts = arg.split(':')
  99. if len(parts) > 5:
  100. raise _OptionError("too many fields (max 5): %r" % (arg,))
  101. while len(parts) < 5:
  102. parts.append('')
  103. action, message, category, module, lineno = [s.strip()
  104. for s in parts]
  105. action = _getaction(action)
  106. message = re.escape(message)
  107. category = _getcategory(category)
  108. module = re.escape(module)
  109. if module:
  110. module = module + '$'
  111. if lineno:
  112. try:
  113. lineno = int(lineno)
  114. if lineno < 0:
  115. raise ValueError
  116. except (ValueError, OverflowError):
  117. raise _OptionError("invalid lineno %r" % (lineno,))
  118. else:
  119. lineno = 0
  120. filterwarnings(action, message, category, module, lineno)
  121. # Helper for _setoption()
  122. def _getaction(action):
  123. if not action:
  124. return "default"
  125. if action == "all": return "always" # Alias
  126. for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
  127. if a.startswith(action):
  128. return a
  129. raise _OptionError("invalid action: %r" % (action,))
  130. # Helper for _setoption()
  131. def _getcategory(category):
  132. import re
  133. if not category:
  134. return Warning
  135. if re.match("^[a-zA-Z0-9_]+$", category):
  136. try:
  137. cat = eval(category)
  138. except NameError:
  139. raise _OptionError("unknown warning category: %r" % (category,))
  140. else:
  141. i = category.rfind(".")
  142. module = category[:i]
  143. klass = category[i+1:]
  144. try:
  145. m = __import__(module, None, None, [klass])
  146. except ImportError:
  147. raise _OptionError("invalid module name: %r" % (module,))
  148. try:
  149. cat = getattr(m, klass)
  150. except AttributeError:
  151. raise _OptionError("unknown warning category: %r" % (category,))
  152. if not issubclass(cat, Warning):
  153. raise _OptionError("invalid warning category: %r" % (category,))
  154. return cat
  155. def _is_internal_frame(frame):
  156. """Signal whether the frame is an internal CPython implementation detail."""
  157. filename = frame.f_code.co_filename
  158. return 'importlib' in filename and '_bootstrap' in filename
  159. def _next_external_frame(frame):
  160. """Find the next frame that doesn't involve CPython internals."""
  161. frame = frame.f_back
  162. while frame is not None and _is_internal_frame(frame):
  163. frame = frame.f_back
  164. return frame
  165. # Code typically replaced by _warnings
  166. def warn(message, category=None, stacklevel=1):
  167. """Issue a warning, or maybe ignore it or raise an exception."""
  168. # Check if message is already a Warning object
  169. if isinstance(message, Warning):
  170. category = message.__class__
  171. # Check category argument
  172. if category is None:
  173. category = UserWarning
  174. if not (isinstance(category, type) and issubclass(category, Warning)):
  175. raise TypeError("category must be a Warning subclass, "
  176. "not '{:s}'".format(type(category).__name__))
  177. # Get context information
  178. try:
  179. if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)):
  180. # If frame is too small to care or if the warning originated in
  181. # internal code, then do not try to hide any frames.
  182. frame = sys._getframe(stacklevel)
  183. else:
  184. frame = sys._getframe(1)
  185. # Look for one frame less since the above line starts us off.
  186. for x in range(stacklevel-1):
  187. frame = _next_external_frame(frame)
  188. if frame is None:
  189. raise ValueError
  190. except ValueError:
  191. globals = sys.__dict__
  192. lineno = 1
  193. else:
  194. globals = frame.f_globals
  195. lineno = frame.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"):
  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 registry.get('version', 0) != _filters_version:
  227. registry.clear()
  228. registry['version'] = _filters_version
  229. if isinstance(message, Warning):
  230. text = str(message)
  231. category = message.__class__
  232. else:
  233. text = message
  234. message = category(message)
  235. key = (text, category, lineno)
  236. # Quick test for common case
  237. if registry.get(key):
  238. return
  239. # Search the filters
  240. for item in filters:
  241. action, msg, cat, mod, ln = item
  242. if ((msg is None or msg.match(text)) and
  243. issubclass(category, cat) and
  244. (mod is None or mod.match(module)) and
  245. (ln == 0 or lineno == ln)):
  246. break
  247. else:
  248. action = defaultaction
  249. # Early exit actions
  250. if action == "ignore":
  251. registry[key] = 1
  252. return
  253. # Prime the linecache for formatting, in case the
  254. # "file" is actually in a zipfile or something.
  255. import linecache
  256. linecache.getlines(filename, module_globals)
  257. if action == "error":
  258. raise message
  259. # Other actions
  260. if action == "once":
  261. registry[key] = 1
  262. oncekey = (text, category)
  263. if onceregistry.get(oncekey):
  264. return
  265. onceregistry[oncekey] = 1
  266. elif action == "always":
  267. pass
  268. elif action == "module":
  269. registry[key] = 1
  270. altkey = (text, category, 0)
  271. if registry.get(altkey):
  272. return
  273. registry[altkey] = 1
  274. elif action == "default":
  275. registry[key] = 1
  276. else:
  277. # Unrecognized actions are errors
  278. raise RuntimeError(
  279. "Unrecognized action (%r) in warnings.filters:\n %s" %
  280. (action, item))
  281. if not callable(showwarning):
  282. raise TypeError("warnings.showwarning() must be set to a "
  283. "function or method")
  284. # Print message and context
  285. showwarning(message, category, filename, lineno)
  286. class WarningMessage(object):
  287. """Holds the result of a single showwarning() call."""
  288. _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
  289. "line")
  290. def __init__(self, message, category, filename, lineno, file=None,
  291. line=None):
  292. local_values = locals()
  293. for attr in self._WARNING_DETAILS:
  294. setattr(self, attr, local_values[attr])
  295. self._category_name = category.__name__ if category else None
  296. def __str__(self):
  297. return ("{message : %r, category : %r, filename : %r, lineno : %s, "
  298. "line : %r}" % (self.message, self._category_name,
  299. self.filename, self.lineno, self.line))
  300. class catch_warnings(object):
  301. """A context manager that copies and restores the warnings filter upon
  302. exiting the context.
  303. The 'record' argument specifies whether warnings should be captured by a
  304. custom implementation of warnings.showwarning() and be appended to a list
  305. returned by the context manager. Otherwise None is returned by the context
  306. manager. The objects appended to the list are arguments whose attributes
  307. mirror the arguments to showwarning().
  308. The 'module' argument is to specify an alternative module to the module
  309. named 'warnings' and imported under that name. This argument is only useful
  310. when testing the warnings module itself.
  311. """
  312. def __init__(self, *, record=False, module=None):
  313. """Specify whether to record warnings and if an alternative module
  314. should be used other than sys.modules['warnings'].
  315. For compatibility with Python 3.0, please consider all arguments to be
  316. keyword-only.
  317. """
  318. self._record = record
  319. self._module = sys.modules['warnings'] if module is None else module
  320. self._entered = False
  321. def __repr__(self):
  322. args = []
  323. if self._record:
  324. args.append("record=True")
  325. if self._module is not sys.modules['warnings']:
  326. args.append("module=%r" % self._module)
  327. name = type(self).__name__
  328. return "%s(%s)" % (name, ", ".join(args))
  329. def __enter__(self):
  330. if self._entered:
  331. raise RuntimeError("Cannot enter %r twice" % self)
  332. self._entered = True
  333. self._filters = self._module.filters
  334. self._module.filters = self._filters[:]
  335. self._module._filters_mutated()
  336. self._showwarning = self._module.showwarning
  337. if self._record:
  338. log = []
  339. def showwarning(*args, **kwargs):
  340. log.append(WarningMessage(*args, **kwargs))
  341. self._module.showwarning = showwarning
  342. return log
  343. else:
  344. return None
  345. def __exit__(self, *exc_info):
  346. if not self._entered:
  347. raise RuntimeError("Cannot exit %r without entering first" % self)
  348. self._module.filters = self._filters
  349. self._module._filters_mutated()
  350. self._module.showwarning = self._showwarning
  351. # filters contains a sequence of filter 5-tuples
  352. # The components of the 5-tuple are:
  353. # - an action: error, ignore, always, default, module, or once
  354. # - a compiled regex that must match the warning message
  355. # - a class representing the warning category
  356. # - a compiled regex that must match the module that is being warned
  357. # - a line number for the line being warning, or 0 to mean any line
  358. # If either if the compiled regexs are None, match anything.
  359. _warnings_defaults = False
  360. try:
  361. from _warnings import (filters, _defaultaction, _onceregistry,
  362. warn, warn_explicit, _filters_mutated)
  363. defaultaction = _defaultaction
  364. onceregistry = _onceregistry
  365. _warnings_defaults = True
  366. except ImportError:
  367. filters = []
  368. defaultaction = "default"
  369. onceregistry = {}
  370. _filters_version = 1
  371. def _filters_mutated():
  372. global _filters_version
  373. _filters_version += 1
  374. # Module initialization
  375. _processoptions(sys.warnoptions)
  376. if not _warnings_defaults:
  377. silence = [ImportWarning, PendingDeprecationWarning]
  378. silence.append(DeprecationWarning)
  379. for cls in silence:
  380. simplefilter("ignore", category=cls)
  381. bytes_warning = sys.flags.bytes_warning
  382. if bytes_warning > 1:
  383. bytes_action = "error"
  384. elif bytes_warning:
  385. bytes_action = "default"
  386. else:
  387. bytes_action = "ignore"
  388. simplefilter(bytes_action, category=BytesWarning, append=1)
  389. # resource usage warnings are enabled by default in pydebug mode
  390. if hasattr(sys, 'gettotalrefcount'):
  391. resource_action = "always"
  392. else:
  393. resource_action = "ignore"
  394. simplefilter(resource_action, category=ResourceWarning, append=1)
  395. del _warnings_defaults