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