parse python functions as a string within decorator -


i trying write function debug decorator at:

def foo(baz):   bar = 1   bar = 2   return bar 

and wrap to:

def foo(baz):   bar = 1   print 'bar: {}'.format(bar)   bar = 2   print 'bar: {}'.format(bar)   return bar 

i need play function text, grab "\w+(?=\s*[=])", not know how access that. have decorator modified blog works, tried changing to:

class decorator_string_check(object):     def __init__(self, func):         self.func = func         wraps(func)(self)     def __call__(self, *args, **kwargs):         print dir(self.func)         print dir(self.func.__code__)         print self.func.__code__.__str__()         ret = self.func(*args, **kwargs)         return ret  @decorator_string_check def fake(x):     y = 6     y = x     return y  y = fake(9) 

and getting nothinng of value, namely:

['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] ['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] <code object fake @ 0x7f98b8b1d030, file "./logging.py", line 48> 

how work actual "func" text, run regexes on , find things need within decorator class object? thank you

first of all, suggest not that. it's hard working code , hard make correct version.

besides don't know want exactly. should decorator add print statement after every assignment , show updated value? or keep track of given subset of variables?

this said, obtain source code of can use inspect module in particular getsource function:

in [1]: def test():    ...:     = 1    ...:     b = 2    ...:       in [2]: import inspect  in [3]: inspect.getsource(test) out[3]: 'def test():\n    = 1\n    b = 2\n' in [4]: print(inspect.getsource(test)) def test():     = 1     b = 2 

you modify , inspect source code wish , compile() new source code.

note that:

  • you have careful when modifying source code, because it's easy create syntactically invalid code (think: multiline expressions etc.)
  • when compiling you'd compile in same scope original function. inspect module has functions allow obtain stackframe decorator called , can obtain environments there. read here how handle stack.
  • the source code may not available. code compiled in bytecode , original file may deleted. in case getsource raise oserror.

a more "sane" solution not @ source code, @ bytecode. can using dis module. may try see when values of variable change , insert bytecode print variable.

note dis module enhanced in python3.4+, previous versions of python hard.

you should read articles python bytecode hacks, gotos revisited before trying this. give idea on how @ bytecode , work it.

this safer (e.g. if source file doesn't exist on machine bytecode still accessible), yet still think have in mind not thing do, except exercise.


as jsbueno points out proper way of doing want (i.e. python debugger) use sys.settrace.

this function allows set tracing function called each "code" executed. function know when function called, new block entered etc. gives access frame code executed, , should able find values interested in.

you should check lnotab_notes.txt file understand how data provided argument function can mapped source code positions understand when assignment performed.

when have time (probably next week end) i'll try implement based on approach demonstrate it.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -