Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: cProfile.py
#! /usr/bin/python3.8
[0] Fix | Delete
[1] Fix | Delete
"""Python interface for the 'lsprof' profiler.
[2] Fix | Delete
Compatible with the 'profile' module.
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["run", "runctx", "Profile"]
[6] Fix | Delete
[7] Fix | Delete
import _lsprof
[8] Fix | Delete
import io
[9] Fix | Delete
import profile as _pyprofile
[10] Fix | Delete
[11] Fix | Delete
# ____________________________________________________________
[12] Fix | Delete
# Simple interface
[13] Fix | Delete
[14] Fix | Delete
def run(statement, filename=None, sort=-1):
[15] Fix | Delete
return _pyprofile._Utils(Profile).run(statement, filename, sort)
[16] Fix | Delete
[17] Fix | Delete
def runctx(statement, globals, locals, filename=None, sort=-1):
[18] Fix | Delete
return _pyprofile._Utils(Profile).runctx(statement, globals, locals,
[19] Fix | Delete
filename, sort)
[20] Fix | Delete
[21] Fix | Delete
run.__doc__ = _pyprofile.run.__doc__
[22] Fix | Delete
runctx.__doc__ = _pyprofile.runctx.__doc__
[23] Fix | Delete
[24] Fix | Delete
# ____________________________________________________________
[25] Fix | Delete
[26] Fix | Delete
class Profile(_lsprof.Profiler):
[27] Fix | Delete
"""Profile(timer=None, timeunit=None, subcalls=True, builtins=True)
[28] Fix | Delete
[29] Fix | Delete
Builds a profiler object using the specified timer function.
[30] Fix | Delete
The default timer is a fast built-in one based on real time.
[31] Fix | Delete
For custom timer functions returning integers, timeunit can
[32] Fix | Delete
be a float specifying a scale (i.e. how long each integer unit
[33] Fix | Delete
is, in seconds).
[34] Fix | Delete
"""
[35] Fix | Delete
[36] Fix | Delete
# Most of the functionality is in the base class.
[37] Fix | Delete
# This subclass only adds convenient and backward-compatible methods.
[38] Fix | Delete
[39] Fix | Delete
def print_stats(self, sort=-1):
[40] Fix | Delete
import pstats
[41] Fix | Delete
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
[42] Fix | Delete
[43] Fix | Delete
def dump_stats(self, file):
[44] Fix | Delete
import marshal
[45] Fix | Delete
with open(file, 'wb') as f:
[46] Fix | Delete
self.create_stats()
[47] Fix | Delete
marshal.dump(self.stats, f)
[48] Fix | Delete
[49] Fix | Delete
def create_stats(self):
[50] Fix | Delete
self.disable()
[51] Fix | Delete
self.snapshot_stats()
[52] Fix | Delete
[53] Fix | Delete
def snapshot_stats(self):
[54] Fix | Delete
entries = self.getstats()
[55] Fix | Delete
self.stats = {}
[56] Fix | Delete
callersdicts = {}
[57] Fix | Delete
# call information
[58] Fix | Delete
for entry in entries:
[59] Fix | Delete
func = label(entry.code)
[60] Fix | Delete
nc = entry.callcount # ncalls column of pstats (before '/')
[61] Fix | Delete
cc = nc - entry.reccallcount # ncalls column of pstats (after '/')
[62] Fix | Delete
tt = entry.inlinetime # tottime column of pstats
[63] Fix | Delete
ct = entry.totaltime # cumtime column of pstats
[64] Fix | Delete
callers = {}
[65] Fix | Delete
callersdicts[id(entry.code)] = callers
[66] Fix | Delete
self.stats[func] = cc, nc, tt, ct, callers
[67] Fix | Delete
# subcall information
[68] Fix | Delete
for entry in entries:
[69] Fix | Delete
if entry.calls:
[70] Fix | Delete
func = label(entry.code)
[71] Fix | Delete
for subentry in entry.calls:
[72] Fix | Delete
try:
[73] Fix | Delete
callers = callersdicts[id(subentry.code)]
[74] Fix | Delete
except KeyError:
[75] Fix | Delete
continue
[76] Fix | Delete
nc = subentry.callcount
[77] Fix | Delete
cc = nc - subentry.reccallcount
[78] Fix | Delete
tt = subentry.inlinetime
[79] Fix | Delete
ct = subentry.totaltime
[80] Fix | Delete
if func in callers:
[81] Fix | Delete
prev = callers[func]
[82] Fix | Delete
nc += prev[0]
[83] Fix | Delete
cc += prev[1]
[84] Fix | Delete
tt += prev[2]
[85] Fix | Delete
ct += prev[3]
[86] Fix | Delete
callers[func] = nc, cc, tt, ct
[87] Fix | Delete
[88] Fix | Delete
# The following two methods can be called by clients to use
[89] Fix | Delete
# a profiler to profile a statement, given as a string.
[90] Fix | Delete
[91] Fix | Delete
def run(self, cmd):
[92] Fix | Delete
import __main__
[93] Fix | Delete
dict = __main__.__dict__
[94] Fix | Delete
return self.runctx(cmd, dict, dict)
[95] Fix | Delete
[96] Fix | Delete
def runctx(self, cmd, globals, locals):
[97] Fix | Delete
self.enable()
[98] Fix | Delete
try:
[99] Fix | Delete
exec(cmd, globals, locals)
[100] Fix | Delete
finally:
[101] Fix | Delete
self.disable()
[102] Fix | Delete
return self
[103] Fix | Delete
[104] Fix | Delete
# This method is more useful to profile a single function call.
[105] Fix | Delete
def runcall(*args, **kw):
[106] Fix | Delete
if len(args) >= 2:
[107] Fix | Delete
self, func, *args = args
[108] Fix | Delete
elif not args:
[109] Fix | Delete
raise TypeError("descriptor 'runcall' of 'Profile' object "
[110] Fix | Delete
"needs an argument")
[111] Fix | Delete
elif 'func' in kw:
[112] Fix | Delete
func = kw.pop('func')
[113] Fix | Delete
self, *args = args
[114] Fix | Delete
import warnings
[115] Fix | Delete
warnings.warn("Passing 'func' as keyword argument is deprecated",
[116] Fix | Delete
DeprecationWarning, stacklevel=2)
[117] Fix | Delete
else:
[118] Fix | Delete
raise TypeError('runcall expected at least 1 positional argument, '
[119] Fix | Delete
'got %d' % (len(args)-1))
[120] Fix | Delete
[121] Fix | Delete
self.enable()
[122] Fix | Delete
try:
[123] Fix | Delete
return func(*args, **kw)
[124] Fix | Delete
finally:
[125] Fix | Delete
self.disable()
[126] Fix | Delete
runcall.__text_signature__ = '($self, func, /, *args, **kw)'
[127] Fix | Delete
[128] Fix | Delete
def __enter__(self):
[129] Fix | Delete
self.enable()
[130] Fix | Delete
return self
[131] Fix | Delete
[132] Fix | Delete
def __exit__(self, *exc_info):
[133] Fix | Delete
self.disable()
[134] Fix | Delete
[135] Fix | Delete
# ____________________________________________________________
[136] Fix | Delete
[137] Fix | Delete
def label(code):
[138] Fix | Delete
if isinstance(code, str):
[139] Fix | Delete
return ('~', 0, code) # built-in functions ('~' sorts at the end)
[140] Fix | Delete
else:
[141] Fix | Delete
return (code.co_filename, code.co_firstlineno, code.co_name)
[142] Fix | Delete
[143] Fix | Delete
# ____________________________________________________________
[144] Fix | Delete
[145] Fix | Delete
def main():
[146] Fix | Delete
import os
[147] Fix | Delete
import sys
[148] Fix | Delete
import runpy
[149] Fix | Delete
import pstats
[150] Fix | Delete
from optparse import OptionParser
[151] Fix | Delete
usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
[152] Fix | Delete
parser = OptionParser(usage=usage)
[153] Fix | Delete
parser.allow_interspersed_args = False
[154] Fix | Delete
parser.add_option('-o', '--outfile', dest="outfile",
[155] Fix | Delete
help="Save stats to <outfile>", default=None)
[156] Fix | Delete
parser.add_option('-s', '--sort', dest="sort",
[157] Fix | Delete
help="Sort order when printing to stdout, based on pstats.Stats class",
[158] Fix | Delete
default=-1,
[159] Fix | Delete
choices=sorted(pstats.Stats.sort_arg_dict_default))
[160] Fix | Delete
parser.add_option('-m', dest="module", action="store_true",
[161] Fix | Delete
help="Profile a library module", default=False)
[162] Fix | Delete
[163] Fix | Delete
if not sys.argv[1:]:
[164] Fix | Delete
parser.print_usage()
[165] Fix | Delete
sys.exit(2)
[166] Fix | Delete
[167] Fix | Delete
(options, args) = parser.parse_args()
[168] Fix | Delete
sys.argv[:] = args
[169] Fix | Delete
[170] Fix | Delete
# The script that we're profiling may chdir, so capture the absolute path
[171] Fix | Delete
# to the output file at startup.
[172] Fix | Delete
if options.outfile is not None:
[173] Fix | Delete
options.outfile = os.path.abspath(options.outfile)
[174] Fix | Delete
[175] Fix | Delete
if len(args) > 0:
[176] Fix | Delete
if options.module:
[177] Fix | Delete
code = "run_module(modname, run_name='__main__')"
[178] Fix | Delete
globs = {
[179] Fix | Delete
'run_module': runpy.run_module,
[180] Fix | Delete
'modname': args[0]
[181] Fix | Delete
}
[182] Fix | Delete
else:
[183] Fix | Delete
progname = args[0]
[184] Fix | Delete
sys.path.insert(0, os.path.dirname(progname))
[185] Fix | Delete
with io.open_code(progname) as fp:
[186] Fix | Delete
code = compile(fp.read(), progname, 'exec')
[187] Fix | Delete
globs = {
[188] Fix | Delete
'__file__': progname,
[189] Fix | Delete
'__name__': '__main__',
[190] Fix | Delete
'__package__': None,
[191] Fix | Delete
'__cached__': None,
[192] Fix | Delete
}
[193] Fix | Delete
try:
[194] Fix | Delete
runctx(code, globs, None, options.outfile, options.sort)
[195] Fix | Delete
except BrokenPipeError as exc:
[196] Fix | Delete
# Prevent "Exception ignored" during interpreter shutdown.
[197] Fix | Delete
sys.stdout = None
[198] Fix | Delete
sys.exit(exc.errno)
[199] Fix | Delete
else:
[200] Fix | Delete
parser.print_usage()
[201] Fix | Delete
return parser
[202] Fix | Delete
[203] Fix | Delete
# When invoked as main program, invoke the profiler on a script
[204] Fix | Delete
if __name__ == '__main__':
[205] Fix | Delete
main()
[206] Fix | Delete
[207] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function