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


Groups > comp.lang.python > #105221

Re: monkey patching __code__

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: monkey patching __code__
Date Fri, 18 Mar 2016 15:42:01 +0100
Lines 33
Message-ID <mailman.324.1458312124.12893.python-list@python.org> (permalink)
References <56EBEB5A.7050207@mail.de> <CALwzid=XVSnSEt14Oer-2dO-0kT+fLi1EP4mNehXc0+zDqTX=g@mail.gmail.com> <CALwzidkyyQRSDPbthjKi4YDBtGGZe7M8J1LOA5PjzAn+qk8skg@mail.gmail.com> <56EC11BE.1040709@mail.de>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de BdznnezdgmkH3fiOjvbwyQHsoakXVmsX263jSGfaxrsw==
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'defaults': 0.05; 'globals': 0.09; 'def': 0.13; 'importing': 0.15; '2016': 0.16; 'attribute"': 0.16; 'attribute?': 0.16; 'crashes': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'am,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'received:10.0': 0.34; 'add': 0.34; 'best,': 0.35; 'replace': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'things': 0.38; 'version': 0.38; 'takes': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; "you'll": 0.61; 'charset:windows-1252': 0.62; 'mar': 0.65; 'here': 0.66
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1458312122; bh=S29Oe10OOX9b5vfmm7q25BDvqzmhcuOPf7q5xStzdP4=; h=Subject:To:References:From:Date:In-Reply-To:From; b=v/+8/T1AmvELLouBvBmUuERUcs0D1//aji0drosgKo8DRso6LbOAE89Pu3HGLcagG 0udIbM16ae1I5LiQ3oPRMQMfqR8doxJqbNLI+MneUJKN7wp/rJSiFu9rDhobHgp2tj 6Pfqo4g7GL3kH+9RWybFhZBuokgIZ3IsOUg2mHog=
In-Reply-To <56EC11BE.1040709@mail.de>
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 892
X-purgate-ID 154282::1458312122-000018ED-3F6C63FF/0/0
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:105221

Show key headers only | View raw


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 <ian.g.kelly@gmail.com> 
>> 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.


def f(a, b=None, c=None):
     print(a, b, c)

def f_patch(a, b=None, c=None, d=None, e=None):
     print(a, b, c, d, e)


f.__code__ = f_patch.__code__
f.__defaults__ = f_patch.__defaults__
f.__kwdefaults__ = f_patch.__kwdefaults__
f.__globals__ = f_patch.__globals__   <<<<< crashes here with 
"AttributeError: readonly attribute"


f('a', e='e')


It seems like we need to work with the globals we have aka. importing 
things locally.


Best,
Sven

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


Thread

Re: monkey patching __code__ "Sven R. Kunze" <srkunze@mail.de> - 2016-03-18 15:42 +0100

csiph-web