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


Groups > comp.lang.python > #105212

Re: monkey patching __code__

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: monkey patching __code__
Date Fri, 18 Mar 2016 07:47:28 -0600
Lines 43
Message-ID <mailman.317.1458308891.12893.python-list@python.org> (permalink)
References <56EBEB5A.7050207@mail.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de W+pg26jyqNo8b6RofeGF3A6oMreOziP0S9fthUn8aOOA==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'defaults': 0.05; 'error:': 0.05; "django's": 0.09; 'def': 0.13; '2016': 0.16; 'attribute?': 0.16; 'given)': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'do.': 0.22; 'arguments': 0.22; 'function:': 0.22; 'problem:': 0.22; 'trying': 0.22; 'am,': 0.23; 'second': 0.24; 'import': 0.24; 'patch': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:u 20': 0.28; 'surely': 0.33; 'add': 0.34; 'received:google.com': 0.35; 'problem.': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209.85.213': 0.37; 'version': 0.38; 'received:209': 0.38; 'skip:p 20': 0.38; 'hi,': 0.38; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'mar': 0.65; 'reverse': 0.66; 'sounds': 0.76; "functions'": 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=MjHdqecYg9fGrIjavPDsdRKexPSG0/yiooQVJy7myOg=; b=zE/ZWixjxQmk/+EmhPps5mSwXbYn/o4BjnXrbLkH6Lc33KUnWmshmm7I1Hgi7EjDgo N6ydB/s1yBR4afYJ5Qby2ArOLtXz4q7aryGNZZueSy9NxkD1OaTcHpQoCgf8WRRSMmG+ jOq4U/qUjcdhzdQF+aqz3ZE0lkBmtlUyPTa2clXZDB/EAJMJWw5LtSCiuwo7ZEp8r4vQ 6ONUo0CA2UrTqeC4flpz9CqCjFeBkPZL1LuabsJOVk+gxlZGlpFrh50D05/1NhovwxKl VupcAimqwyDi8q/by5v2EOu6ZzV8FpVgooJNq8sspNaMaQFX+BazfwkiwjNc2hVMMMly 9yjA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=MjHdqecYg9fGrIjavPDsdRKexPSG0/yiooQVJy7myOg=; b=harFsM7smNDW6rYQmzWtqo5XjH9pHLczyPdFxP/bhpj7eGhPiyCNlTPbkviQSuMDXc fmBD8qdGXE3HKp5HCOtMq8OWKENz/vsJkh/5CY+hc0RX3lR/fvdj27Vb+EpIcUViBRah RLQaz40FsRVH5jcWPcePCktGMjUqmgiE6N5jjeBWb1QDl6WYEpgCxml7+IW8FXkPxK+8 lmH60A/oSOUqM1KKQ1S3Ybee3LbLksEJ89Pdj719xQ4urYsLNsiEIh6lOOc+1EW/7Etz c6blUpwJjCOgwQR/WIhV1SPE3orpZDaOXgmRVB6+xYx2bJsxvpN3iv/sKK0Uw44l7km7 8lGQ==
X-Gm-Message-State AD7BkJLhejPyKKwnmH2j7oMT6Y8Mo/8Lvc4wvPjnz61d0VbrWvr2WUaVFY3i+y8T6W+CQwj4T8xpZgY+fsXXhw==
X-Received by 10.50.61.177 with SMTP id q17mr13421313igr.68.1458308887590; Fri, 18 Mar 2016 06:48:07 -0700 (PDT)
In-Reply-To <56EBEB5A.7050207@mail.de>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105212

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web