def changeAttrExpression(thread_id, frame_id, attr, expression): """Changes some attribute in a given frame. """ frame = findFrame(thread_id, frame_id) if frame is None: return try: expression = expression.replace("@LINE@", "\n") # if isinstance(frame, DjangoTemplateFrame): # TODO: implemente for plugins # result = eval(expression, frame.f_globals, frame.f_locals) # frame.changeVariable(attr, result) # return result if attr[:7] == "Globals": attr = attr[8:] if attr in frame.f_globals: frame.f_globals[attr] = eval(expression, frame.f_globals, frame.f_locals) return frame.f_globals[attr] else: if pydevd_save_locals.is_save_locals_available(): frame.f_locals[attr] = eval(expression, frame.f_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) return frame.f_locals[attr] # default way (only works for changing it in the topmost frame) result = eval(expression, frame.f_globals, frame.f_locals) Exec("%s=%s" % (attr, expression), frame.f_globals, frame.f_locals) return result except Exception: traceback.print_exc()
def evaluateExpression(thread_id, frame_id, expression, doExec): '''returns the result of the evaluated expression @param doExec: determines if we should do an exec or an eval ''' frame = findFrame(thread_id, frame_id) if frame is None: return expression = str(expression.replace('@LINE@', '\n')) #Not using frame.f_globals because of https://sourceforge.net/tracker2/?func=detail&aid=2541355&group_id=85796&atid=577329 #(Names not resolved in generator expression in method) #See message: http://mail.python.org/pipermail/python-list/2009-January/526522.html updated_globals = {} updated_globals.update(frame.f_globals) updated_globals.update( frame.f_locals ) #locals later because it has precedence over the actual globals try: if doExec: try: #try to make it an eval (if it is an eval we can print it, otherwise we'll exec it and #it will have whatever the user actually did) compiled = compile(expression, '<string>', 'eval') except: Exec(expression, updated_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) else: result = eval(compiled, updated_globals, frame.f_locals) if result is not None: #Only print if it's not None (as python does) sys.stdout.write('%s\n' % (result, )) return else: result = None try: result = eval(expression, updated_globals, frame.f_locals) except Exception: s = StringIO() traceback.print_exc(file=s) result = s.getvalue() try: try: etype, value, tb = sys.exc_info() result = value finally: etype = value = tb = None except: pass result = ExceptionOnEvaluate(result) return result finally: #Should not be kept alive if an exception happens and this frame is kept in the stack. del updated_globals del frame
def changeAttrExpression(thread_id, frame_id, attr, expression): '''Changes some attribute in a given frame. ''' frame = findFrame(thread_id, frame_id) if frame is None: return try: expression = expression.replace('@LINE@', '\n') if isinstance(frame, DjangoTemplateFrame): result = eval(expression, frame.f_globals, frame.f_locals) frame.changeVariable(attr, result) return if attr[:7] == "Globals": attr = attr[8:] if attr in frame.f_globals: frame.f_globals[attr] = eval(expression, frame.f_globals, frame.f_locals) return frame.f_globals[attr] else: if pydevd_save_locals.is_save_locals_available(): frame.f_locals[attr] = eval(expression, frame.f_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) return #default way (only works for changing it in the topmost frame) result = eval(expression, frame.f_globals, frame.f_locals) Exec('%s=%s' % (attr, expression), frame.f_globals, frame.f_locals) return result except Exception: traceback.print_exc()
def changeAttrExpression(thread_id, frame_id, attr, expression, dbg): '''Changes some attribute in a given frame. ''' frame = findFrame(thread_id, frame_id) if frame is None: return try: expression = expression.replace('@LINE@', '\n') if dbg.plugin: result = dbg.plugin.change_variable(frame, attr, expression) if result: return result if attr[:7] == "Globals": attr = attr[8:] if attr in frame.f_globals: frame.f_globals[attr] = eval(expression, frame.f_globals, frame.f_locals) return frame.f_globals[attr] else: if pydevd_save_locals.is_save_locals_available(): frame.f_locals[attr] = eval(expression, frame.f_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) return frame.f_locals[attr] #default way (only works for changing it in the topmost frame) result = eval(expression, frame.f_globals, frame.f_locals) Exec('%s=%s' % (attr, expression), frame.f_globals, frame.f_locals) return result except Exception: traceback.print_exc()
def changeAttrExpression(thread_id, frame_id, attr, expression): '''Changes some attribute in a given frame. ''' frame = findFrame(thread_id, frame_id) if frame is None: return try: expression = expression.replace('@LINE@', '\n') # if isinstance(frame, DjangoTemplateFrame): # TODO: implemente for plugins # result = eval(expression, frame.f_globals, frame.f_locals) # frame.changeVariable(attr, result) # return if attr[:7] == "Globals": attr = attr[8:] if attr in frame.f_globals: frame.f_globals[attr] = eval(expression, frame.f_globals, frame.f_locals) return frame.f_globals[attr] else: if pydevd_save_locals.is_save_locals_available(): frame.f_locals[attr] = eval(expression, frame.f_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) return frame.f_locals[attr] #default way (only works for changing it in the topmost frame) result = eval(expression, frame.f_globals, frame.f_locals) Exec('%s=%s' % (attr, expression), frame.f_globals, frame.f_locals) return result except Exception: traceback.print_exc()
def changeAttrExpression(thread_id, frame_id, attr, expression): '''Changes some attribute in a given frame. @note: it will not (currently) work if we're not in the topmost frame (that's a python deficiency -- and it appears that there is no way of making it currently work -- will probably need some change to the python internals) ''' frame = findFrame(thread_id, frame_id) if frame is None: return try: expression = expression.replace('@LINE@', '\n') if attr[:7] == "Globals": attr = attr[8:] if attr in frame.f_globals: frame.f_globals[attr] = eval(expression, frame.f_globals, frame.f_locals) else: if pydevd_save_locals.is_save_locals_available(): frame.f_locals[attr] = eval(expression, frame.f_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) return #default way (only works for changing it in the topmost frame) Exec('%s=%s' % (attr, expression), frame.f_globals, frame.f_locals) except Exception: traceback.print_exc()
def evaluateExpression(thread_id, frame_id, expression, doExec): '''returns the result of the evaluated expression @param doExec: determines if we should do an exec or an eval ''' frame = findFrame(thread_id, frame_id) if frame is None: return expression = str(expression.replace('@LINE@', '\n')) #Not using frame.f_globals because of https://sourceforge.net/tracker2/?func=detail&aid=2541355&group_id=85796&atid=577329 #(Names not resolved in generator expression in method) #See message: http://mail.python.org/pipermail/python-list/2009-January/526522.html updated_globals = {} updated_globals.update(frame.f_globals) updated_globals.update(frame.f_locals) #locals later because it has precedence over the actual globals try: if doExec: try: #try to make it an eval (if it is an eval we can print it, otherwise we'll exec it and #it will have whatever the user actually did) compiled = compile(expression, '<string>', 'eval') except: Exec(expression, updated_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) else: result = eval(compiled, updated_globals, frame.f_locals) if result is not None: #Only print if it's not None (as python does) sys.stdout.write('%s\n' % (result,)) return else: result = None try: result = eval(expression, updated_globals, frame.f_locals) except Exception: s = StringIO() traceback.print_exc(file=s) result = s.getvalue() try: try: etype, value, tb = sys.exc_info() result = value finally: etype = value = tb = None except: pass result = ExceptionOnEvaluate(result) return result finally: #Should not be kept alive if an exception happens and this frame is kept in the stack. del updated_globals del frame
def use_save_locals(name, value): """ Attempt to set the local of the given name to value, using locals_to_fast. """ frame = inspect.currentframe().f_back locals_dict = frame.f_locals locals_dict[name] = value save_locals(frame)
def check_co_vars(a): frame = sys._getframe() def function2(): print(a) assert 'a' in frame.f_code.co_cellvars frame = sys._getframe() frame.f_locals['a'] = 50 save_locals(frame) self.assertEquals(50, a)
def runcode(self, code): """Execute a code object. When an exception occurs, self.showtraceback() is called to display a traceback. All exceptions are caught except SystemExit, which is reraised. A note about KeyboardInterrupt: this exception may occur elsewhere in this code, and may not always be caught. The caller should be prepared to deal with it. """ try: Exec(code, self.frame.f_globals, self.frame.f_locals) pydevd_save_locals.save_locals(self.frame) except SystemExit: raise except: self.showtraceback()
def evaluateExpression(thread_id, frame_id, expression, doExec): """returns the result of the evaluated expression @param doExec: determines if we should do an exec or an eval """ frame = findFrame(thread_id, frame_id) if frame is None: return expression = str(expression.replace("@LINE@", "\n")) # Not using frame.f_globals because of https://sourceforge.net/tracker2/?func=detail&aid=2541355&group_id=85796&atid=577329 # (Names not resolved in generator expression in method) # See message: http://mail.python.org/pipermail/python-list/2009-January/526522.html updated_globals = {} updated_globals.update(frame.f_globals) updated_globals.update(frame.f_locals) # locals later because it has precedence over the actual globals try: if doExec: try: # try to make it an eval (if it is an eval we can print it, otherwise we'll exec it and # it will have whatever the user actually did) compiled = compile(expression, "<string>", "eval") except: Exec(expression, updated_globals, frame.f_locals) pydevd_save_locals.save_locals(frame) else: result = eval(compiled, updated_globals, frame.f_locals) if result is not None: # Only print if it's not None (as python does) sys.stdout.write("%s\n" % (result,)) return else: result = None try: result = eval(expression, updated_globals, frame.f_locals) except Exception: s = StringIO() traceback.print_exc(file=s) result = s.getvalue() try: try: etype, value, tb = sys.exc_info() result = value finally: etype = value = tb = None except: pass result = ExceptionOnEvaluate(result) # Ok, we have the initial error message, but let's see if we're dealing with a name mangling error... try: if "__" in expression: # Try to handle '__' name mangling... split = expression.split(".") curr = frame.f_locals.get(split[0]) for entry in split[1:]: if entry.startswith("__") and not hasattr(curr, entry): entry = "_%s%s" % (curr.__class__.__name__, entry) curr = getattr(curr, entry) result = curr except: pass return result finally: # Should not be kept alive if an exception happens and this frame is kept in the stack. del updated_globals del frame
def test_frame_simple_change(self): frame = sys._getframe() a = 20 frame.f_locals['a'] = 50 save_locals(frame) self.assertEquals(50, a)
def change(f): self.assert_(f is not sys._getframe()) f.f_locals['a']= 50 save_locals(f)
def func(): frame = sys._getframe() frame.f_locals['outer_var'] = 50 save_locals(frame) self.assertEquals(50, outer_var)