Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 1 | """Python part of the warnings subsystem.""" |
| 2 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 3 | import sys |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 4 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 5 | |
Brett Cannon | 14ad531 | 2014-08-22 10:44:47 -0400 | [diff] [blame] | 6 | __all__ = ["warn", "warn_explicit", "showwarning", |
| 7 | "formatwarning", "filterwarnings", "simplefilter", |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 8 | "resetwarnings", "catch_warnings"] |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 9 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 10 | def showwarning(message, category, filename, lineno, file=None, line=None): |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 11 | """Hook to write a warning to a file; replace if you like.""" |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 12 | msg = WarningMessage(message, category, filename, lineno, file, line) |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 13 | _showwarnmsg_impl(msg) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 14 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 15 | def formatwarning(message, category, filename, lineno, line=None): |
Guido van Rossum | 9464a7d | 2001-01-14 14:08:40 +0000 | [diff] [blame] | 16 | """Function to format a warning the standard way.""" |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 17 | msg = WarningMessage(message, category, filename, lineno, None, line) |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 18 | return _formatwarnmsg_impl(msg) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 19 | |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 20 | def _showwarnmsg_impl(msg): |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 21 | file = msg.file |
| 22 | if file is None: |
| 23 | file = sys.stderr |
| 24 | if file is None: |
| 25 | # sys.stderr is None when run with pythonw.exe: |
| 26 | # warnings get lost |
| 27 | return |
| 28 | text = _formatwarnmsg(msg) |
| 29 | try: |
| 30 | file.write(text) |
| 31 | except OSError: |
| 32 | # the file (probably stderr) is invalid - this warning gets lost. |
| 33 | pass |
| 34 | |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 35 | def _formatwarnmsg_impl(msg): |
Victor Stinner | 2c07c49 | 2018-11-13 02:41:00 +0100 | [diff] [blame] | 36 | category = msg.category.__name__ |
| 37 | s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n" |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 38 | |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 39 | if msg.line is None: |
Victor Stinner | 2746168 | 2016-03-25 00:30:32 +0100 | [diff] [blame] | 40 | try: |
| 41 | import linecache |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 42 | line = linecache.getline(msg.filename, msg.lineno) |
Victor Stinner | 2746168 | 2016-03-25 00:30:32 +0100 | [diff] [blame] | 43 | except Exception: |
| 44 | # When a warning is logged during Python shutdown, linecache |
Martin Panter | cc71a79 | 2016-04-05 06:19:42 +0000 | [diff] [blame] | 45 | # and the import machinery don't work anymore |
Victor Stinner | 2746168 | 2016-03-25 00:30:32 +0100 | [diff] [blame] | 46 | line = None |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 47 | linecache = None |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 48 | else: |
| 49 | line = msg.line |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 50 | if line: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 51 | line = line.strip() |
| 52 | s += " %s\n" % line |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 53 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 54 | if msg.source is not None: |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 55 | try: |
| 56 | import tracemalloc |
Victor Stinner | 2c07c49 | 2018-11-13 02:41:00 +0100 | [diff] [blame] | 57 | # Logging a warning should not raise a new exception: |
| 58 | # catch Exception, not only ImportError and RecursionError. |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 59 | except Exception: |
Victor Stinner | 2c07c49 | 2018-11-13 02:41:00 +0100 | [diff] [blame] | 60 | # don't suggest to enable tracemalloc if it's not available |
| 61 | tracing = True |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 62 | tb = None |
Victor Stinner | 2c07c49 | 2018-11-13 02:41:00 +0100 | [diff] [blame] | 63 | else: |
| 64 | tracing = tracemalloc.is_tracing() |
| 65 | try: |
| 66 | tb = tracemalloc.get_object_traceback(msg.source) |
| 67 | except Exception: |
| 68 | # When a warning is logged during Python shutdown, tracemalloc |
| 69 | # and the import machinery don't work anymore |
| 70 | tb = None |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 71 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 72 | if tb is not None: |
Jesse-Bakker | 706e10b | 2017-11-30 00:05:07 +0100 | [diff] [blame] | 73 | s += 'Object allocated at (most recent call last):\n' |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 74 | for frame in tb: |
| 75 | s += (' File "%s", lineno %s\n' |
| 76 | % (frame.filename, frame.lineno)) |
Victor Stinner | e091d32 | 2016-03-25 00:33:12 +0100 | [diff] [blame] | 77 | |
| 78 | try: |
| 79 | if linecache is not None: |
| 80 | line = linecache.getline(frame.filename, frame.lineno) |
| 81 | else: |
| 82 | line = None |
| 83 | except Exception: |
| 84 | line = None |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 85 | if line: |
| 86 | line = line.strip() |
| 87 | s += ' %s\n' % line |
Victor Stinner | 2c07c49 | 2018-11-13 02:41:00 +0100 | [diff] [blame] | 88 | elif not tracing: |
| 89 | s += (f'{category}: Enable tracemalloc to get the object ' |
| 90 | f'allocation traceback\n') |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 91 | return s |
| 92 | |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 93 | # Keep a reference to check if the function was replaced |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 94 | _showwarning_orig = showwarning |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 95 | |
| 96 | def _showwarnmsg(msg): |
| 97 | """Hook to write a warning to a file; replace if you like.""" |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 98 | try: |
| 99 | sw = showwarning |
| 100 | except NameError: |
| 101 | pass |
| 102 | else: |
| 103 | if sw is not _showwarning_orig: |
| 104 | # warnings.showwarning() was replaced |
| 105 | if not callable(sw): |
| 106 | raise TypeError("warnings.showwarning() must be set to a " |
| 107 | "function or method") |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 108 | |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 109 | sw(msg.message, msg.category, msg.filename, msg.lineno, |
| 110 | msg.file, msg.line) |
| 111 | return |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 112 | _showwarnmsg_impl(msg) |
| 113 | |
| 114 | # Keep a reference to check if the function was replaced |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 115 | _formatwarning_orig = formatwarning |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 116 | |
| 117 | def _formatwarnmsg(msg): |
| 118 | """Function to format a warning the standard way.""" |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 119 | try: |
| 120 | fw = formatwarning |
| 121 | except NameError: |
| 122 | pass |
| 123 | else: |
| 124 | if fw is not _formatwarning_orig: |
| 125 | # warnings.formatwarning() was replaced |
| 126 | return fw(msg.message, msg.category, |
Xtreak | be7c460 | 2019-03-01 22:47:55 +0530 | [diff] [blame] | 127 | msg.filename, msg.lineno, msg.line) |
Victor Stinner | eedf13f | 2016-03-19 02:11:56 +0100 | [diff] [blame] | 128 | return _formatwarnmsg_impl(msg) |
| 129 | |
Guido van Rossum | 9464a7d | 2001-01-14 14:08:40 +0000 | [diff] [blame] | 130 | def filterwarnings(action, message="", category=Warning, module="", lineno=0, |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 131 | append=False): |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 132 | """Insert an entry into the list of warnings filters (at the front). |
| 133 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 134 | 'action' -- one of "error", "ignore", "always", "default", "module", |
| 135 | or "once" |
| 136 | 'message' -- a regex that the warning message must match |
| 137 | 'category' -- a class that the warning must be a subclass of |
| 138 | 'module' -- a regex that the module name must match |
| 139 | 'lineno' -- an integer line number, 0 matches all warnings |
| 140 | 'append' -- if true, append to the list of filters |
| 141 | """ |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 142 | assert action in ("error", "ignore", "always", "default", "module", |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 143 | "once"), "invalid action: %r" % (action,) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 144 | assert isinstance(message, str), "message must be a string" |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 145 | assert isinstance(category, type), "category must be a class" |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 146 | assert issubclass(category, Warning), "category must be a Warning subclass" |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 147 | assert isinstance(module, str), "module must be a string" |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 148 | assert isinstance(lineno, int) and lineno >= 0, \ |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 149 | "lineno must be an int >= 0" |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 150 | |
| 151 | if message or module: |
| 152 | import re |
| 153 | |
| 154 | if message: |
| 155 | message = re.compile(message, re.I) |
| 156 | else: |
| 157 | message = None |
| 158 | if module: |
| 159 | module = re.compile(module) |
| 160 | else: |
| 161 | module = None |
| 162 | |
| 163 | _add_filter(action, message, category, module, lineno, append=append) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 164 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 165 | def simplefilter(action, category=Warning, lineno=0, append=False): |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 166 | """Insert a simple entry into the list of warnings filters (at the front). |
| 167 | |
| 168 | A simple filter matches all modules and messages. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 169 | 'action' -- one of "error", "ignore", "always", "default", "module", |
| 170 | or "once" |
| 171 | 'category' -- a class that the warning must be a subclass of |
| 172 | 'lineno' -- an integer line number, 0 matches all warnings |
| 173 | 'append' -- if true, append to the list of filters |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 174 | """ |
| 175 | assert action in ("error", "ignore", "always", "default", "module", |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 176 | "once"), "invalid action: %r" % (action,) |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 177 | assert isinstance(lineno, int) and lineno >= 0, \ |
| 178 | "lineno must be an int >= 0" |
Martin Panter | 43593a1 | 2016-05-26 09:10:55 +0000 | [diff] [blame] | 179 | _add_filter(action, None, category, None, lineno, append=append) |
| 180 | |
| 181 | def _add_filter(*item, append): |
| 182 | # Remove possible duplicate filters, so new one will be placed |
| 183 | # in correct place. If append=True and duplicate exists, do nothing. |
| 184 | if not append: |
| 185 | try: |
| 186 | filters.remove(item) |
| 187 | except ValueError: |
| 188 | pass |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 189 | filters.insert(0, item) |
Martin Panter | 43593a1 | 2016-05-26 09:10:55 +0000 | [diff] [blame] | 190 | else: |
| 191 | if item not in filters: |
| 192 | filters.append(item) |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 193 | _filters_mutated() |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 195 | def resetwarnings(): |
Tim Peters | d0cc4f0 | 2002-04-16 01:51:25 +0000 | [diff] [blame] | 196 | """Clear the list of warning filters, so that no filters are active.""" |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 197 | filters[:] = [] |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 198 | _filters_mutated() |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 199 | |
| 200 | class _OptionError(Exception): |
| 201 | """Exception used by option processing helpers.""" |
| 202 | pass |
| 203 | |
| 204 | # Helper to process -W options passed via sys.warnoptions |
| 205 | def _processoptions(args): |
| 206 | for arg in args: |
| 207 | try: |
| 208 | _setoption(arg) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 209 | except _OptionError as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 210 | print("Invalid -W option ignored:", msg, file=sys.stderr) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 211 | |
| 212 | # Helper for _processoptions() |
| 213 | def _setoption(arg): |
Skip Montanaro | d8f2120 | 2003-05-14 17:33:53 +0000 | [diff] [blame] | 214 | import re |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 215 | parts = arg.split(':') |
| 216 | if len(parts) > 5: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 217 | raise _OptionError("too many fields (max 5): %r" % (arg,)) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 218 | while len(parts) < 5: |
| 219 | parts.append('') |
| 220 | action, message, category, module, lineno = [s.strip() |
| 221 | for s in parts] |
| 222 | action = _getaction(action) |
| 223 | message = re.escape(message) |
| 224 | category = _getcategory(category) |
| 225 | module = re.escape(module) |
| 226 | if module: |
| 227 | module = module + '$' |
| 228 | if lineno: |
| 229 | try: |
| 230 | lineno = int(lineno) |
| 231 | if lineno < 0: |
| 232 | raise ValueError |
| 233 | except (ValueError, OverflowError): |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 234 | raise _OptionError("invalid lineno %r" % (lineno,)) from None |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 235 | else: |
| 236 | lineno = 0 |
| 237 | filterwarnings(action, message, category, module, lineno) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 238 | |
| 239 | # Helper for _setoption() |
| 240 | def _getaction(action): |
| 241 | if not action: |
| 242 | return "default" |
| 243 | if action == "all": return "always" # Alias |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 244 | for a in ('default', 'always', 'ignore', 'module', 'once', 'error'): |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 245 | if a.startswith(action): |
| 246 | return a |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 247 | raise _OptionError("invalid action: %r" % (action,)) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 248 | |
| 249 | # Helper for _setoption() |
| 250 | def _getcategory(category): |
Skip Montanaro | d8f2120 | 2003-05-14 17:33:53 +0000 | [diff] [blame] | 251 | import re |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 252 | if not category: |
| 253 | return Warning |
| 254 | if re.match("^[a-zA-Z0-9_]+$", category): |
| 255 | try: |
| 256 | cat = eval(category) |
Guido van Rossum | d1db30b | 2000-12-19 03:04:50 +0000 | [diff] [blame] | 257 | except NameError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 258 | raise _OptionError("unknown warning category: %r" % (category,)) from None |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 259 | else: |
| 260 | i = category.rfind(".") |
| 261 | module = category[:i] |
| 262 | klass = category[i+1:] |
Guido van Rossum | d1db30b | 2000-12-19 03:04:50 +0000 | [diff] [blame] | 263 | try: |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 264 | m = __import__(module, None, None, [klass]) |
| 265 | except ImportError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 266 | raise _OptionError("invalid module name: %r" % (module,)) from None |
Guido van Rossum | d1db30b | 2000-12-19 03:04:50 +0000 | [diff] [blame] | 267 | try: |
| 268 | cat = getattr(m, klass) |
| 269 | except AttributeError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 270 | raise _OptionError("unknown warning category: %r" % (category,)) from None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 271 | if not issubclass(cat, Warning): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 272 | raise _OptionError("invalid warning category: %r" % (category,)) |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 273 | return cat |
| 274 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 275 | |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 276 | def _is_internal_frame(frame): |
| 277 | """Signal whether the frame is an internal CPython implementation detail.""" |
| 278 | filename = frame.f_code.co_filename |
| 279 | return 'importlib' in filename and '_bootstrap' in filename |
| 280 | |
| 281 | |
| 282 | def _next_external_frame(frame): |
| 283 | """Find the next frame that doesn't involve CPython internals.""" |
| 284 | frame = frame.f_back |
| 285 | while frame is not None and _is_internal_frame(frame): |
| 286 | frame = frame.f_back |
| 287 | return frame |
| 288 | |
| 289 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 290 | # Code typically replaced by _warnings |
Victor Stinner | e19558a | 2016-03-23 00:28:08 +0100 | [diff] [blame] | 291 | def warn(message, category=None, stacklevel=1, source=None): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 292 | """Issue a warning, or maybe ignore it or raise an exception.""" |
| 293 | # Check if message is already a Warning object |
| 294 | if isinstance(message, Warning): |
| 295 | category = message.__class__ |
| 296 | # Check category argument |
| 297 | if category is None: |
| 298 | category = UserWarning |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 299 | if not (isinstance(category, type) and issubclass(category, Warning)): |
| 300 | raise TypeError("category must be a Warning subclass, " |
| 301 | "not '{:s}'".format(type(category).__name__)) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 302 | # Get context information |
| 303 | try: |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 304 | if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)): |
| 305 | # If frame is too small to care or if the warning originated in |
| 306 | # internal code, then do not try to hide any frames. |
| 307 | frame = sys._getframe(stacklevel) |
| 308 | else: |
| 309 | frame = sys._getframe(1) |
| 310 | # Look for one frame less since the above line starts us off. |
| 311 | for x in range(stacklevel-1): |
| 312 | frame = _next_external_frame(frame) |
| 313 | if frame is None: |
| 314 | raise ValueError |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 315 | except ValueError: |
| 316 | globals = sys.__dict__ |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 317 | filename = "sys" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 318 | lineno = 1 |
| 319 | else: |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 320 | globals = frame.f_globals |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 321 | filename = frame.f_code.co_filename |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 322 | lineno = frame.f_lineno |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 323 | if '__name__' in globals: |
| 324 | module = globals['__name__'] |
| 325 | else: |
| 326 | module = "<string>" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 327 | registry = globals.setdefault("__warningregistry__", {}) |
| 328 | warn_explicit(message, category, filename, lineno, module, registry, |
Victor Stinner | e19558a | 2016-03-23 00:28:08 +0100 | [diff] [blame] | 329 | globals, source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 330 | |
| 331 | def warn_explicit(message, category, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 332 | module=None, registry=None, module_globals=None, |
| 333 | source=None): |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 334 | lineno = int(lineno) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 335 | if module is None: |
| 336 | module = filename or "<unknown>" |
| 337 | if module[-3:].lower() == ".py": |
| 338 | module = module[:-3] # XXX What about leading pathname? |
| 339 | if registry is None: |
| 340 | registry = {} |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 341 | if registry.get('version', 0) != _filters_version: |
| 342 | registry.clear() |
| 343 | registry['version'] = _filters_version |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 344 | if isinstance(message, Warning): |
| 345 | text = str(message) |
| 346 | category = message.__class__ |
| 347 | else: |
| 348 | text = message |
| 349 | message = category(message) |
| 350 | key = (text, category, lineno) |
| 351 | # Quick test for common case |
| 352 | if registry.get(key): |
| 353 | return |
| 354 | # Search the filters |
| 355 | for item in filters: |
| 356 | action, msg, cat, mod, ln = item |
| 357 | if ((msg is None or msg.match(text)) and |
| 358 | issubclass(category, cat) and |
| 359 | (mod is None or mod.match(module)) and |
| 360 | (ln == 0 or lineno == ln)): |
| 361 | break |
| 362 | else: |
| 363 | action = defaultaction |
| 364 | # Early exit actions |
| 365 | if action == "ignore": |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 366 | return |
| 367 | |
| 368 | # Prime the linecache for formatting, in case the |
| 369 | # "file" is actually in a zipfile or something. |
Antoine Pitrou | 7cb11fa | 2013-10-24 22:23:42 +0200 | [diff] [blame] | 370 | import linecache |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 371 | linecache.getlines(filename, module_globals) |
| 372 | |
| 373 | if action == "error": |
| 374 | raise message |
| 375 | # Other actions |
| 376 | if action == "once": |
| 377 | registry[key] = 1 |
| 378 | oncekey = (text, category) |
| 379 | if onceregistry.get(oncekey): |
| 380 | return |
| 381 | onceregistry[oncekey] = 1 |
| 382 | elif action == "always": |
| 383 | pass |
| 384 | elif action == "module": |
| 385 | registry[key] = 1 |
| 386 | altkey = (text, category, 0) |
| 387 | if registry.get(altkey): |
| 388 | return |
| 389 | registry[altkey] = 1 |
| 390 | elif action == "default": |
| 391 | registry[key] = 1 |
| 392 | else: |
| 393 | # Unrecognized actions are errors |
| 394 | raise RuntimeError( |
| 395 | "Unrecognized action (%r) in warnings.filters:\n %s" % |
| 396 | (action, item)) |
| 397 | # Print message and context |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 398 | msg = WarningMessage(message, category, filename, lineno, source) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 399 | _showwarnmsg(msg) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 400 | |
| 401 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 402 | class WarningMessage(object): |
| 403 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 404 | _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file", |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 405 | "line", "source") |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 406 | |
| 407 | def __init__(self, message, category, filename, lineno, file=None, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 408 | line=None, source=None): |
Alex Gaynor | 5de3a64 | 2017-06-04 11:34:16 -0400 | [diff] [blame] | 409 | self.message = message |
| 410 | self.category = category |
| 411 | self.filename = filename |
| 412 | self.lineno = lineno |
| 413 | self.file = file |
| 414 | self.line = line |
| 415 | self.source = source |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 416 | self._category_name = category.__name__ if category else None |
| 417 | |
| 418 | def __str__(self): |
| 419 | return ("{message : %r, category : %r, filename : %r, lineno : %s, " |
| 420 | "line : %r}" % (self.message, self._category_name, |
| 421 | self.filename, self.lineno, self.line)) |
| 422 | |
| 423 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 424 | class catch_warnings(object): |
| 425 | |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 426 | """A context manager that copies and restores the warnings filter upon |
| 427 | exiting the context. |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 428 | |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 429 | The 'record' argument specifies whether warnings should be captured by a |
| 430 | custom implementation of warnings.showwarning() and be appended to a list |
| 431 | returned by the context manager. Otherwise None is returned by the context |
| 432 | manager. The objects appended to the list are arguments whose attributes |
| 433 | mirror the arguments to showwarning(). |
| 434 | |
| 435 | The 'module' argument is to specify an alternative module to the module |
| 436 | named 'warnings' and imported under that name. This argument is only useful |
| 437 | when testing the warnings module itself. |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 438 | |
| 439 | """ |
| 440 | |
| 441 | def __init__(self, *, record=False, module=None): |
| 442 | """Specify whether to record warnings and if an alternative module |
| 443 | should be used other than sys.modules['warnings']. |
| 444 | |
| 445 | For compatibility with Python 3.0, please consider all arguments to be |
| 446 | keyword-only. |
| 447 | |
| 448 | """ |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 449 | self._record = record |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 450 | self._module = sys.modules['warnings'] if module is None else module |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 451 | self._entered = False |
| 452 | |
| 453 | def __repr__(self): |
| 454 | args = [] |
| 455 | if self._record: |
| 456 | args.append("record=True") |
| 457 | if self._module is not sys.modules['warnings']: |
| 458 | args.append("module=%r" % self._module) |
| 459 | name = type(self).__name__ |
| 460 | return "%s(%s)" % (name, ", ".join(args)) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 461 | |
| 462 | def __enter__(self): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 463 | if self._entered: |
| 464 | raise RuntimeError("Cannot enter %r twice" % self) |
| 465 | self._entered = True |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 466 | self._filters = self._module.filters |
| 467 | self._module.filters = self._filters[:] |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 468 | self._module._filters_mutated() |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 469 | self._showwarning = self._module.showwarning |
Victor Stinner | 8ef46be | 2016-12-06 10:53:52 +0100 | [diff] [blame] | 470 | self._showwarnmsg_impl = self._module._showwarnmsg_impl |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 471 | if self._record: |
| 472 | log = [] |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 473 | self._module._showwarnmsg_impl = log.append |
Victor Stinner | 8ef46be | 2016-12-06 10:53:52 +0100 | [diff] [blame] | 474 | # Reset showwarning() to the default implementation to make sure |
| 475 | # that _showwarnmsg() calls _showwarnmsg_impl() |
Ned Deily | c1c3292 | 2016-12-06 17:12:47 -0500 | [diff] [blame] | 476 | self._module.showwarning = self._module._showwarning_orig |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 477 | return log |
| 478 | else: |
| 479 | return None |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 480 | |
| 481 | def __exit__(self, *exc_info): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 482 | if not self._entered: |
| 483 | raise RuntimeError("Cannot exit %r without entering first" % self) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 484 | self._module.filters = self._filters |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 485 | self._module._filters_mutated() |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 486 | self._module.showwarning = self._showwarning |
Victor Stinner | 8ef46be | 2016-12-06 10:53:52 +0100 | [diff] [blame] | 487 | self._module._showwarnmsg_impl = self._showwarnmsg_impl |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 488 | |
| 489 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 490 | # Private utility function called by _PyErr_WarnUnawaitedCoroutine |
| 491 | def _warn_unawaited_coroutine(coro): |
| 492 | msg_lines = [ |
| 493 | f"coroutine '{coro.__qualname__}' was never awaited\n" |
| 494 | ] |
| 495 | if coro.cr_origin is not None: |
| 496 | import linecache, traceback |
| 497 | def extract(): |
| 498 | for filename, lineno, funcname in reversed(coro.cr_origin): |
| 499 | line = linecache.getline(filename, lineno) |
| 500 | yield (filename, lineno, funcname, line) |
| 501 | msg_lines.append("Coroutine created at (most recent call last)\n") |
| 502 | msg_lines += traceback.format_list(list(extract())) |
| 503 | msg = "".join(msg_lines).rstrip("\n") |
| 504 | # Passing source= here means that if the user happens to have tracemalloc |
| 505 | # enabled and tracking where the coroutine was created, the warning will |
| 506 | # contain that traceback. This does mean that if they have *both* |
| 507 | # coroutine origin tracking *and* tracemalloc enabled, they'll get two |
| 508 | # partially-redundant tracebacks. If we wanted to be clever we could |
| 509 | # probably detect this case and avoid it, but for now we don't bother. |
| 510 | warn(msg, category=RuntimeWarning, stacklevel=2, source=coro) |
| 511 | |
| 512 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 513 | # filters contains a sequence of filter 5-tuples |
| 514 | # The components of the 5-tuple are: |
| 515 | # - an action: error, ignore, always, default, module, or once |
| 516 | # - a compiled regex that must match the warning message |
| 517 | # - a class representing the warning category |
| 518 | # - a compiled regex that must match the module that is being warned |
| 519 | # - a line number for the line being warning, or 0 to mean any line |
| 520 | # If either if the compiled regexs are None, match anything. |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 521 | try: |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 522 | from _warnings import (filters, _defaultaction, _onceregistry, |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 523 | warn, warn_explicit, _filters_mutated) |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 524 | defaultaction = _defaultaction |
| 525 | onceregistry = _onceregistry |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 526 | _warnings_defaults = True |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 527 | except ImportError: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 528 | filters = [] |
| 529 | defaultaction = "default" |
| 530 | onceregistry = {} |
| 531 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 532 | _filters_version = 1 |
| 533 | |
| 534 | def _filters_mutated(): |
| 535 | global _filters_version |
| 536 | _filters_version += 1 |
| 537 | |
Victor Stinner | 09f3a8a | 2017-11-20 17:32:40 -0800 | [diff] [blame] | 538 | _warnings_defaults = False |
| 539 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | 2a862c6 | 2000-12-15 21:59:53 +0000 | [diff] [blame] | 541 | # Module initialization |
Tim Peters | 6602520 | 2004-03-21 17:06:20 +0000 | [diff] [blame] | 542 | _processoptions(sys.warnoptions) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 543 | if not _warnings_defaults: |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 544 | # Several warning categories are ignored by default in regular builds |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 545 | if not hasattr(sys, 'gettotalrefcount'): |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 546 | filterwarnings("default", category=DeprecationWarning, |
| 547 | module="__main__", append=1) |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 548 | simplefilter("ignore", category=DeprecationWarning, append=1) |
| 549 | simplefilter("ignore", category=PendingDeprecationWarning, append=1) |
| 550 | simplefilter("ignore", category=ImportWarning, append=1) |
| 551 | simplefilter("ignore", category=ResourceWarning, append=1) |
Victor Stinner | 09f3a8a | 2017-11-20 17:32:40 -0800 | [diff] [blame] | 552 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 553 | del _warnings_defaults |