RE: Dynamically altering __init__

2011-11-13 Thread Kääriäinen Anssi
I wrote: """ I will post a link to a complete example once I have done the AST transformations etc. I hope this will be useful to readers of this list. I didn't find such an example, so maybe the next asker will find it... """ Finally got time to do this. The example can be found at: https://githu

RE: Dynamically altering __init__

2011-10-13 Thread Kääriäinen Anssi
Ian Kelly wrote: """ You can either pull the function code object out of the module code object constants: ... st_code = parser.compilest(st) func_code = st_code.co_consts[0] f = types.FunctionType(func_code, {}, '__init__') ... But you should take care to ensure that the function code object act

Re: Dynamically altering __init__

2011-10-13 Thread Ian Kelly
2011/10/13 Kääriäinen Anssi : > import parser > import types > st = parser.suite(src) > dyn_func = parser.compilest(st) > f = types.FunctionType(dyn_func, {}, '__init__') > im = types.MethodType(f, None, Foo) > Foo() > Foo.__init__ = im > Foo(1, 2, 3) > > The result is: TypeError: () takes no argum

Dynamically altering __init__

2011-10-13 Thread Kääriäinen Anssi
Hello all, I am trying to alter the init method of a class by source to AST -> alter AST -> AST back to code -> f = types.FunctionType(code, {}, '__init__') -> mt = types.MethodType(f, None, Foo) -> Foo.__init__ = mt I have two problems, first I haven't yet figured out how to make the AST back to