Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: trace.py
#! /usr/libexec/platform-python -s
[0] Fix | Delete
[1] Fix | Delete
# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
[2] Fix | Delete
# err... reserved and offered to the public under the terms of the
[3] Fix | Delete
# Python 2.2 license.
[4] Fix | Delete
# Author: Zooko O'Whielacronx
[5] Fix | Delete
# http://zooko.com/
[6] Fix | Delete
# mailto:zooko@zooko.com
[7] Fix | Delete
#
[8] Fix | Delete
# Copyright 2000, Mojam Media, Inc., all rights reserved.
[9] Fix | Delete
# Author: Skip Montanaro
[10] Fix | Delete
#
[11] Fix | Delete
# Copyright 1999, Bioreason, Inc., all rights reserved.
[12] Fix | Delete
# Author: Andrew Dalke
[13] Fix | Delete
#
[14] Fix | Delete
# Copyright 1995-1997, Automatrix, Inc., all rights reserved.
[15] Fix | Delete
# Author: Skip Montanaro
[16] Fix | Delete
#
[17] Fix | Delete
# Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.
[18] Fix | Delete
#
[19] Fix | Delete
#
[20] Fix | Delete
# Permission to use, copy, modify, and distribute this Python software and
[21] Fix | Delete
# its associated documentation for any purpose without fee is hereby
[22] Fix | Delete
# granted, provided that the above copyright notice appears in all copies,
[23] Fix | Delete
# and that both that copyright notice and this permission notice appear in
[24] Fix | Delete
# supporting documentation, and that the name of neither Automatrix,
[25] Fix | Delete
# Bioreason or Mojam Media be used in advertising or publicity pertaining to
[26] Fix | Delete
# distribution of the software without specific, written prior permission.
[27] Fix | Delete
#
[28] Fix | Delete
"""program/module to trace Python program or function execution
[29] Fix | Delete
[30] Fix | Delete
Sample use, command line:
[31] Fix | Delete
trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
[32] Fix | Delete
trace.py -t --ignore-dir '$prefix' spam.py eggs
[33] Fix | Delete
trace.py --trackcalls spam.py eggs
[34] Fix | Delete
[35] Fix | Delete
Sample use, programmatically
[36] Fix | Delete
import sys
[37] Fix | Delete
[38] Fix | Delete
# create a Trace object, telling it what to ignore, and whether to
[39] Fix | Delete
# do tracing or line-counting or both.
[40] Fix | Delete
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
[41] Fix | Delete
trace=0, count=1)
[42] Fix | Delete
# run the new command using the given tracer
[43] Fix | Delete
tracer.run('main()')
[44] Fix | Delete
# make a report, placing output in /tmp
[45] Fix | Delete
r = tracer.results()
[46] Fix | Delete
r.write_results(show_missing=True, coverdir="/tmp")
[47] Fix | Delete
"""
[48] Fix | Delete
__all__ = ['Trace', 'CoverageResults']
[49] Fix | Delete
[50] Fix | Delete
import linecache
[51] Fix | Delete
import os
[52] Fix | Delete
import sys
[53] Fix | Delete
import sysconfig
[54] Fix | Delete
import token
[55] Fix | Delete
import tokenize
[56] Fix | Delete
import inspect
[57] Fix | Delete
import gc
[58] Fix | Delete
import dis
[59] Fix | Delete
import pickle
[60] Fix | Delete
from time import monotonic as _time
[61] Fix | Delete
[62] Fix | Delete
import threading
[63] Fix | Delete
[64] Fix | Delete
PRAGMA_NOCOVER = "#pragma NO COVER"
[65] Fix | Delete
[66] Fix | Delete
class _Ignore:
[67] Fix | Delete
def __init__(self, modules=None, dirs=None):
[68] Fix | Delete
self._mods = set() if not modules else set(modules)
[69] Fix | Delete
self._dirs = [] if not dirs else [os.path.normpath(d)
[70] Fix | Delete
for d in dirs]
[71] Fix | Delete
self._ignore = { '<string>': 1 }
[72] Fix | Delete
[73] Fix | Delete
def names(self, filename, modulename):
[74] Fix | Delete
if modulename in self._ignore:
[75] Fix | Delete
return self._ignore[modulename]
[76] Fix | Delete
[77] Fix | Delete
# haven't seen this one before, so see if the module name is
[78] Fix | Delete
# on the ignore list.
[79] Fix | Delete
if modulename in self._mods: # Identical names, so ignore
[80] Fix | Delete
self._ignore[modulename] = 1
[81] Fix | Delete
return 1
[82] Fix | Delete
[83] Fix | Delete
# check if the module is a proper submodule of something on
[84] Fix | Delete
# the ignore list
[85] Fix | Delete
for mod in self._mods:
[86] Fix | Delete
# Need to take some care since ignoring
[87] Fix | Delete
# "cmp" mustn't mean ignoring "cmpcache" but ignoring
[88] Fix | Delete
# "Spam" must also mean ignoring "Spam.Eggs".
[89] Fix | Delete
if modulename.startswith(mod + '.'):
[90] Fix | Delete
self._ignore[modulename] = 1
[91] Fix | Delete
return 1
[92] Fix | Delete
[93] Fix | Delete
# Now check that filename isn't in one of the directories
[94] Fix | Delete
if filename is None:
[95] Fix | Delete
# must be a built-in, so we must ignore
[96] Fix | Delete
self._ignore[modulename] = 1
[97] Fix | Delete
return 1
[98] Fix | Delete
[99] Fix | Delete
# Ignore a file when it contains one of the ignorable paths
[100] Fix | Delete
for d in self._dirs:
[101] Fix | Delete
# The '+ os.sep' is to ensure that d is a parent directory,
[102] Fix | Delete
# as compared to cases like:
[103] Fix | Delete
# d = "/usr/local"
[104] Fix | Delete
# filename = "/usr/local.py"
[105] Fix | Delete
# or
[106] Fix | Delete
# d = "/usr/local.py"
[107] Fix | Delete
# filename = "/usr/local.py"
[108] Fix | Delete
if filename.startswith(d + os.sep):
[109] Fix | Delete
self._ignore[modulename] = 1
[110] Fix | Delete
return 1
[111] Fix | Delete
[112] Fix | Delete
# Tried the different ways, so we don't ignore this module
[113] Fix | Delete
self._ignore[modulename] = 0
[114] Fix | Delete
return 0
[115] Fix | Delete
[116] Fix | Delete
def _modname(path):
[117] Fix | Delete
"""Return a plausible module name for the patch."""
[118] Fix | Delete
[119] Fix | Delete
base = os.path.basename(path)
[120] Fix | Delete
filename, ext = os.path.splitext(base)
[121] Fix | Delete
return filename
[122] Fix | Delete
[123] Fix | Delete
def _fullmodname(path):
[124] Fix | Delete
"""Return a plausible module name for the path."""
[125] Fix | Delete
[126] Fix | Delete
# If the file 'path' is part of a package, then the filename isn't
[127] Fix | Delete
# enough to uniquely identify it. Try to do the right thing by
[128] Fix | Delete
# looking in sys.path for the longest matching prefix. We'll
[129] Fix | Delete
# assume that the rest is the package name.
[130] Fix | Delete
[131] Fix | Delete
comparepath = os.path.normcase(path)
[132] Fix | Delete
longest = ""
[133] Fix | Delete
for dir in sys.path:
[134] Fix | Delete
dir = os.path.normcase(dir)
[135] Fix | Delete
if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
[136] Fix | Delete
if len(dir) > len(longest):
[137] Fix | Delete
longest = dir
[138] Fix | Delete
[139] Fix | Delete
if longest:
[140] Fix | Delete
base = path[len(longest) + 1:]
[141] Fix | Delete
else:
[142] Fix | Delete
base = path
[143] Fix | Delete
# the drive letter is never part of the module name
[144] Fix | Delete
drive, base = os.path.splitdrive(base)
[145] Fix | Delete
base = base.replace(os.sep, ".")
[146] Fix | Delete
if os.altsep:
[147] Fix | Delete
base = base.replace(os.altsep, ".")
[148] Fix | Delete
filename, ext = os.path.splitext(base)
[149] Fix | Delete
return filename.lstrip(".")
[150] Fix | Delete
[151] Fix | Delete
class CoverageResults:
[152] Fix | Delete
def __init__(self, counts=None, calledfuncs=None, infile=None,
[153] Fix | Delete
callers=None, outfile=None):
[154] Fix | Delete
self.counts = counts
[155] Fix | Delete
if self.counts is None:
[156] Fix | Delete
self.counts = {}
[157] Fix | Delete
self.counter = self.counts.copy() # map (filename, lineno) to count
[158] Fix | Delete
self.calledfuncs = calledfuncs
[159] Fix | Delete
if self.calledfuncs is None:
[160] Fix | Delete
self.calledfuncs = {}
[161] Fix | Delete
self.calledfuncs = self.calledfuncs.copy()
[162] Fix | Delete
self.callers = callers
[163] Fix | Delete
if self.callers is None:
[164] Fix | Delete
self.callers = {}
[165] Fix | Delete
self.callers = self.callers.copy()
[166] Fix | Delete
self.infile = infile
[167] Fix | Delete
self.outfile = outfile
[168] Fix | Delete
if self.infile:
[169] Fix | Delete
# Try to merge existing counts file.
[170] Fix | Delete
try:
[171] Fix | Delete
with open(self.infile, 'rb') as f:
[172] Fix | Delete
counts, calledfuncs, callers = pickle.load(f)
[173] Fix | Delete
self.update(self.__class__(counts, calledfuncs, callers))
[174] Fix | Delete
except (OSError, EOFError, ValueError) as err:
[175] Fix | Delete
print(("Skipping counts file %r: %s"
[176] Fix | Delete
% (self.infile, err)), file=sys.stderr)
[177] Fix | Delete
[178] Fix | Delete
def is_ignored_filename(self, filename):
[179] Fix | Delete
"""Return True if the filename does not refer to a file
[180] Fix | Delete
we want to have reported.
[181] Fix | Delete
"""
[182] Fix | Delete
return filename.startswith('<') and filename.endswith('>')
[183] Fix | Delete
[184] Fix | Delete
def update(self, other):
[185] Fix | Delete
"""Merge in the data from another CoverageResults"""
[186] Fix | Delete
counts = self.counts
[187] Fix | Delete
calledfuncs = self.calledfuncs
[188] Fix | Delete
callers = self.callers
[189] Fix | Delete
other_counts = other.counts
[190] Fix | Delete
other_calledfuncs = other.calledfuncs
[191] Fix | Delete
other_callers = other.callers
[192] Fix | Delete
[193] Fix | Delete
for key in other_counts:
[194] Fix | Delete
counts[key] = counts.get(key, 0) + other_counts[key]
[195] Fix | Delete
[196] Fix | Delete
for key in other_calledfuncs:
[197] Fix | Delete
calledfuncs[key] = 1
[198] Fix | Delete
[199] Fix | Delete
for key in other_callers:
[200] Fix | Delete
callers[key] = 1
[201] Fix | Delete
[202] Fix | Delete
def write_results(self, show_missing=True, summary=False, coverdir=None):
[203] Fix | Delete
"""
[204] Fix | Delete
Write the coverage results.
[205] Fix | Delete
[206] Fix | Delete
:param show_missing: Show lines that had no hits.
[207] Fix | Delete
:param summary: Include coverage summary per module.
[208] Fix | Delete
:param coverdir: If None, the results of each module are placed in its
[209] Fix | Delete
directory, otherwise it is included in the directory
[210] Fix | Delete
specified.
[211] Fix | Delete
"""
[212] Fix | Delete
if self.calledfuncs:
[213] Fix | Delete
print()
[214] Fix | Delete
print("functions called:")
[215] Fix | Delete
calls = self.calledfuncs
[216] Fix | Delete
for filename, modulename, funcname in sorted(calls):
[217] Fix | Delete
print(("filename: %s, modulename: %s, funcname: %s"
[218] Fix | Delete
% (filename, modulename, funcname)))
[219] Fix | Delete
[220] Fix | Delete
if self.callers:
[221] Fix | Delete
print()
[222] Fix | Delete
print("calling relationships:")
[223] Fix | Delete
lastfile = lastcfile = ""
[224] Fix | Delete
for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
[225] Fix | Delete
in sorted(self.callers):
[226] Fix | Delete
if pfile != lastfile:
[227] Fix | Delete
print()
[228] Fix | Delete
print("***", pfile, "***")
[229] Fix | Delete
lastfile = pfile
[230] Fix | Delete
lastcfile = ""
[231] Fix | Delete
if cfile != pfile and lastcfile != cfile:
[232] Fix | Delete
print(" -->", cfile)
[233] Fix | Delete
lastcfile = cfile
[234] Fix | Delete
print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc))
[235] Fix | Delete
[236] Fix | Delete
# turn the counts data ("(filename, lineno) = count") into something
[237] Fix | Delete
# accessible on a per-file basis
[238] Fix | Delete
per_file = {}
[239] Fix | Delete
for filename, lineno in self.counts:
[240] Fix | Delete
lines_hit = per_file[filename] = per_file.get(filename, {})
[241] Fix | Delete
lines_hit[lineno] = self.counts[(filename, lineno)]
[242] Fix | Delete
[243] Fix | Delete
# accumulate summary info, if needed
[244] Fix | Delete
sums = {}
[245] Fix | Delete
[246] Fix | Delete
for filename, count in per_file.items():
[247] Fix | Delete
if self.is_ignored_filename(filename):
[248] Fix | Delete
continue
[249] Fix | Delete
[250] Fix | Delete
if filename.endswith(".pyc"):
[251] Fix | Delete
filename = filename[:-1]
[252] Fix | Delete
[253] Fix | Delete
if coverdir is None:
[254] Fix | Delete
dir = os.path.dirname(os.path.abspath(filename))
[255] Fix | Delete
modulename = _modname(filename)
[256] Fix | Delete
else:
[257] Fix | Delete
dir = coverdir
[258] Fix | Delete
if not os.path.exists(dir):
[259] Fix | Delete
os.makedirs(dir)
[260] Fix | Delete
modulename = _fullmodname(filename)
[261] Fix | Delete
[262] Fix | Delete
# If desired, get a list of the line numbers which represent
[263] Fix | Delete
# executable content (returned as a dict for better lookup speed)
[264] Fix | Delete
if show_missing:
[265] Fix | Delete
lnotab = _find_executable_linenos(filename)
[266] Fix | Delete
else:
[267] Fix | Delete
lnotab = {}
[268] Fix | Delete
source = linecache.getlines(filename)
[269] Fix | Delete
coverpath = os.path.join(dir, modulename + ".cover")
[270] Fix | Delete
with open(filename, 'rb') as fp:
[271] Fix | Delete
encoding, _ = tokenize.detect_encoding(fp.readline)
[272] Fix | Delete
n_hits, n_lines = self.write_results_file(coverpath, source,
[273] Fix | Delete
lnotab, count, encoding)
[274] Fix | Delete
if summary and n_lines:
[275] Fix | Delete
percent = int(100 * n_hits / n_lines)
[276] Fix | Delete
sums[modulename] = n_lines, percent, modulename, filename
[277] Fix | Delete
[278] Fix | Delete
[279] Fix | Delete
if summary and sums:
[280] Fix | Delete
print("lines cov% module (path)")
[281] Fix | Delete
for m in sorted(sums):
[282] Fix | Delete
n_lines, percent, modulename, filename = sums[m]
[283] Fix | Delete
print("%5d %3d%% %s (%s)" % sums[m])
[284] Fix | Delete
[285] Fix | Delete
if self.outfile:
[286] Fix | Delete
# try and store counts and module info into self.outfile
[287] Fix | Delete
try:
[288] Fix | Delete
with open(self.outfile, 'wb') as f:
[289] Fix | Delete
pickle.dump((self.counts, self.calledfuncs, self.callers),
[290] Fix | Delete
f, 1)
[291] Fix | Delete
except OSError as err:
[292] Fix | Delete
print("Can't save counts files because %s" % err, file=sys.stderr)
[293] Fix | Delete
[294] Fix | Delete
def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
[295] Fix | Delete
"""Return a coverage results file in path."""
[296] Fix | Delete
# ``lnotab`` is a dict of executable lines, or a line number "table"
[297] Fix | Delete
[298] Fix | Delete
try:
[299] Fix | Delete
outfile = open(path, "w", encoding=encoding)
[300] Fix | Delete
except OSError as err:
[301] Fix | Delete
print(("trace: Could not open %r for writing: %s "
[302] Fix | Delete
"- skipping" % (path, err)), file=sys.stderr)
[303] Fix | Delete
return 0, 0
[304] Fix | Delete
[305] Fix | Delete
n_lines = 0
[306] Fix | Delete
n_hits = 0
[307] Fix | Delete
with outfile:
[308] Fix | Delete
for lineno, line in enumerate(lines, 1):
[309] Fix | Delete
# do the blank/comment match to try to mark more lines
[310] Fix | Delete
# (help the reader find stuff that hasn't been covered)
[311] Fix | Delete
if lineno in lines_hit:
[312] Fix | Delete
outfile.write("%5d: " % lines_hit[lineno])
[313] Fix | Delete
n_hits += 1
[314] Fix | Delete
n_lines += 1
[315] Fix | Delete
elif lineno in lnotab and not PRAGMA_NOCOVER in line:
[316] Fix | Delete
# Highlight never-executed lines, unless the line contains
[317] Fix | Delete
# #pragma: NO COVER
[318] Fix | Delete
outfile.write(">>>>>> ")
[319] Fix | Delete
n_lines += 1
[320] Fix | Delete
else:
[321] Fix | Delete
outfile.write(" ")
[322] Fix | Delete
outfile.write(line.expandtabs(8))
[323] Fix | Delete
[324] Fix | Delete
return n_hits, n_lines
[325] Fix | Delete
[326] Fix | Delete
def _find_lines_from_code(code, strs):
[327] Fix | Delete
"""Return dict where keys are lines in the line number table."""
[328] Fix | Delete
linenos = {}
[329] Fix | Delete
[330] Fix | Delete
for _, lineno in dis.findlinestarts(code):
[331] Fix | Delete
if lineno not in strs:
[332] Fix | Delete
linenos[lineno] = 1
[333] Fix | Delete
[334] Fix | Delete
return linenos
[335] Fix | Delete
[336] Fix | Delete
def _find_lines(code, strs):
[337] Fix | Delete
"""Return lineno dict for all code objects reachable from code."""
[338] Fix | Delete
# get all of the lineno information from the code of this scope level
[339] Fix | Delete
linenos = _find_lines_from_code(code, strs)
[340] Fix | Delete
[341] Fix | Delete
# and check the constants for references to other code objects
[342] Fix | Delete
for c in code.co_consts:
[343] Fix | Delete
if inspect.iscode(c):
[344] Fix | Delete
# find another code object, so recurse into it
[345] Fix | Delete
linenos.update(_find_lines(c, strs))
[346] Fix | Delete
return linenos
[347] Fix | Delete
[348] Fix | Delete
def _find_strings(filename, encoding=None):
[349] Fix | Delete
"""Return a dict of possible docstring positions.
[350] Fix | Delete
[351] Fix | Delete
The dict maps line numbers to strings. There is an entry for
[352] Fix | Delete
line that contains only a string or a part of a triple-quoted
[353] Fix | Delete
string.
[354] Fix | Delete
"""
[355] Fix | Delete
d = {}
[356] Fix | Delete
# If the first token is a string, then it's the module docstring.
[357] Fix | Delete
# Add this special case so that the test in the loop passes.
[358] Fix | Delete
prev_ttype = token.INDENT
[359] Fix | Delete
with open(filename, encoding=encoding) as f:
[360] Fix | Delete
tok = tokenize.generate_tokens(f.readline)
[361] Fix | Delete
for ttype, tstr, start, end, line in tok:
[362] Fix | Delete
if ttype == token.STRING:
[363] Fix | Delete
if prev_ttype == token.INDENT:
[364] Fix | Delete
sline, scol = start
[365] Fix | Delete
eline, ecol = end
[366] Fix | Delete
for i in range(sline, eline + 1):
[367] Fix | Delete
d[i] = 1
[368] Fix | Delete
prev_ttype = ttype
[369] Fix | Delete
return d
[370] Fix | Delete
[371] Fix | Delete
def _find_executable_linenos(filename):
[372] Fix | Delete
"""Return dict where keys are line numbers in the line number table."""
[373] Fix | Delete
try:
[374] Fix | Delete
with tokenize.open(filename) as f:
[375] Fix | Delete
prog = f.read()
[376] Fix | Delete
encoding = f.encoding
[377] Fix | Delete
except OSError as err:
[378] Fix | Delete
print(("Not printing coverage data for %r: %s"
[379] Fix | Delete
% (filename, err)), file=sys.stderr)
[380] Fix | Delete
return {}
[381] Fix | Delete
code = compile(prog, filename, "exec")
[382] Fix | Delete
strs = _find_strings(filename, encoding)
[383] Fix | Delete
return _find_lines(code, strs)
[384] Fix | Delete
[385] Fix | Delete
class Trace:
[386] Fix | Delete
def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
[387] Fix | Delete
ignoremods=(), ignoredirs=(), infile=None, outfile=None,
[388] Fix | Delete
timing=False):
[389] Fix | Delete
"""
[390] Fix | Delete
@param count true iff it should count number of times each
[391] Fix | Delete
line is executed
[392] Fix | Delete
@param trace true iff it should print out each line that is
[393] Fix | Delete
being counted
[394] Fix | Delete
@param countfuncs true iff it should just output a list of
[395] Fix | Delete
(filename, modulename, funcname,) for functions
[396] Fix | Delete
that were called at least once; This overrides
[397] Fix | Delete
`count' and `trace'
[398] Fix | Delete
@param ignoremods a list of the names of modules to ignore
[399] Fix | Delete
@param ignoredirs a list of the names of directories to ignore
[400] Fix | Delete
all of the (recursive) contents of
[401] Fix | Delete
@param infile file from which to read stored counts to be
[402] Fix | Delete
added into the results
[403] Fix | Delete
@param outfile file in which to write the results
[404] Fix | Delete
@param timing true iff timing information be displayed
[405] Fix | Delete
"""
[406] Fix | Delete
self.infile = infile
[407] Fix | Delete
self.outfile = outfile
[408] Fix | Delete
self.ignore = _Ignore(ignoremods, ignoredirs)
[409] Fix | Delete
self.counts = {} # keys are (filename, linenumber)
[410] Fix | Delete
self.pathtobasename = {} # for memoizing os.path.basename
[411] Fix | Delete
self.donothing = 0
[412] Fix | Delete
self.trace = trace
[413] Fix | Delete
self._calledfuncs = {}
[414] Fix | Delete
self._callers = {}
[415] Fix | Delete
self._caller_cache = {}
[416] Fix | Delete
self.start_time = None
[417] Fix | Delete
if timing:
[418] Fix | Delete
self.start_time = _time()
[419] Fix | Delete
if countcallers:
[420] Fix | Delete
self.globaltrace = self.globaltrace_trackcallers
[421] Fix | Delete
elif countfuncs:
[422] Fix | Delete
self.globaltrace = self.globaltrace_countfuncs
[423] Fix | Delete
elif trace and count:
[424] Fix | Delete
self.globaltrace = self.globaltrace_lt
[425] Fix | Delete
self.localtrace = self.localtrace_trace_and_count
[426] Fix | Delete
elif trace:
[427] Fix | Delete
self.globaltrace = self.globaltrace_lt
[428] Fix | Delete
self.localtrace = self.localtrace_trace
[429] Fix | Delete
elif count:
[430] Fix | Delete
self.globaltrace = self.globaltrace_lt
[431] Fix | Delete
self.localtrace = self.localtrace_count
[432] Fix | Delete
else:
[433] Fix | Delete
# Ahem -- do nothing? Okay.
[434] Fix | Delete
self.donothing = 1
[435] Fix | Delete
[436] Fix | Delete
def run(self, cmd):
[437] Fix | Delete
import __main__
[438] Fix | Delete
dict = __main__.__dict__
[439] Fix | Delete
self.runctx(cmd, dict, dict)
[440] Fix | Delete
[441] Fix | Delete
def runctx(self, cmd, globals=None, locals=None):
[442] Fix | Delete
if globals is None: globals = {}
[443] Fix | Delete
if locals is None: locals = {}
[444] Fix | Delete
if not self.donothing:
[445] Fix | Delete
threading.settrace(self.globaltrace)
[446] Fix | Delete
sys.settrace(self.globaltrace)
[447] Fix | Delete
try:
[448] Fix | Delete
exec(cmd, globals, locals)
[449] Fix | Delete
finally:
[450] Fix | Delete
if not self.donothing:
[451] Fix | Delete
sys.settrace(None)
[452] Fix | Delete
threading.settrace(None)
[453] Fix | Delete
[454] Fix | Delete
def runfunc(self, func, /, *args, **kw):
[455] Fix | Delete
result = None
[456] Fix | Delete
if not self.donothing:
[457] Fix | Delete
sys.settrace(self.globaltrace)
[458] Fix | Delete
try:
[459] Fix | Delete
result = func(*args, **kw)
[460] Fix | Delete
finally:
[461] Fix | Delete
if not self.donothing:
[462] Fix | Delete
sys.settrace(None)
[463] Fix | Delete
return result
[464] Fix | Delete
[465] Fix | Delete
def file_module_function_of(self, frame):
[466] Fix | Delete
code = frame.f_code
[467] Fix | Delete
filename = code.co_filename
[468] Fix | Delete
if filename:
[469] Fix | Delete
modulename = _modname(filename)
[470] Fix | Delete
else:
[471] Fix | Delete
modulename = None
[472] Fix | Delete
[473] Fix | Delete
funcname = code.co_name
[474] Fix | Delete
clsname = None
[475] Fix | Delete
if code in self._caller_cache:
[476] Fix | Delete
if self._caller_cache[code] is not None:
[477] Fix | Delete
clsname = self._caller_cache[code]
[478] Fix | Delete
else:
[479] Fix | Delete
self._caller_cache[code] = None
[480] Fix | Delete
## use of gc.get_referrers() was suggested by Michael Hudson
[481] Fix | Delete
# all functions which refer to this code object
[482] Fix | Delete
funcs = [f for f in gc.get_referrers(code)
[483] Fix | Delete
if inspect.isfunction(f)]
[484] Fix | Delete
# require len(func) == 1 to avoid ambiguity caused by calls to
[485] Fix | Delete
# new.function(): "In the face of ambiguity, refuse the
[486] Fix | Delete
# temptation to guess."
[487] Fix | Delete
if len(funcs) == 1:
[488] Fix | Delete
dicts = [d for d in gc.get_referrers(funcs[0])
[489] Fix | Delete
if isinstance(d, dict)]
[490] Fix | Delete
if len(dicts) == 1:
[491] Fix | Delete
classes = [c for c in gc.get_referrers(dicts[0])
[492] Fix | Delete
if hasattr(c, "__bases__")]
[493] Fix | Delete
if len(classes) == 1:
[494] Fix | Delete
# ditto for new.classobj()
[495] Fix | Delete
clsname = classes[0].__name__
[496] Fix | Delete
# cache the result - assumption is that new.* is
[497] Fix | Delete
# not called later to disturb this relationship
[498] Fix | Delete
# _caller_cache could be flushed if functions in
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function