本文整理汇总了Python中sys.getprofile函数的典型用法代码示例。如果您正苦于以下问题:Python getprofile函数的具体用法?Python getprofile怎么用?Python getprofile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getprofile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_setprofile
def test_setprofile():
profiler = TracingProfiler()
assert sys.getprofile() is None
with profiler:
assert sys.getprofile() == profiler._profile
assert sys.getprofile() is None
sys.setprofile(lambda *x: x)
with pytest.raises(RuntimeError):
profiler.start()
sys.setprofile(None)
示例2: test_profile_enable_disable
def test_profile_enable_disable(self):
prof = self.profilerclass()
# Make sure we clean ourselves up if the test fails for some reason.
self.addCleanup(prof.disable)
prof.enable()
self.assertIs(sys.getprofile(), prof)
prof.disable()
self.assertIs(sys.getprofile(), None)
示例3: test_error_when_set_multiple
def test_error_when_set_multiple(self):
self.monitor._profile.replace(self.bar)
self.assertIs(sys.getprofile(), self.bar)
self.monitor._profile.replace(self.bar)
self.assertIs(sys.getprofile(), self.bar)
self.monitor._profile.recover()
self.monitor._profile.replace(self.bar)
self.assertIs(sys.getprofile(), self.bar)
with self.assertRaises(RuntimeError):
self.monitor._profile.replace(None)
self.assertIs(sys.getprofile(), self.bar)
self.monitor._profile.recover()
示例4: test_profile_as_context_manager
def test_profile_as_context_manager(self):
prof = self.profilerclass()
# Make sure we clean ourselves up if the test fails for some reason.
self.addCleanup(prof.disable)
with prof as __enter__return_value:
# profile.__enter__ should return itself.
self.assertIs(prof, __enter__return_value)
# profile should be set as the global profiler inside the
# with-block
self.assertIs(sys.getprofile(), prof)
# profile shouldn't be set once we leave the with-block.
self.assertIs(sys.getprofile(), None)
示例5: get_info_on_next_invocation
def get_info_on_next_invocation(self, func, callback, needed):
"""
func is a tuple (filename, funcname)
needed is a list/tuple of NeededInfo objects
"""
if isinstance(needed, NeededInfo):
needed = (needed,)
request = WatchRequest(needed, {}, callback)
if func in self.watching_funcs:
self.watching_funcs[func].requests.append(request)
else:
self.watching_funcs[func] = FuncWatchStatus(func, requests=[request])
fws = self.watching_funcs[func]
for ni in needed:
hsh = 0
for i in range(ni.stmt_idx+1):
hsh += hash(ni.stmt_sequence[i])
#cprint ("adding to hash %s: %i"%(str(ni.stmt_sequence[i]), hsh), 'green')
if hsh in fws.ni_hashmap:
fws.ni_hashmap[hsh].append((request,ni))
else:
fws.ni_hashmap[hsh] = [(request,ni)]
cprint("finishing up get_info_on_next_invocation", 'red')
# XXX Why do I need to use both settrace and setprofile? settrace doesn't
# XXX trigger on return events, and setprofile doesn't trigger on line events...
self.old_trace, self.old_profile = sys.gettrace(), sys.getprofile()
sys.settrace(self.trace_cb)
sys.setprofile(self.profile_cb)
示例6: __init__
def __init__(self, metascript):
# Indicates when activations should be collected
# (only after the first call to the script)
self.enabled = False
# User files
self.script = metascript.path
self.paths = metascript.paths
# Which function types ('main', 'package' or 'all')
# should be considered for the threshold
self.context = metascript.context
# How deep we want to go when capturing function activations?
self.depth_threshold = metascript.depth
# How deep we want to go beyond our context
self.non_user_depth_threshold = metascript.non_user_depth
# Object serializer function
self.serialize = metascript.serialize
self.metascript = metascript
self.trial_id = metascript.trial_id
self.event_map = defaultdict(lambda: self.trace_empty, {})
self.default_profile = sys.getprofile()
self.default_trace = sys.gettrace()
self.argument_captor = ArgumentCaptor(self)
示例7: start
def start(self):
if sys.getprofile() is not None:
raise RuntimeError('Another profiler already registered.')
self._running = True
sys.setprofile(self._profile)
threading.setprofile(self._profile)
self.timer.start()
self.stats.record_starting(time.clock())
示例8: task
def task(self, expected_trace_function=None, expected_profile_function=None):
# this task tests that the trace/profile function has the
# expected value
tasklet = stackless.current
self.assertIs(sys.gettrace(), expected_trace_function)
self.assertIs(tasklet.trace_function, expected_trace_function)
self.assertIs(sys.getprofile(), expected_profile_function)
self.assertIs(tasklet.profile_function, expected_profile_function)
示例9: setUp
def setUp(self):
if sys.gettrace() or sys.getprofile():
self.skipTest("Trace or profile function already active")
self.addCleanup(sys.settrace, None)
self.addCleanup(stackless.set_schedule_callback, None)
self.schedule_callback_exc = []
self.unexpected_trace_events = []
self.m = mutex()
示例10: replace
def replace(self, function):
""" Set a new function in sys.setprofile.
If the function has been already set and it is not the same as before
then RuntimeError is raised.
"""
if hasattr(self, 'previous'):
if function != sys.getprofile():
raise RuntimeError(
'Cannot replace profile function more than once')
return
else:
self.previous = sys.getprofile()
if has_threading:
threading.setprofile(function)
sys.setprofile(function)
示例11: trace_execution
def trace_execution(fn, args, save_to=None):
import inspect
if save_to:
os.environ["INTEL_SEA_SAVE_TO"] = save_to
itt = ITT("python")
if itt.lib:
file_id = itt.get_string_id("__FILE__")
line_id = itt.get_string_id("__LINE__")
module_id = itt.get_string_id("__MODULE__")
trace_execution.frames = {}
trace_execution.recurrent = False
high_part = 2 ** 32
def profiler(frame, event, arg): # https://pymotw.com/2/sys/tracing.html
if trace_execution.recurrent:
return
trace_execution.recurrent = True
task_id = id(frame.f_code)
if "call" in event:
if task_id in trace_execution.frames:
trace_execution.frames[task_id] += 1
else:
trace_execution.frames[task_id] = 1
task_id += trace_execution.frames[task_id] * high_part
name = frame.f_code.co_name + ((" (%s)" % arg.__name__) if arg else "")
if "self" in frame.f_locals:
cls = frame.f_locals["self"].__class__.__name__
name = cls + "." + name
# print event, name, task_id, arg
mdl = inspect.getmodule(frame)
itt.lib.itt_task_begin_overlapped(itt.domain, task_id, 0, itt.get_string_id(name), 0)
itt.lib.itt_metadata_add_str(itt.domain, task_id, file_id, frame.f_code.co_filename)
itt.lib.itt_metadata_add(itt.domain, task_id, line_id, frame.f_code.co_firstlineno)
if mdl:
itt.lib.itt_metadata_add_str(itt.domain, task_id, module_id, mdl.__name__)
elif "return" in event:
# print event, frame.f_code.co_name, task_id + trace_execution.frames[task_id] * high_part
if task_id in trace_execution.frames:
itt.lib.itt_task_end_overlapped(
itt.domain, 0, task_id + trace_execution.frames[task_id] * high_part
)
if trace_execution.frames[task_id] > 1:
trace_execution.frames[task_id] -= 1
else:
del trace_execution.frames[task_id]
trace_execution.recurrent = False
print trace_execution.frames
old_profiler = sys.getprofile()
sys.setprofile(profiler)
old_threading_profiler = threading.setprofile(profiler)
fn(*args)
sys.setprofile(old_profiler)
threading.setprofile(old_threading_profiler)
else:
fn(*args)
示例12: task2
def task2(self, expected_trace_function=None, expected_profile_function=None):
# task switches tasklets and then tests that the trace/profile function has the
# expected value
stackless.schedule()
tasklet = stackless.current
self.assertIs(sys.gettrace(), expected_trace_function)
self.assertIs(tasklet.trace_function, expected_trace_function)
self.assertIs(sys.getprofile(), expected_profile_function)
self.assertIs(tasklet.profile_function, expected_profile_function)
示例13: main
def main():
i = 1500
orig_profile = sys.getprofile()
sys.setprofile(profile)
try:
while i > 0:
i = sub(i, 1)
finally:
sys.setprofile(orig_profile)
示例14: testSetTraceOnCurrent
def testSetTraceOnCurrent(self):
tf = self.nullTraceFunc
tasklet = stackless.current
self.assertIsNone(sys.gettrace())
self.assertIsNone(sys.getprofile())
self.assertIsNone(tasklet.trace_function)
self.assertIsNone(tasklet.profile_function)
tasklet.trace_function = tf
self.assertIs(sys.gettrace(), tf)
self.assertIsNone(sys.getprofile())
self.assertIs(tasklet.trace_function, tf)
self.assertIsNone(tasklet.profile_function)
tasklet.trace_function = None
self.assertIsNone(sys.gettrace())
self.assertIsNone(sys.getprofile())
self.assertIsNone(tasklet.trace_function)
self.assertIsNone(tasklet.profile_function)
self.assertGreater(self.tracecount, 0)
示例15: uninstall
def uninstall(self):
"""
Deactivate this tracer.
If another profile hook was installed after this tracer was installed,
nothing will happen. If a different profile hook was installed prior to
calling ``install()``, it will be restored.
"""
if sys.getprofile() == self._trace:
sys.setprofile(self._wrapped_profiler)