Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'true,': 0.05; '(of': 0.07; 'attribute': 0.07; 'filing': 0.07; 'modifying': 0.07; 'decorator': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; '"""return': 0.16; '201': 0.16; 'attribute.': 0.16; 'called.': 0.16; 'function?': 0.16; 'general.': 0.16; 'portable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'robust.': 0.16; 'simpson': 0.16; 'stdlib.': 0.16; 'attach': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'skip:f 30': 0.19; 'later': 0.20; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'certainly': 0.24; 'decorators': 0.24; 'requirement.': 0.24; 'java': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'code': 0.31; 'convince': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'another': 0.32; "i'd": 0.34; 'but': 0.35; 'there': 0.35; 'accessing': 0.36; 'instances': 0.36; 'method': 0.36; "i'll": 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'black': 0.61; 'received:173': 0.61; 'first': 0.61; 'received:fios.verizon.net': 0.84; 'not:': 0.91; 'whereas': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Skipping decorators in unit tests Date: Fri, 11 Oct 2013 04:32:56 -0400 References: <525768a5$0$29984$c3e8da3$5496439d@news.astraweb.com> <20131011031319.GA30742@cskk.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381480506 news.xs4all.nl 15979 [2001:888:2000:d::a6]:42089 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56663 On 10/11/2013 4:17 AM, Terry Reedy wrote: > On 10/10/2013 11:13 PM, Cameron Simpson wrote: >> On 11Oct2013 02:55, Steven D'Aprano >> wrote: > >>> def undecorate(f): >>> """Return the undecorated inner function from function f.""" >>> return f.func_closure[0].cell_contents >> >> Whereas this feels like black magic. Is this portable to any decorated >> function? If so, I'd have hoped it was in the stdlib. If not: black >> magic. >> >>> And in use: >>> >>> py> f(100) >>> 201 >>> py> undecorate(f)(100) >>> 200 >> >> All lovely, provided you can convince me that undecorate() is robust. >> (And if you can, I'll certainly be filing it away in my funcutils >> module for later use.) > > It only works if the decorator returns a closure with the original > function as the first member (of func_closure). Often true, but not at > all a requirement. Another standard decorator method is to write a class with a .__call__ method and attach the original function to instances as an attribute. (Indeed, decorators were borrowed from class-happy Java ;-). But there is no standard as to what the function attribute of instances is called. The OP's request for accessing the function without modifying the tested code cannot be met in general. One must have access to the tested code. -- Terry Jan Reedy