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