Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: monkey patching __code__ Date: Fri, 18 Mar 2016 15:42:01 +0100 Lines: 33 Message-ID: References: <56EBEB5A.7050207@mail.de> <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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105221 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 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