def run(cls, cmdlineargs, *args, **kwargs): # print cls cls._options = filter( lambda x: isinstance(x, CmdLineOption), map(lambda x: eval("cls." + x, {"cls": cls}), dir(cls)) ) do_parse_options(cls.ProgramMetadata, cls._options) setprocname(sys.argv[0].split("/")[-1]) if int(cls.debugger_option.value): if int(cls.debugger_option.value) == 1: import pdb try: pdb.run("cls.prepare_process()", globals(), {"cls": cls}) pdb.run("cls.process()", globals(), {"cls": cls}) pdb.run("cls.finish()", globals(), {"cls": cls}) except: pdb.post_mortem(sys.exc_info()[2]) elif len(cls.profiler_option): import cProfile cProfile.run("cls.prepare_process()", cls.profiler_option + "-prepare") cProfile.run("cls.process()", cls.profiler_option + "-process") cProfile.run("cls.finish()", cls.profiler_option + "-finish") else: cls.prepare_process() sys.call_tracing(lambda: cls.process(), []) cls.finish() else: cls.prepare_process() cls.process() cls.finish()
def enter_recursive_debugger(self, code, filename, continue_if_has_breakpoints): """ Enter debugger recursively. """ sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals # Create child debugger debugger = SpyderPdb(completekey=self.completekey, stdin=self.stdin, stdout=self.stdout) debugger.use_rawinput = self.use_rawinput debugger.prompt = "(%s) " % self.prompt.strip() filename = debugger.canonic(filename) debugger._wait_for_mainpyfile = True debugger.mainpyfile = filename debugger.continue_if_has_breakpoints = continue_if_has_breakpoints debugger._user_requested_quit = False # Enter recursive debugger sys.call_tracing(debugger.run, (code, globals, locals)) # Reset parent debugger sys.settrace(self.trace_dispatch) self.lastcmd = debugger.lastcmd get_ipython().pdb_session = self
def enter_recursive_debugger(self, code, filename, continue_if_has_breakpoints): """ Enter debugger recursively. """ sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals # Create child debugger debugger = SpyderPdb( completekey=self.completekey, stdin=self.stdin, stdout=self.stdout) debugger.use_rawinput = self.use_rawinput debugger.prompt = "(%s) " % self.prompt.strip() debugger.set_remote_filename(filename) debugger.continue_if_has_breakpoints = continue_if_has_breakpoints # Enter recursive debugger sys.call_tracing(debugger.run, (code, globals, locals)) # Reset parent debugger sys.settrace(self.trace_dispatch) self.lastcmd = debugger.lastcmd get_ipython().pdb_session = self # Reset _previous_step so that publish_pdb_state() called from within # postcmd() notifies Spyder about a changed debugger position. The reset # is required because the recursive debugger might change the position, # but the parent debugger (self) is not aware of this. self._previous_step = None
def recursive(self, g, l): # Inspect curent debugger vars through pdb sys.settrace(None) from pdb import Pdb p = Pdb() sys.call_tracing(p.run, ('1/0', g, l)) sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe.f_locals p = Debugger(io=self._ui.IO) p.reset() sys.call_tracing(p.run, (arg, globals, locals)) sys.settrace(p.trace_dispatch)
def debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe.f_locals p = Debugger() p.reset() sys.call_tracing(p.run, (arg, globals, locals)) sys.settrace(self.trace_dispatch)
def _recursive(self, g, l): """Inspect wdb with pdb""" # Inspect curent debugger vars through pdb sys.settrace(None) from pdb import Pdb p = Pdb() sys.call_tracing(p.run, ('1/0', g, l)) sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def _recursive(self, g, l): """Inspect wdb with pdb""" # Inspect curent debugger vars through pdb sys.settrace(None) from pdb import Pdb p = Pdb() sys.call_tracing(p.run, ("1/0", g, l)) sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = Exterminator(self.completekey, self.stdin, self.stdout) p.prompt = "({}) ".format(self.prompt.strip()) output("ENTERING RECURSIVE DEBUGGER") sys.call_tracing(p.run, (arg, globals, locals)) output("LEAVING RECURSIVE DEBUGGER") sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = Pdb(self.completekey, self.stdin, self.stdout) p.prompt = "(%s) " % self.prompt.strip() print >> self.stdout, "ENTERING RECURSIVE DEBUGGER" sys.call_tracing(p.run, (arg, globals, locals)) print >> self.stdout, "LEAVING RECURSIVE DEBUGGER" sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe.f_locals p = Epdb() p.prompt = "(%s) " % self.prompt.strip() print("ENTERING RECURSIVE DEBUGGER") sys.call_tracing(p.run, (arg, globals, locals)) print("LEAVING RECURSIVE DEBUGGER") sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe.f_locals p = Pdb() p.prompt = "(%s) " % self.prompt.strip() self.stdout.write("ENTERING RECURSIVE DEBUGGER\n") sys.call_tracing(p.run, (arg, globals, locals)) self.stdout.write("LEAVING RECURSIVE DEBUGGER\n") sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = Pdb(self.completekey, self.stdin, self.stdout) p.prompt = "(%s) " % self.prompt.strip() shout >>self.stdout, "ENTERING RECURSIVE DEBUGGER" sys.call_tracing(p.run, (arg, globals, locals)) shout >>self.stdout, "LEAVING RECURSIVE DEBUGGER" sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def test_call_tracing(self): def f(i): return i * 2 def g(): pass # outside of a traceback self.assertEqual(10, sys.call_tracing(f, (5, ))) # inside of a traceback log = [] def thandler(frm, evt, pl): if evt == 'call': log.append(frm.f_code.co_name) if log[-1] == 'g': sys.call_tracing(f, (5, )) return thandler sys.settrace(thandler) g() sys.settrace(None) self.assertEqual(log, ['g', 'f'])
def do_debug(self, arg): """debug code Enter a recursive debugger that steps through the code argument (which is an arbitrary expression or statement to be executed in the current environment). """ sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = self.__class__(completekey=self.completekey, stdin=self.stdin, stdout=self.stdout) p.use_rawinput = self.use_rawinput p.prompt = "(%s) " % self.prompt.strip() self.message("ENTERING RECURSIVE DEBUGGER") sys.call_tracing(p.run, (arg, globals, locals)) self.message("LEAVING RECURSIVE DEBUGGER") sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def enter_debugger(filename, continue_if_has_breakpoints, code_format): """Enter debugger. Code format should be a format that accept filename.""" kernel = get_ipython().kernel recursive = kernel.is_debugging() if recursive: parent_debugger = kernel._pdb_obj sys.settrace(None) globals = parent_debugger.curframe.f_globals locals = parent_debugger.curframe_locals # Create child debugger debugger = SpyderPdb( completekey=parent_debugger.completekey, stdin=parent_debugger.stdin, stdout=parent_debugger.stdout) debugger.use_rawinput = parent_debugger.use_rawinput debugger.prompt = "(%s) " % parent_debugger.prompt.strip() else: debugger = SpyderPdb() filename = debugger.canonic(filename) debugger._wait_for_mainpyfile = True debugger.mainpyfile = filename debugger.continue_if_has_breakpoints = continue_if_has_breakpoints debugger._user_requested_quit = False if os.name == 'nt': filename = filename.replace('\\', '/') code = code_format.format(repr(filename)) if recursive: # Enter recursive debugger sys.call_tracing(debugger.run, (code, globals, locals)) # Reset parent debugger sys.settrace(parent_debugger.trace_dispatch) parent_debugger.lastcmd = debugger.lastcmd kernel._register_pdb_session(parent_debugger) else: # The breakpoint might not be in the cell debugger.run(code)
def run(self, args): arg = ' '.join(args[1:]) curframe = self.proc.curframe if not curframe: self.msg("No frame selected.") return for attr in ('prompt_str', 'frame', 'event', 'event_arg', 'curindex'): cmd = 'old_%s = self.proc.%s' % (attr, attr) exec(cmd) pass old_lock = self.core.debugger_lock old_stop_level = self.core.stop_level old_different_line = self.core.stop_level self.proc.debug_nest += 1 self.core.debugger_lock = threading.Lock() self.core.stop_level = None self.core.different_line = None global_vars = curframe.f_globals local_vars = curframe.f_locals self.section("ENTERING NESTED DEBUGGER") self.core.step_ignore = 2 # call_tracing will stop in itself. try: ret = sys.call_tracing(eval, (arg, global_vars, local_vars)) self.msg("R=> %s" % self.proc._saferepr(ret)) except: pass self.section("LEAVING NESTED DEBUGGER") self.core.debugger_lock = old_lock self.core.stop_level = old_stop_level self.core.different_line = old_different_line self.proc.continue_running = False self.proc.debug_nest -= 1 for attr in ('prompt_str', 'frame', 'event', 'event_arg', 'curindex'): cmd = 'self.proc.%s = old_%s' % (attr, attr) exec(cmd) pass if hasattr(self.proc, 'print_location'): self.proc.print_location() return False
def run(self, args): arg = ' '.join(args[1:]) curframe = self.proc.curframe if not curframe: self.msg("No frame selected.") return for attr in ('prompt_str', 'frame', 'event', 'event_arg', 'curindex'): cmd = 'old_%s = self.proc.%s' % (attr, attr) exec(cmd) pass old_lock = self.core.debugger_lock old_stop_level = self.core.stop_level old_different_line = self.core.stop_level self.proc.debug_nest += 1 self.core.debugger_lock = threading.Lock() self.core.stop_level = None self.core.different_line = None global_vars = curframe.f_globals local_vars = curframe.f_locals self.section("ENTERING NESTED DEBUGGER") self.core.step_ignore = 2 # call_tracing will stop in itself. try: ret = sys.call_tracing(eval, (arg, global_vars, local_vars)) self.msg("R=> %s" % self.proc._saferepr(ret)) except: pass self.section("LEAVING NESTED DEBUGGER") self.core.debugger_lock = old_lock self.core.stop_level = old_stop_level self.core.different_line = old_different_line self.proc.continue_running = False self.proc.debug_nest -= 1 for attr in ('prompt_str', 'frame', 'event', 'event_arg', 'curindex'): cmd = 'self.proc.%s = old_%s' % (attr, attr) exec(cmd) pass self.proc.location() return False
# =============================================== # MODULE STUDY: sys import sys sys.argv # The list of command line arguments passed to a Python script. argv[0] is the script name # ............................................ sys.byteorder # An indicator of the native byte order. This will have the value 'big' on big-endian (most-significant byte first) platforms, and 'little' on little-endian (least-significant byte first) platforms. # ............................................ sys.builtin_module_names # A tuple of strings giving the names of all modules that are compiled into this Python interpreter. # ............................................ sys.call_tracing(func, args) # Call func(*args), while tracing is enabled. The tracing state is saved, and restored afterwards. # ............................................ sys.copyright # A string containing the copyright pertaining to the Python interpreter. # ............................................ sys._clear_type_cache() # Clear the internal type cache. The type cache is used to speed up attribute and method lookups. # ............................................ sys._current_frames() # ............................................ sys.dllhandle # Integer specifying the handle of the Python DLL. Availability: Windows. # ............................................ sys.displayhook(value) # If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._. # ............................................ sys.dont_write_bytecode # If this is true, Python won’t try to write .pyc or .pyo files on the import of source modules. # This value is initially set to True or False depending on the -B command line option and the PYTHONDONTWRITEBYTECODE environment variable, # but you can set it yourself to control bytecode file generation. # ............................................ sys.excepthook(type, value, traceback) # This function prints out a given traceback and exception to sys.stderr. # ............................................ sys.__displayhook__ sys.__excepthook__ # These objects contain the original values of displayhook and excepthook at the start of the program.
def test_call_tracing(self): self.assertEqual(sys.call_tracing(str, (2,)), "2") self.assertRaises(TypeError, sys.call_tracing, str, 2)
def sys_func(): lists = sys.argv # 传递给Python脚本的命令行参数列表 => python p.py -> ['p.py'] / python p.py a 1 -> ['p.py', 'a', '1'] / 程序内执行 -> [''] strs = sys.getdefaultencoding() # 默认字符集名称 strs = sys.getfilesystemencoding() # 系统文件名字符集名称 num = sys.getrefcount(object) # 返回object的引用计数(比实际多1个) dicts = sys.modules # 已加载的模块, 可修改, 但不能通过修改返回的字典进行修改 lists = sys.path # 模块搜索路径 sys.path.append("./test") # 动态添加模块搜索路径 strs = sys.platform # 平台标识符(系统身份进行详细的检查,推荐使用) Linux:'linux' / Windows:'win32' / Cygwin:'cygwin' / Mac OS X:'darwin' strs = sys.version # python解释器版本 lists = sys.thread_info # 线程信息 num = sys.api_version # 解释器C API版本 types, value, back = sys.exc_info() # 捕获异常 详见 异常 文章的 excep() 代码块第二小部分(http://blog.csdn.net/rozol/article/details/69313164) sys.excepthook(types, value, back) # 打印异常 types = sys.last_type value = sys.last_value back = sys.last_traceback # sys.exit([arg]) // 引发SystemExit异常退出Python(可以try), 范围[0,127], None==0, "string"==1 sys.exit(0) num = sys.getrecursionlimit() # 最大递归数(堆栈最大深度), 详见 函数 文章(http://blog.csdn.net/rozol/article/details/69242050) sys.setrecursionlimit(5000) # 修改最大递归数 fnum = sys.getswitchinterval() # 获取线程切换间隔 sys.setswitchinterval(0.005) # 设置线程切换间隔, 单位秒 num = sys.getcheckinterval() # 解释器的检查间隔 sys.setcheckinterval(100) # 设置解释器检查间隔, 执行(默认)100个虚拟指令执行一次检查, 值为<=0时,检查每个虚拟指令 # sys.stdin // 标准输入流 strs = sys.stdin.readline()[:-1] # sys.stdout // 标准出入输出 sys.stdout.write(">>") sys.stdout.flush() # sys.stderr // 标注错误流 sys.stderr.write(">>") # --- lists = sys.builtin_module_names # 所有模块 (注:非导入模块) path = sys.base_exec_prefix # Python安装路径 path = sys.base_prefix # 同base_exec_prefix path = sys.exec_prefix # 同base_exec_prefix path = sys.prefix # 同base_exec_prefix path = sys.executable # Python解释器的绝对路径 strs = ys.byteorder # 本机字节顺序指示器, big-endian(最高有效字节在第一位)值为'big', little-endian(最低有效字节在第一位)值为'little' strs = sys.copyright # python版权 num = sys.hexversion # 16进制版本号 lists = sys.implementation # 当前运行的解释器的信息 num = sys.getallocatedblocks() # 解释器当前分配的内存块的数量 boolean = sys.dont_write_bytecode # 是否不会尝试导入源模块是写入.pyc文件 (False会写入.pyc文件) # sys.getsizeof(object[, default]) // 返回对象的大小bit, 只计算自身内存消耗,不计算引用对象的内存消耗, 调用对象的__sizeof__(), default没有获取到默认返回值 num = sys.getsizeof(object) boolean = sys.is_finalizing() # 解释器是否正在被关机 num = sys.maxsize # 最大整数值(2 ** 31 -1), 与系统有关 num = sys.maxunicode # 最大Unicode值的整数 (1114111) strs = sys.ps1 # 解释器主提示符 strs = sys.ps2 # 解释器次提示符 sys.call_tracing(func, ("arg",)) # 调用函数 sys._clear_type_cache() # 清除内部类型缓存 sys._debugmallocstats() # 打印CPython内存分配器状态的低级信息 sys.setprofile(profilefunc) # 设置profile函数, 默认None sys.getprofile() # 获取profile函数 sys.settrace(tracefunc) # 设置跟踪函数, def tracefunc(frame、event 和arg): sys.gettrace() # 获取跟踪函数, 默认None sys.set_coroutine_wrapper(wrapper) # 设置包装 def wrapper(coro): sys.get_coroutine_wrapper() # 包装, 默认None
def thandler(frm, evt, pl): if evt == 'call': log.append(frm.f_code.co_name) if log[-1] == 'g': sys.call_tracing(f, (5, )) return thandler
os.system(command + 'args') # <yes> <report> PYTHON_INJECTION_COMMAND 6aae42 os.popen(command) # <yes> <report> PYTHON_INJECTION_COMMAND c6f252 subprocess.call("mycmd" + args, shell=True) # <yes> <report> PYTHON_INJECTION_COMMAND c6f252 subprocess.run("mycmd" + args, shell=True) # <yes> <report> PYTHON_INJECTION_COMMAND af00c9 pipe = Popen(command, shell=True, bufsize=bufsize, stdout=PIPE).stdout # <yes> <report> PYTHON_INJECTION_COMMAND 691790 eval(command) # <yes> <report> PYTHON_INJECTION_COMMAND 691790 exec(command) # <yes> <report> PYTHON_INJECTION_COMMAND 691790 pickle(args) # <yes> <report> PYTHON_INJECTION_COMMAND 34877f popen2.popen3(command) # <yes> <report> PYTHON_INJECTION_COMMAND 0187fa commands.getoutput(command) import sys # <yes> <report> PYTHON_INJECTION_COMMAND 3fbfda sys.call_tracing(command) # <yes> <report> PYTHON_INJECTION_COMMAND cd6f58 input(command)
def test_call_tracing(self): if test.test_support.due_to_ironpython_bug( "http://ironpython.codeplex.com/workitem/28171"): return self.assertEqual(sys.call_tracing(str, (2, )), "2") self.assertRaises(TypeError, sys.call_tracing, str, 2)
print(sys.argv) print(sys.base_exec_prefix) print(sys.base_prefix) print(sys.exec_prefix) # 虚拟环境 print(sys.prefix) # 虚拟环境 # 本地字节顺序的指示符。在大端序(最高有效位优先)操作系统上值为 'big' ,在小端序(最低有效位优先)操作系统上为 'little' 。 # 字节存放顺序:大尾,小尾 http://blog.sina.com.cn/s/blog_6277623c0102vntv.html print(sys.byteorder) # 包含了所有的被编译进 Python 解释器的模块 print(sys.builtin_module_names) sys.call_tracing(print, (1, )) def mrintArgs(*args): # 创建方法,收集参数。 print(list(args)) sys.call_tracing(mrintArgs, (1, 2, 3, 4)) print(sys.copyright) sys._clear_type_cache() print(sys._current_frames()) # sys.breakpointhook()
min_el = matrix[0][col] for row in range(rows): if min_el > matrix[row][col]: min_el = matrix[row][col] if info < min_el: info = min_el return info # Для функции column_move: # matrix <class 'list'>: 3732 # info <class 'int'>: 14 # columns <class 'int'>: 14 # rows <class 'int'>: 14 # col <class 'int'>: 14 # min_el <class 'int'>: 12 # row <class 'int'>: 14 # Суммарный объем памяти 3814 funcs = [(column_move, ())] sys.settrace(trace_func) for func, args in funcs: ram = 0 print(f'Для функции {func.__name__}:') sys.call_tracing(func, args) print(f'Суммарный объем памяти {ram}\n')
def test_call_tracing(self): if test.test_support.due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"): return self.assertEqual(sys.call_tracing(str, (2,)), "2") self.assertRaises(TypeError, sys.call_tracing, str, 2)