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