Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3....
File: trace.py
funcs = [f for f in gc.get_referrers(code)
[500] Fix | Delete
if inspect.isfunction(f)]
[501] Fix | Delete
# require len(func) == 1 to avoid ambiguity caused by calls to
[502] Fix | Delete
# new.function(): "In the face of ambiguity, refuse the
[503] Fix | Delete
# temptation to guess."
[504] Fix | Delete
if len(funcs) == 1:
[505] Fix | Delete
dicts = [d for d in gc.get_referrers(funcs[0])
[506] Fix | Delete
if isinstance(d, dict)]
[507] Fix | Delete
if len(dicts) == 1:
[508] Fix | Delete
classes = [c for c in gc.get_referrers(dicts[0])
[509] Fix | Delete
if hasattr(c, "__bases__")]
[510] Fix | Delete
if len(classes) == 1:
[511] Fix | Delete
# ditto for new.classobj()
[512] Fix | Delete
clsname = classes[0].__name__
[513] Fix | Delete
# cache the result - assumption is that new.* is
[514] Fix | Delete
# not called later to disturb this relationship
[515] Fix | Delete
# _caller_cache could be flushed if functions in
[516] Fix | Delete
# the new module get called.
[517] Fix | Delete
self._caller_cache[code] = clsname
[518] Fix | Delete
if clsname is not None:
[519] Fix | Delete
funcname = "%s.%s" % (clsname, funcname)
[520] Fix | Delete
[521] Fix | Delete
return filename, modulename, funcname
[522] Fix | Delete
[523] Fix | Delete
def globaltrace_trackcallers(self, frame, why, arg):
[524] Fix | Delete
"""Handler for call events.
[525] Fix | Delete
[526] Fix | Delete
Adds information about who called who to the self._callers dict.
[527] Fix | Delete
"""
[528] Fix | Delete
if why == 'call':
[529] Fix | Delete
# XXX Should do a better job of identifying methods
[530] Fix | Delete
this_func = self.file_module_function_of(frame)
[531] Fix | Delete
parent_func = self.file_module_function_of(frame.f_back)
[532] Fix | Delete
self._callers[(parent_func, this_func)] = 1
[533] Fix | Delete
[534] Fix | Delete
def globaltrace_countfuncs(self, frame, why, arg):
[535] Fix | Delete
"""Handler for call events.
[536] Fix | Delete
[537] Fix | Delete
Adds (filename, modulename, funcname) to the self._calledfuncs dict.
[538] Fix | Delete
"""
[539] Fix | Delete
if why == 'call':
[540] Fix | Delete
this_func = self.file_module_function_of(frame)
[541] Fix | Delete
self._calledfuncs[this_func] = 1
[542] Fix | Delete
[543] Fix | Delete
def globaltrace_lt(self, frame, why, arg):
[544] Fix | Delete
"""Handler for call events.
[545] Fix | Delete
[546] Fix | Delete
If the code block being entered is to be ignored, returns `None',
[547] Fix | Delete
else returns self.localtrace.
[548] Fix | Delete
"""
[549] Fix | Delete
if why == 'call':
[550] Fix | Delete
code = frame.f_code
[551] Fix | Delete
filename = frame.f_globals.get('__file__', None)
[552] Fix | Delete
if filename:
[553] Fix | Delete
# XXX _modname() doesn't work right for packages, so
[554] Fix | Delete
# the ignore support won't work right for packages
[555] Fix | Delete
modulename = _modname(filename)
[556] Fix | Delete
if modulename is not None:
[557] Fix | Delete
ignore_it = self.ignore.names(filename, modulename)
[558] Fix | Delete
if not ignore_it:
[559] Fix | Delete
if self.trace:
[560] Fix | Delete
print((" --- modulename: %s, funcname: %s"
[561] Fix | Delete
% (modulename, code.co_name)))
[562] Fix | Delete
return self.localtrace
[563] Fix | Delete
else:
[564] Fix | Delete
return None
[565] Fix | Delete
[566] Fix | Delete
def localtrace_trace_and_count(self, frame, why, arg):
[567] Fix | Delete
if why == "line":
[568] Fix | Delete
# record the file name and line number of every trace
[569] Fix | Delete
filename = frame.f_code.co_filename
[570] Fix | Delete
lineno = frame.f_lineno
[571] Fix | Delete
key = filename, lineno
[572] Fix | Delete
self.counts[key] = self.counts.get(key, 0) + 1
[573] Fix | Delete
[574] Fix | Delete
if self.start_time:
[575] Fix | Delete
print('%.2f' % (_time() - self.start_time), end=' ')
[576] Fix | Delete
bname = os.path.basename(filename)
[577] Fix | Delete
print("%s(%d): %s" % (bname, lineno,
[578] Fix | Delete
linecache.getline(filename, lineno)), end='')
[579] Fix | Delete
return self.localtrace
[580] Fix | Delete
[581] Fix | Delete
def localtrace_trace(self, frame, why, arg):
[582] Fix | Delete
if why == "line":
[583] Fix | Delete
# record the file name and line number of every trace
[584] Fix | Delete
filename = frame.f_code.co_filename
[585] Fix | Delete
lineno = frame.f_lineno
[586] Fix | Delete
[587] Fix | Delete
if self.start_time:
[588] Fix | Delete
print('%.2f' % (_time() - self.start_time), end=' ')
[589] Fix | Delete
bname = os.path.basename(filename)
[590] Fix | Delete
print("%s(%d): %s" % (bname, lineno,
[591] Fix | Delete
linecache.getline(filename, lineno)), end='')
[592] Fix | Delete
return self.localtrace
[593] Fix | Delete
[594] Fix | Delete
def localtrace_count(self, frame, why, arg):
[595] Fix | Delete
if why == "line":
[596] Fix | Delete
filename = frame.f_code.co_filename
[597] Fix | Delete
lineno = frame.f_lineno
[598] Fix | Delete
key = filename, lineno
[599] Fix | Delete
self.counts[key] = self.counts.get(key, 0) + 1
[600] Fix | Delete
return self.localtrace
[601] Fix | Delete
[602] Fix | Delete
def results(self):
[603] Fix | Delete
return CoverageResults(self.counts, infile=self.infile,
[604] Fix | Delete
outfile=self.outfile,
[605] Fix | Delete
calledfuncs=self._calledfuncs,
[606] Fix | Delete
callers=self._callers)
[607] Fix | Delete
[608] Fix | Delete
def main():
[609] Fix | Delete
import argparse
[610] Fix | Delete
[611] Fix | Delete
parser = argparse.ArgumentParser()
[612] Fix | Delete
parser.add_argument('--version', action='version', version='trace 2.0')
[613] Fix | Delete
[614] Fix | Delete
grp = parser.add_argument_group('Main options',
[615] Fix | Delete
'One of these (or --report) must be given')
[616] Fix | Delete
[617] Fix | Delete
grp.add_argument('-c', '--count', action='store_true',
[618] Fix | Delete
help='Count the number of times each line is executed and write '
[619] Fix | Delete
'the counts to <module>.cover for each module executed, in '
[620] Fix | Delete
'the module\'s directory. See also --coverdir, --file, '
[621] Fix | Delete
'--no-report below.')
[622] Fix | Delete
grp.add_argument('-t', '--trace', action='store_true',
[623] Fix | Delete
help='Print each line to sys.stdout before it is executed')
[624] Fix | Delete
grp.add_argument('-l', '--listfuncs', action='store_true',
[625] Fix | Delete
help='Keep track of which functions are executed at least once '
[626] Fix | Delete
'and write the results to sys.stdout after the program exits. '
[627] Fix | Delete
'Cannot be specified alongside --trace or --count.')
[628] Fix | Delete
grp.add_argument('-T', '--trackcalls', action='store_true',
[629] Fix | Delete
help='Keep track of caller/called pairs and write the results to '
[630] Fix | Delete
'sys.stdout after the program exits.')
[631] Fix | Delete
[632] Fix | Delete
grp = parser.add_argument_group('Modifiers')
[633] Fix | Delete
[634] Fix | Delete
_grp = grp.add_mutually_exclusive_group()
[635] Fix | Delete
_grp.add_argument('-r', '--report', action='store_true',
[636] Fix | Delete
help='Generate a report from a counts file; does not execute any '
[637] Fix | Delete
'code. --file must specify the results file to read, which '
[638] Fix | Delete
'must have been created in a previous run with --count '
[639] Fix | Delete
'--file=FILE')
[640] Fix | Delete
_grp.add_argument('-R', '--no-report', action='store_true',
[641] Fix | Delete
help='Do not generate the coverage report files. '
[642] Fix | Delete
'Useful if you want to accumulate over several runs.')
[643] Fix | Delete
[644] Fix | Delete
grp.add_argument('-f', '--file',
[645] Fix | Delete
help='File to accumulate counts over several runs')
[646] Fix | Delete
grp.add_argument('-C', '--coverdir',
[647] Fix | Delete
help='Directory where the report files go. The coverage report '
[648] Fix | Delete
'for <package>.<module> will be written to file '
[649] Fix | Delete
'<dir>/<package>/<module>.cover')
[650] Fix | Delete
grp.add_argument('-m', '--missing', action='store_true',
[651] Fix | Delete
help='Annotate executable lines that were not executed with '
[652] Fix | Delete
'">>>>>> "')
[653] Fix | Delete
grp.add_argument('-s', '--summary', action='store_true',
[654] Fix | Delete
help='Write a brief summary for each file to sys.stdout. '
[655] Fix | Delete
'Can only be used with --count or --report')
[656] Fix | Delete
grp.add_argument('-g', '--timing', action='store_true',
[657] Fix | Delete
help='Prefix each line with the time since the program started. '
[658] Fix | Delete
'Only used while tracing')
[659] Fix | Delete
[660] Fix | Delete
grp = parser.add_argument_group('Filters',
[661] Fix | Delete
'Can be specified multiple times')
[662] Fix | Delete
grp.add_argument('--ignore-module', action='append', default=[],
[663] Fix | Delete
help='Ignore the given module(s) and its submodules '
[664] Fix | Delete
'(if it is a package). Accepts comma separated list of '
[665] Fix | Delete
'module names.')
[666] Fix | Delete
grp.add_argument('--ignore-dir', action='append', default=[],
[667] Fix | Delete
help='Ignore files in the given directory '
[668] Fix | Delete
'(multiple directories can be joined by os.pathsep).')
[669] Fix | Delete
[670] Fix | Delete
parser.add_argument('--module', action='store_true', default=False,
[671] Fix | Delete
help='Trace a module. ')
[672] Fix | Delete
parser.add_argument('progname', nargs='?',
[673] Fix | Delete
help='file to run as main program')
[674] Fix | Delete
parser.add_argument('arguments', nargs=argparse.REMAINDER,
[675] Fix | Delete
help='arguments to the program')
[676] Fix | Delete
[677] Fix | Delete
opts = parser.parse_args()
[678] Fix | Delete
[679] Fix | Delete
if opts.ignore_dir:
[680] Fix | Delete
_prefix = sysconfig.get_path("stdlib")
[681] Fix | Delete
_exec_prefix = sysconfig.get_path("platstdlib")
[682] Fix | Delete
[683] Fix | Delete
def parse_ignore_dir(s):
[684] Fix | Delete
s = os.path.expanduser(os.path.expandvars(s))
[685] Fix | Delete
s = s.replace('$prefix', _prefix).replace('$exec_prefix', _exec_prefix)
[686] Fix | Delete
return os.path.normpath(s)
[687] Fix | Delete
[688] Fix | Delete
opts.ignore_module = [mod.strip()
[689] Fix | Delete
for i in opts.ignore_module for mod in i.split(',')]
[690] Fix | Delete
opts.ignore_dir = [parse_ignore_dir(s)
[691] Fix | Delete
for i in opts.ignore_dir for s in i.split(os.pathsep)]
[692] Fix | Delete
[693] Fix | Delete
if opts.report:
[694] Fix | Delete
if not opts.file:
[695] Fix | Delete
parser.error('-r/--report requires -f/--file')
[696] Fix | Delete
results = CoverageResults(infile=opts.file, outfile=opts.file)
[697] Fix | Delete
return results.write_results(opts.missing, opts.summary, opts.coverdir)
[698] Fix | Delete
[699] Fix | Delete
if not any([opts.trace, opts.count, opts.listfuncs, opts.trackcalls]):
[700] Fix | Delete
parser.error('must specify one of --trace, --count, --report, '
[701] Fix | Delete
'--listfuncs, or --trackcalls')
[702] Fix | Delete
[703] Fix | Delete
if opts.listfuncs and (opts.count or opts.trace):
[704] Fix | Delete
parser.error('cannot specify both --listfuncs and (--trace or --count)')
[705] Fix | Delete
[706] Fix | Delete
if opts.summary and not opts.count:
[707] Fix | Delete
parser.error('--summary can only be used with --count or --report')
[708] Fix | Delete
[709] Fix | Delete
if opts.progname is None:
[710] Fix | Delete
parser.error('progname is missing: required with the main options')
[711] Fix | Delete
[712] Fix | Delete
t = Trace(opts.count, opts.trace, countfuncs=opts.listfuncs,
[713] Fix | Delete
countcallers=opts.trackcalls, ignoremods=opts.ignore_module,
[714] Fix | Delete
ignoredirs=opts.ignore_dir, infile=opts.file,
[715] Fix | Delete
outfile=opts.file, timing=opts.timing)
[716] Fix | Delete
try:
[717] Fix | Delete
if opts.module:
[718] Fix | Delete
import runpy
[719] Fix | Delete
module_name = opts.progname
[720] Fix | Delete
mod_name, mod_spec, code = runpy._get_module_details(module_name)
[721] Fix | Delete
sys.argv = [code.co_filename, *opts.arguments]
[722] Fix | Delete
globs = {
[723] Fix | Delete
'__name__': '__main__',
[724] Fix | Delete
'__file__': code.co_filename,
[725] Fix | Delete
'__package__': mod_spec.parent,
[726] Fix | Delete
'__loader__': mod_spec.loader,
[727] Fix | Delete
'__spec__': mod_spec,
[728] Fix | Delete
'__cached__': None,
[729] Fix | Delete
}
[730] Fix | Delete
else:
[731] Fix | Delete
sys.argv = [opts.progname, *opts.arguments]
[732] Fix | Delete
sys.path[0] = os.path.dirname(opts.progname)
[733] Fix | Delete
[734] Fix | Delete
with io.open_code(opts.progname) as fp:
[735] Fix | Delete
code = compile(fp.read(), opts.progname, 'exec')
[736] Fix | Delete
# try to emulate __main__ namespace as much as possible
[737] Fix | Delete
globs = {
[738] Fix | Delete
'__file__': opts.progname,
[739] Fix | Delete
'__name__': '__main__',
[740] Fix | Delete
'__package__': None,
[741] Fix | Delete
'__cached__': None,
[742] Fix | Delete
}
[743] Fix | Delete
t.runctx(code, globs, globs)
[744] Fix | Delete
except OSError as err:
[745] Fix | Delete
sys.exit("Cannot run file %r because: %s" % (sys.argv[0], err))
[746] Fix | Delete
except SystemExit:
[747] Fix | Delete
pass
[748] Fix | Delete
[749] Fix | Delete
results = t.results()
[750] Fix | Delete
[751] Fix | Delete
if not opts.no_report:
[752] Fix | Delete
results.write_results(opts.missing, opts.summary, opts.coverdir)
[753] Fix | Delete
[754] Fix | Delete
if __name__=='__main__':
[755] Fix | Delete
main()
[756] Fix | Delete
[757] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function