Re: monkey patching __code__

2016-03-23 Thread Sven R. Kunze
On 23.03.2016 09:24, dieter wrote: But you have observed that you cannot do everything with a code substitution: a function call does not only depend on the code but also on other properties of the function object: e.g. the parameter processing. Yep, that's because Python is very flexible and p

Re: monkey patching __code__

2016-03-23 Thread dieter
"Sven R. Kunze" writes: > ... > As far as I can see, the code replacement approach solves the problem > once and for all. Thus is far more stable. > > Manually finding out every single module that might or might not have > imported "reverse" before we could monkeypatch it might result in a > maint

Re: monkey patching __code__

2016-03-22 Thread Sven R. Kunze
On 21.03.2016 21:42, Matt Wheeler wrote: On 20 March 2016 at 16:46, Sven R. Kunze wrote: On 19.03.2016 00:58, Matt Wheeler wrote: I know you have a working solution now with updating the code & defaults of the function, but what about just injecting your function into the modules that had alre

Re: monkey patching __code__

2016-03-21 Thread Matt Wheeler
On 20 March 2016 at 16:46, Sven R. Kunze wrote: > On 19.03.2016 00:58, Matt Wheeler wrote: >> >> I know you have a working solution now with updating the code & >> defaults of the function, but what about just injecting your function >> into the modules that had already imported it after the >> mo

Re: monkey patching __code__

2016-03-20 Thread Sven R. Kunze
On 19.03.2016 00:58, Matt Wheeler wrote: I know you have a working solution now with updating the code & defaults of the function, but what about just injecting your function into the modules that had already imported it after the monkeypatching? Seems perhaps cleaner, unless you'd end up having

Re: monkey patching __code__

2016-03-20 Thread Ian Kelly
On Fri, Mar 18, 2016 at 7:47 AM, Ian Kelly wrote: > Your patched version takes two extra arguments. Did you add the > defaults for those to the function's __defaults__ attribute? And as an afterthought, you'll likely need to replace the function's __globals__ with your own as well. -- https://ma

Re: monkey patching __code__

2016-03-19 Thread Ian Kelly
On Mar 18, 2016 8:33 AM, "Sven R. Kunze" wrote: > > On 18.03.2016 14:47, Ian Kelly wrote: >> >> Your patched version takes two extra arguments. Did you add the >> defaults for those to the function's __defaults__ attribute? > > > That's it! :-) Thanks a lot. > > Just to understand this better: why

Re: monkey patching __code__

2016-03-19 Thread Matt Wheeler
On 18 March 2016 at 11:49, Sven R. Kunze wrote: > Hi, > > we got an interesting problem. We need to monkeypatch Django's reverse > function: > > > First approach: > > urlresolvers.reverse = patched_reverse > > > Problem: some of Django's internal modules import urlresolvers.reverse > before we can

Re: monkey patching __code__

2016-03-19 Thread Ian Kelly
On Fri, Mar 18, 2016 at 9:01 AM, Sven R. Kunze wrote: > On 18.03.2016 15:48, Ian Kelly wrote: >> >> Well I didn't design it, so I'm not really sure. But it could be argued >> that the defaults are intrinsic to the function declaration, not the code >> object, as not all code objects even have argu

Re: monkey patching __code__

2016-03-19 Thread Sven R. Kunze
On 18.03.2016 14:47, Ian Kelly wrote: Your patched version takes two extra arguments. Did you add the defaults for those to the function's __defaults__ attribute? That's it! :-) Thanks a lot. Just to understand this better: why is that not part of the code object but part of the function?

Re: monkey patching __code__

2016-03-19 Thread Sven R. Kunze
On 18.03.2016 15:48, Ian Kelly wrote: Well I didn't design it, so I'm not really sure. But it could be argued that the defaults are intrinsic to the function declaration, not the code object, as not all code objects even have arguments. It also makes it straight-forward to create a new function t

Re: monkey patching __code__

2016-03-19 Thread Ian Kelly
On Fri, Mar 18, 2016 at 5:49 AM, Sven R. Kunze wrote: > Hi, > > we got an interesting problem. We need to monkeypatch Django's reverse > function: > > > First approach: > > urlresolvers.reverse = patched_reverse > > > Problem: some of Django's internal modules import urlresolvers.reverse > before

Re: monkey patching __code__

2016-03-19 Thread Sven R. Kunze
On 18.03.2016 15:23, Ian Kelly wrote: On Fri, Mar 18, 2016 at 7:47 AM, Ian Kelly wrote: Your patched version takes two extra arguments. Did you add the defaults for those to the function's __defaults__ attribute? And as an afterthought, you'll likely need to replace the function's __globals__

Re: monkey patching __code__

2016-03-19 Thread Ned Batchelder
On Friday, March 18, 2016 at 10:33:46 AM UTC-4, Sven R. Kunze wrote: > On 18.03.2016 15:23, Ian Kelly wrote: > > On Fri, Mar 18, 2016 at 7:47 AM, Ian Kelly wrote: > >> Your patched version takes two extra arguments. Did you add the > >> defaults for those to the function's __defaults__ attribute?

Re: monkey patching __code__

2016-03-18 Thread Sven R. Kunze
On 18.03.2016 15:33, Sven R. Kunze wrote: On 18.03.2016 15:23, Ian Kelly wrote: On Fri, Mar 18, 2016 at 7:47 AM, Ian Kelly wrote: Your patched version takes two extra arguments. Did you add the defaults for those to the function's __defaults__ attribute? And as an afterthought, you'll likely

monkey patching __code__

2016-03-18 Thread Sven R. Kunze
Hi, we got an interesting problem. We need to monkeypatch Django's reverse function: First approach: urlresolvers.reverse = patched_reverse Problem: some of Django's internal modules import urlresolvers.reverse before we can patch it for some reasons. Second approach: urlresolvers.rev

Re: monkey patching __code__

2016-03-18 Thread Terry Reedy
On 3/18/2016 10:32 AM, Sven R. Kunze wrote: Just to understand this better: why is [function.__defaults__] > not part of the code object but part of the function? compile(codestring, ...) compiles code into a code object. Besides being used to compile function bodies, which do have a functio