Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105212 > unrolled thread

Re: monkey patching __code__

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2016-03-18 07:47 -0600
Last post2016-03-18 07:47 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: monkey patching __code__ Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-18 07:47 -0600

#105212 — Re: monkey patching __code__

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-03-18 07:47 -0600
SubjectRe: monkey patching __code__
Message-ID<mailman.317.1458308891.12893.python-list@python.org>
On Fri, Mar 18, 2016 at 5:49 AM, Sven R. Kunze <srkunze@mail.de> 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 patch it for some reasons.
>
>
> Second approach:
>
> urlresolvers.reverse.__code__ = patched_reverse.__code__
>
>
> Unfortunately, we got this error:
>
>>>> reverse('login')
>
> patched_reverse() takes at least 3 arguments (1 given)
>
>
> These are the functions' signatures:
>
> def patched_reverse(viewname, urlconf=None, args=None, kwargs=None,
> prefix=None, current_app=None, get=None, fragment=None):
> def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None,
> current_app=None):
>
>
> Some ideas?

Your patched version takes two extra arguments. Did you add the
defaults for those to the function's __defaults__ attribute?

This sounds like a pretty hairy thing that you're trying to do. Surely
there must be some better way to accomplish the same goal.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web