Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: monkey patching __code__ Date: Wed, 23 Mar 2016 09:24:16 +0100 Lines: 19 Message-ID: References: <56EBEB5A.7050207@mail.de> <56EED3DE.7050101@mail.de> <56F13417.8060108@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de 3cLLLsWfNtAjyuLtXkUybADsL0l4L4dQoK86UU4sz1BQ== Cancel-Lock: sha1:iiZ/1wa97hWWNtIY+ohBZ2D1ytI= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'processing.': 0.07; 'imported': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stable.': 0.09; 'django': 0.13; 'nightmare': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'otherwise,': 0.20; 'parameter': 0.22; 'properties': 0.24; 'thus': 0.24; 'all.': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'possibility': 0.27; 'see,': 0.27; 'function': 0.28; 'code': 0.30; 'e.g.': 0.30; 'problem': 0.33; 'similar': 0.33; 'could': 0.35; 'but': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'lose': 0.63; 'more': 0.63; 'direct': 0.68; 'construct': 0.84; 'object:': 0.84; 'observed': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57b39563.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) 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:105523 "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 > maintenance nightmare (just think about a Django upgrade). 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. You might be able to change them in a similar way as "__code__" (i.e. direct modification). Otherwise, you would need to construct a new "function object" -- and lose the possibility to completely change the function object in place.