Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: warnings.py
"""Python part of the warnings subsystem."""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
[3] Fix | Delete
[4] Fix | Delete
__all__ = ["warn", "warn_explicit", "showwarning",
[5] Fix | Delete
"formatwarning", "filterwarnings", "simplefilter",
[6] Fix | Delete
"resetwarnings", "catch_warnings"]
[7] Fix | Delete
[8] Fix | Delete
def showwarning(message, category, filename, lineno, file=None, line=None):
[9] Fix | Delete
"""Hook to write a warning to a file; replace if you like."""
[10] Fix | Delete
msg = WarningMessage(message, category, filename, lineno, file, line)
[11] Fix | Delete
_showwarnmsg_impl(msg)
[12] Fix | Delete
[13] Fix | Delete
def formatwarning(message, category, filename, lineno, line=None):
[14] Fix | Delete
"""Function to format a warning the standard way."""
[15] Fix | Delete
msg = WarningMessage(message, category, filename, lineno, None, line)
[16] Fix | Delete
return _formatwarnmsg_impl(msg)
[17] Fix | Delete
[18] Fix | Delete
def _showwarnmsg_impl(msg):
[19] Fix | Delete
file = msg.file
[20] Fix | Delete
if file is None:
[21] Fix | Delete
file = sys.stderr
[22] Fix | Delete
if file is None:
[23] Fix | Delete
# sys.stderr is None when run with pythonw.exe:
[24] Fix | Delete
# warnings get lost
[25] Fix | Delete
return
[26] Fix | Delete
text = _formatwarnmsg(msg)
[27] Fix | Delete
try:
[28] Fix | Delete
file.write(text)
[29] Fix | Delete
except OSError:
[30] Fix | Delete
# the file (probably stderr) is invalid - this warning gets lost.
[31] Fix | Delete
pass
[32] Fix | Delete
[33] Fix | Delete
def _formatwarnmsg_impl(msg):
[34] Fix | Delete
category = msg.category.__name__
[35] Fix | Delete
s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
[36] Fix | Delete
[37] Fix | Delete
if msg.line is None:
[38] Fix | Delete
try:
[39] Fix | Delete
import linecache
[40] Fix | Delete
line = linecache.getline(msg.filename, msg.lineno)
[41] Fix | Delete
except Exception:
[42] Fix | Delete
# When a warning is logged during Python shutdown, linecache
[43] Fix | Delete
# and the import machinery don't work anymore
[44] Fix | Delete
line = None
[45] Fix | Delete
linecache = None
[46] Fix | Delete
else:
[47] Fix | Delete
line = msg.line
[48] Fix | Delete
if line:
[49] Fix | Delete
line = line.strip()
[50] Fix | Delete
s += " %s\n" % line
[51] Fix | Delete
[52] Fix | Delete
if msg.source is not None:
[53] Fix | Delete
try:
[54] Fix | Delete
import tracemalloc
[55] Fix | Delete
# Logging a warning should not raise a new exception:
[56] Fix | Delete
# catch Exception, not only ImportError and RecursionError.
[57] Fix | Delete
except Exception:
[58] Fix | Delete
# don't suggest to enable tracemalloc if it's not available
[59] Fix | Delete
tracing = True
[60] Fix | Delete
tb = None
[61] Fix | Delete
else:
[62] Fix | Delete
tracing = tracemalloc.is_tracing()
[63] Fix | Delete
try:
[64] Fix | Delete
tb = tracemalloc.get_object_traceback(msg.source)
[65] Fix | Delete
except Exception:
[66] Fix | Delete
# When a warning is logged during Python shutdown, tracemalloc
[67] Fix | Delete
# and the import machinery don't work anymore
[68] Fix | Delete
tb = None
[69] Fix | Delete
[70] Fix | Delete
if tb is not None:
[71] Fix | Delete
s += 'Object allocated at (most recent call last):\n'
[72] Fix | Delete
for frame in tb:
[73] Fix | Delete
s += (' File "%s", lineno %s\n'
[74] Fix | Delete
% (frame.filename, frame.lineno))
[75] Fix | Delete
[76] Fix | Delete
try:
[77] Fix | Delete
if linecache is not None:
[78] Fix | Delete
line = linecache.getline(frame.filename, frame.lineno)
[79] Fix | Delete
else:
[80] Fix | Delete
line = None
[81] Fix | Delete
except Exception:
[82] Fix | Delete
line = None
[83] Fix | Delete
if line:
[84] Fix | Delete
line = line.strip()
[85] Fix | Delete
s += ' %s\n' % line
[86] Fix | Delete
elif not tracing:
[87] Fix | Delete
s += (f'{category}: Enable tracemalloc to get the object '
[88] Fix | Delete
f'allocation traceback\n')
[89] Fix | Delete
return s
[90] Fix | Delete
[91] Fix | Delete
# Keep a reference to check if the function was replaced
[92] Fix | Delete
_showwarning_orig = showwarning
[93] Fix | Delete
[94] Fix | Delete
def _showwarnmsg(msg):
[95] Fix | Delete
"""Hook to write a warning to a file; replace if you like."""
[96] Fix | Delete
try:
[97] Fix | Delete
sw = showwarning
[98] Fix | Delete
except NameError:
[99] Fix | Delete
pass
[100] Fix | Delete
else:
[101] Fix | Delete
if sw is not _showwarning_orig:
[102] Fix | Delete
# warnings.showwarning() was replaced
[103] Fix | Delete
if not callable(sw):
[104] Fix | Delete
raise TypeError("warnings.showwarning() must be set to a "
[105] Fix | Delete
"function or method")
[106] Fix | Delete
[107] Fix | Delete
sw(msg.message, msg.category, msg.filename, msg.lineno,
[108] Fix | Delete
msg.file, msg.line)
[109] Fix | Delete
return
[110] Fix | Delete
_showwarnmsg_impl(msg)
[111] Fix | Delete
[112] Fix | Delete
# Keep a reference to check if the function was replaced
[113] Fix | Delete
_formatwarning_orig = formatwarning
[114] Fix | Delete
[115] Fix | Delete
def _formatwarnmsg(msg):
[116] Fix | Delete
"""Function to format a warning the standard way."""
[117] Fix | Delete
try:
[118] Fix | Delete
fw = formatwarning
[119] Fix | Delete
except NameError:
[120] Fix | Delete
pass
[121] Fix | Delete
else:
[122] Fix | Delete
if fw is not _formatwarning_orig:
[123] Fix | Delete
# warnings.formatwarning() was replaced
[124] Fix | Delete
return fw(msg.message, msg.category,
[125] Fix | Delete
msg.filename, msg.lineno, msg.line)
[126] Fix | Delete
return _formatwarnmsg_impl(msg)
[127] Fix | Delete
[128] Fix | Delete
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
[129] Fix | Delete
append=False):
[130] Fix | Delete
"""Insert an entry into the list of warnings filters (at the front).
[131] Fix | Delete
[132] Fix | Delete
'action' -- one of "error", "ignore", "always", "default", "module",
[133] Fix | Delete
or "once"
[134] Fix | Delete
'message' -- a regex that the warning message must match
[135] Fix | Delete
'category' -- a class that the warning must be a subclass of
[136] Fix | Delete
'module' -- a regex that the module name must match
[137] Fix | Delete
'lineno' -- an integer line number, 0 matches all warnings
[138] Fix | Delete
'append' -- if true, append to the list of filters
[139] Fix | Delete
"""
[140] Fix | Delete
assert action in ("error", "ignore", "always", "default", "module",
[141] Fix | Delete
"once"), "invalid action: %r" % (action,)
[142] Fix | Delete
assert isinstance(message, str), "message must be a string"
[143] Fix | Delete
assert isinstance(category, type), "category must be a class"
[144] Fix | Delete
assert issubclass(category, Warning), "category must be a Warning subclass"
[145] Fix | Delete
assert isinstance(module, str), "module must be a string"
[146] Fix | Delete
assert isinstance(lineno, int) and lineno >= 0, \
[147] Fix | Delete
"lineno must be an int >= 0"
[148] Fix | Delete
[149] Fix | Delete
if message or module:
[150] Fix | Delete
import re
[151] Fix | Delete
[152] Fix | Delete
if message:
[153] Fix | Delete
message = re.compile(message, re.I)
[154] Fix | Delete
else:
[155] Fix | Delete
message = None
[156] Fix | Delete
if module:
[157] Fix | Delete
module = re.compile(module)
[158] Fix | Delete
else:
[159] Fix | Delete
module = None
[160] Fix | Delete
[161] Fix | Delete
_add_filter(action, message, category, module, lineno, append=append)
[162] Fix | Delete
[163] Fix | Delete
def simplefilter(action, category=Warning, lineno=0, append=False):
[164] Fix | Delete
"""Insert a simple entry into the list of warnings filters (at the front).
[165] Fix | Delete
[166] Fix | Delete
A simple filter matches all modules and messages.
[167] Fix | Delete
'action' -- one of "error", "ignore", "always", "default", "module",
[168] Fix | Delete
or "once"
[169] Fix | Delete
'category' -- a class that the warning must be a subclass of
[170] Fix | Delete
'lineno' -- an integer line number, 0 matches all warnings
[171] Fix | Delete
'append' -- if true, append to the list of filters
[172] Fix | Delete
"""
[173] Fix | Delete
assert action in ("error", "ignore", "always", "default", "module",
[174] Fix | Delete
"once"), "invalid action: %r" % (action,)
[175] Fix | Delete
assert isinstance(lineno, int) and lineno >= 0, \
[176] Fix | Delete
"lineno must be an int >= 0"
[177] Fix | Delete
_add_filter(action, None, category, None, lineno, append=append)
[178] Fix | Delete
[179] Fix | Delete
def _add_filter(*item, append):
[180] Fix | Delete
# Remove possible duplicate filters, so new one will be placed
[181] Fix | Delete
# in correct place. If append=True and duplicate exists, do nothing.
[182] Fix | Delete
if not append:
[183] Fix | Delete
try:
[184] Fix | Delete
filters.remove(item)
[185] Fix | Delete
except ValueError:
[186] Fix | Delete
pass
[187] Fix | Delete
filters.insert(0, item)
[188] Fix | Delete
else:
[189] Fix | Delete
if item not in filters:
[190] Fix | Delete
filters.append(item)
[191] Fix | Delete
_filters_mutated()
[192] Fix | Delete
[193] Fix | Delete
def resetwarnings():
[194] Fix | Delete
"""Clear the list of warning filters, so that no filters are active."""
[195] Fix | Delete
filters[:] = []
[196] Fix | Delete
_filters_mutated()
[197] Fix | Delete
[198] Fix | Delete
class _OptionError(Exception):
[199] Fix | Delete
"""Exception used by option processing helpers."""
[200] Fix | Delete
pass
[201] Fix | Delete
[202] Fix | Delete
# Helper to process -W options passed via sys.warnoptions
[203] Fix | Delete
def _processoptions(args):
[204] Fix | Delete
for arg in args:
[205] Fix | Delete
try:
[206] Fix | Delete
_setoption(arg)
[207] Fix | Delete
except _OptionError as msg:
[208] Fix | Delete
print("Invalid -W option ignored:", msg, file=sys.stderr)
[209] Fix | Delete
[210] Fix | Delete
# Helper for _processoptions()
[211] Fix | Delete
def _setoption(arg):
[212] Fix | Delete
parts = arg.split(':')
[213] Fix | Delete
if len(parts) > 5:
[214] Fix | Delete
raise _OptionError("too many fields (max 5): %r" % (arg,))
[215] Fix | Delete
while len(parts) < 5:
[216] Fix | Delete
parts.append('')
[217] Fix | Delete
action, message, category, module, lineno = [s.strip()
[218] Fix | Delete
for s in parts]
[219] Fix | Delete
action = _getaction(action)
[220] Fix | Delete
category = _getcategory(category)
[221] Fix | Delete
if message or module:
[222] Fix | Delete
import re
[223] Fix | Delete
if message:
[224] Fix | Delete
message = re.escape(message)
[225] Fix | Delete
if module:
[226] Fix | Delete
module = re.escape(module) + r'\Z'
[227] Fix | Delete
if lineno:
[228] Fix | Delete
try:
[229] Fix | Delete
lineno = int(lineno)
[230] Fix | Delete
if lineno < 0:
[231] Fix | Delete
raise ValueError
[232] Fix | Delete
except (ValueError, OverflowError):
[233] Fix | Delete
raise _OptionError("invalid lineno %r" % (lineno,)) from None
[234] Fix | Delete
else:
[235] Fix | Delete
lineno = 0
[236] Fix | Delete
filterwarnings(action, message, category, module, lineno)
[237] Fix | Delete
[238] Fix | Delete
# Helper for _setoption()
[239] Fix | Delete
def _getaction(action):
[240] Fix | Delete
if not action:
[241] Fix | Delete
return "default"
[242] Fix | Delete
if action == "all": return "always" # Alias
[243] Fix | Delete
for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
[244] Fix | Delete
if a.startswith(action):
[245] Fix | Delete
return a
[246] Fix | Delete
raise _OptionError("invalid action: %r" % (action,))
[247] Fix | Delete
[248] Fix | Delete
# Helper for _setoption()
[249] Fix | Delete
def _getcategory(category):
[250] Fix | Delete
if not category:
[251] Fix | Delete
return Warning
[252] Fix | Delete
if '.' not in category:
[253] Fix | Delete
import builtins as m
[254] Fix | Delete
klass = category
[255] Fix | Delete
else:
[256] Fix | Delete
module, _, klass = category.rpartition('.')
[257] Fix | Delete
try:
[258] Fix | Delete
m = __import__(module, None, None, [klass])
[259] Fix | Delete
except ImportError:
[260] Fix | Delete
raise _OptionError("invalid module name: %r" % (module,)) from None
[261] Fix | Delete
try:
[262] Fix | Delete
cat = getattr(m, klass)
[263] Fix | Delete
except AttributeError:
[264] Fix | Delete
raise _OptionError("unknown warning category: %r" % (category,)) from None
[265] Fix | Delete
if not issubclass(cat, Warning):
[266] Fix | Delete
raise _OptionError("invalid warning category: %r" % (category,))
[267] Fix | Delete
return cat
[268] Fix | Delete
[269] Fix | Delete
[270] Fix | Delete
def _is_internal_frame(frame):
[271] Fix | Delete
"""Signal whether the frame is an internal CPython implementation detail."""
[272] Fix | Delete
filename = frame.f_code.co_filename
[273] Fix | Delete
return 'importlib' in filename and '_bootstrap' in filename
[274] Fix | Delete
[275] Fix | Delete
[276] Fix | Delete
def _next_external_frame(frame):
[277] Fix | Delete
"""Find the next frame that doesn't involve CPython internals."""
[278] Fix | Delete
frame = frame.f_back
[279] Fix | Delete
while frame is not None and _is_internal_frame(frame):
[280] Fix | Delete
frame = frame.f_back
[281] Fix | Delete
return frame
[282] Fix | Delete
[283] Fix | Delete
[284] Fix | Delete
# Code typically replaced by _warnings
[285] Fix | Delete
def warn(message, category=None, stacklevel=1, source=None):
[286] Fix | Delete
"""Issue a warning, or maybe ignore it or raise an exception."""
[287] Fix | Delete
# Check if message is already a Warning object
[288] Fix | Delete
if isinstance(message, Warning):
[289] Fix | Delete
category = message.__class__
[290] Fix | Delete
# Check category argument
[291] Fix | Delete
if category is None:
[292] Fix | Delete
category = UserWarning
[293] Fix | Delete
if not (isinstance(category, type) and issubclass(category, Warning)):
[294] Fix | Delete
raise TypeError("category must be a Warning subclass, "
[295] Fix | Delete
"not '{:s}'".format(type(category).__name__))
[296] Fix | Delete
# Get context information
[297] Fix | Delete
try:
[298] Fix | Delete
if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)):
[299] Fix | Delete
# If frame is too small to care or if the warning originated in
[300] Fix | Delete
# internal code, then do not try to hide any frames.
[301] Fix | Delete
frame = sys._getframe(stacklevel)
[302] Fix | Delete
else:
[303] Fix | Delete
frame = sys._getframe(1)
[304] Fix | Delete
# Look for one frame less since the above line starts us off.
[305] Fix | Delete
for x in range(stacklevel-1):
[306] Fix | Delete
frame = _next_external_frame(frame)
[307] Fix | Delete
if frame is None:
[308] Fix | Delete
raise ValueError
[309] Fix | Delete
except ValueError:
[310] Fix | Delete
globals = sys.__dict__
[311] Fix | Delete
filename = "sys"
[312] Fix | Delete
lineno = 1
[313] Fix | Delete
else:
[314] Fix | Delete
globals = frame.f_globals
[315] Fix | Delete
filename = frame.f_code.co_filename
[316] Fix | Delete
lineno = frame.f_lineno
[317] Fix | Delete
if '__name__' in globals:
[318] Fix | Delete
module = globals['__name__']
[319] Fix | Delete
else:
[320] Fix | Delete
module = "<string>"
[321] Fix | Delete
registry = globals.setdefault("__warningregistry__", {})
[322] Fix | Delete
warn_explicit(message, category, filename, lineno, module, registry,
[323] Fix | Delete
globals, source)
[324] Fix | Delete
[325] Fix | Delete
def warn_explicit(message, category, filename, lineno,
[326] Fix | Delete
module=None, registry=None, module_globals=None,
[327] Fix | Delete
source=None):
[328] Fix | Delete
lineno = int(lineno)
[329] Fix | Delete
if module is None:
[330] Fix | Delete
module = filename or "<unknown>"
[331] Fix | Delete
if module[-3:].lower() == ".py":
[332] Fix | Delete
module = module[:-3] # XXX What about leading pathname?
[333] Fix | Delete
if registry is None:
[334] Fix | Delete
registry = {}
[335] Fix | Delete
if registry.get('version', 0) != _filters_version:
[336] Fix | Delete
registry.clear()
[337] Fix | Delete
registry['version'] = _filters_version
[338] Fix | Delete
if isinstance(message, Warning):
[339] Fix | Delete
text = str(message)
[340] Fix | Delete
category = message.__class__
[341] Fix | Delete
else:
[342] Fix | Delete
text = message
[343] Fix | Delete
message = category(message)
[344] Fix | Delete
key = (text, category, lineno)
[345] Fix | Delete
# Quick test for common case
[346] Fix | Delete
if registry.get(key):
[347] Fix | Delete
return
[348] Fix | Delete
# Search the filters
[349] Fix | Delete
for item in filters:
[350] Fix | Delete
action, msg, cat, mod, ln = item
[351] Fix | Delete
if ((msg is None or msg.match(text)) and
[352] Fix | Delete
issubclass(category, cat) and
[353] Fix | Delete
(mod is None or mod.match(module)) and
[354] Fix | Delete
(ln == 0 or lineno == ln)):
[355] Fix | Delete
break
[356] Fix | Delete
else:
[357] Fix | Delete
action = defaultaction
[358] Fix | Delete
# Early exit actions
[359] Fix | Delete
if action == "ignore":
[360] Fix | Delete
return
[361] Fix | Delete
[362] Fix | Delete
# Prime the linecache for formatting, in case the
[363] Fix | Delete
# "file" is actually in a zipfile or something.
[364] Fix | Delete
import linecache
[365] Fix | Delete
linecache.getlines(filename, module_globals)
[366] Fix | Delete
[367] Fix | Delete
if action == "error":
[368] Fix | Delete
raise message
[369] Fix | Delete
# Other actions
[370] Fix | Delete
if action == "once":
[371] Fix | Delete
registry[key] = 1
[372] Fix | Delete
oncekey = (text, category)
[373] Fix | Delete
if onceregistry.get(oncekey):
[374] Fix | Delete
return
[375] Fix | Delete
onceregistry[oncekey] = 1
[376] Fix | Delete
elif action == "always":
[377] Fix | Delete
pass
[378] Fix | Delete
elif action == "module":
[379] Fix | Delete
registry[key] = 1
[380] Fix | Delete
altkey = (text, category, 0)
[381] Fix | Delete
if registry.get(altkey):
[382] Fix | Delete
return
[383] Fix | Delete
registry[altkey] = 1
[384] Fix | Delete
elif action == "default":
[385] Fix | Delete
registry[key] = 1
[386] Fix | Delete
else:
[387] Fix | Delete
# Unrecognized actions are errors
[388] Fix | Delete
raise RuntimeError(
[389] Fix | Delete
"Unrecognized action (%r) in warnings.filters:\n %s" %
[390] Fix | Delete
(action, item))
[391] Fix | Delete
# Print message and context
[392] Fix | Delete
msg = WarningMessage(message, category, filename, lineno, source)
[393] Fix | Delete
_showwarnmsg(msg)
[394] Fix | Delete
[395] Fix | Delete
[396] Fix | Delete
class WarningMessage(object):
[397] Fix | Delete
[398] Fix | Delete
_WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
[399] Fix | Delete
"line", "source")
[400] Fix | Delete
[401] Fix | Delete
def __init__(self, message, category, filename, lineno, file=None,
[402] Fix | Delete
line=None, source=None):
[403] Fix | Delete
self.message = message
[404] Fix | Delete
self.category = category
[405] Fix | Delete
self.filename = filename
[406] Fix | Delete
self.lineno = lineno
[407] Fix | Delete
self.file = file
[408] Fix | Delete
self.line = line
[409] Fix | Delete
self.source = source
[410] Fix | Delete
self._category_name = category.__name__ if category else None
[411] Fix | Delete
[412] Fix | Delete
def __str__(self):
[413] Fix | Delete
return ("{message : %r, category : %r, filename : %r, lineno : %s, "
[414] Fix | Delete
"line : %r}" % (self.message, self._category_name,
[415] Fix | Delete
self.filename, self.lineno, self.line))
[416] Fix | Delete
[417] Fix | Delete
[418] Fix | Delete
class catch_warnings(object):
[419] Fix | Delete
[420] Fix | Delete
"""A context manager that copies and restores the warnings filter upon
[421] Fix | Delete
exiting the context.
[422] Fix | Delete
[423] Fix | Delete
The 'record' argument specifies whether warnings should be captured by a
[424] Fix | Delete
custom implementation of warnings.showwarning() and be appended to a list
[425] Fix | Delete
returned by the context manager. Otherwise None is returned by the context
[426] Fix | Delete
manager. The objects appended to the list are arguments whose attributes
[427] Fix | Delete
mirror the arguments to showwarning().
[428] Fix | Delete
[429] Fix | Delete
The 'module' argument is to specify an alternative module to the module
[430] Fix | Delete
named 'warnings' and imported under that name. This argument is only useful
[431] Fix | Delete
when testing the warnings module itself.
[432] Fix | Delete
[433] Fix | Delete
"""
[434] Fix | Delete
[435] Fix | Delete
def __init__(self, *, record=False, module=None):
[436] Fix | Delete
"""Specify whether to record warnings and if an alternative module
[437] Fix | Delete
should be used other than sys.modules['warnings'].
[438] Fix | Delete
[439] Fix | Delete
For compatibility with Python 3.0, please consider all arguments to be
[440] Fix | Delete
keyword-only.
[441] Fix | Delete
[442] Fix | Delete
"""
[443] Fix | Delete
self._record = record
[444] Fix | Delete
self._module = sys.modules['warnings'] if module is None else module
[445] Fix | Delete
self._entered = False
[446] Fix | Delete
[447] Fix | Delete
def __repr__(self):
[448] Fix | Delete
args = []
[449] Fix | Delete
if self._record:
[450] Fix | Delete
args.append("record=True")
[451] Fix | Delete
if self._module is not sys.modules['warnings']:
[452] Fix | Delete
args.append("module=%r" % self._module)
[453] Fix | Delete
name = type(self).__name__
[454] Fix | Delete
return "%s(%s)" % (name, ", ".join(args))
[455] Fix | Delete
[456] Fix | Delete
def __enter__(self):
[457] Fix | Delete
if self._entered:
[458] Fix | Delete
raise RuntimeError("Cannot enter %r twice" % self)
[459] Fix | Delete
self._entered = True
[460] Fix | Delete
self._filters = self._module.filters
[461] Fix | Delete
self._module.filters = self._filters[:]
[462] Fix | Delete
self._module._filters_mutated()
[463] Fix | Delete
self._showwarning = self._module.showwarning
[464] Fix | Delete
self._showwarnmsg_impl = self._module._showwarnmsg_impl
[465] Fix | Delete
if self._record:
[466] Fix | Delete
log = []
[467] Fix | Delete
self._module._showwarnmsg_impl = log.append
[468] Fix | Delete
# Reset showwarning() to the default implementation to make sure
[469] Fix | Delete
# that _showwarnmsg() calls _showwarnmsg_impl()
[470] Fix | Delete
self._module.showwarning = self._module._showwarning_orig
[471] Fix | Delete
return log
[472] Fix | Delete
else:
[473] Fix | Delete
return None
[474] Fix | Delete
[475] Fix | Delete
def __exit__(self, *exc_info):
[476] Fix | Delete
if not self._entered:
[477] Fix | Delete
raise RuntimeError("Cannot exit %r without entering first" % self)
[478] Fix | Delete
self._module.filters = self._filters
[479] Fix | Delete
self._module._filters_mutated()
[480] Fix | Delete
self._module.showwarning = self._showwarning
[481] Fix | Delete
self._module._showwarnmsg_impl = self._showwarnmsg_impl
[482] Fix | Delete
[483] Fix | Delete
[484] Fix | Delete
# Private utility function called by _PyErr_WarnUnawaitedCoroutine
[485] Fix | Delete
def _warn_unawaited_coroutine(coro):
[486] Fix | Delete
msg_lines = [
[487] Fix | Delete
f"coroutine '{coro.__qualname__}' was never awaited\n"
[488] Fix | Delete
]
[489] Fix | Delete
if coro.cr_origin is not None:
[490] Fix | Delete
import linecache, traceback
[491] Fix | Delete
def extract():
[492] Fix | Delete
for filename, lineno, funcname in reversed(coro.cr_origin):
[493] Fix | Delete
line = linecache.getline(filename, lineno)
[494] Fix | Delete
yield (filename, lineno, funcname, line)
[495] Fix | Delete
msg_lines.append("Coroutine created at (most recent call last)\n")
[496] Fix | Delete
msg_lines += traceback.format_list(list(extract()))
[497] Fix | Delete
msg = "".join(msg_lines).rstrip("\n")
[498] Fix | Delete
# Passing source= here means that if the user happens to have tracemalloc
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function