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


Groups > comp.lang.python > #50733

Re: a little more explicative error message?

Path csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'subject:error': 0.03; 'argument': 0.05; 'interpreter': 0.05; 'explicit': 0.07; 'python3': 0.07; 'remaining': 0.07; 'arguments': 0.09; 'correct,': 0.09; 'decorator': 0.09; 'executes': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'returns,': 0.09; 'shame': 0.09; 'wrapped': 0.09; 'wrapper': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; "'__doc__',": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'argument,': 0.16; 'argument:': 0.16; 'before.': 0.16; 'callable': 0.16; 'callable,': 0.16; 'called,': 0.16; 'exactly,': 0.16; 'factory': 0.16; 'invokes': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'typeerror:': 0.16; 'wraps': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'module': 0.19; 'trying': 0.19; 'properly': 0.19; 'later': 0.20; 'feb': 0.22; '>>>': 0.22; 'code,': 0.22; 'input': 0.22; 'import': 0.22; 'minutes.': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'pass': 0.26; 'asking': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'forgot': 0.30; 'returned': 0.30; 'said,': 0.30; 'skip:( 20': 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; 'convenience': 0.31; 'doc': 0.31; 'file': 0.32; 'this.': 0.32; 'supposed': 0.32; 'linux': 0.33; '(most': 0.33; 'call.': 0.33; 'totally': 0.33; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'fun,': 0.36; 'returning': 0.36; 'done': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'half': 0.37; 'so,': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'above,': 0.60; 'catch': 0.60; 'tell': 0.60; 'lost': 0.61; 'received:173': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'happen': 0.63; 'subject:more': 0.64; 'more': 0.64; 'due': 0.66; 'minutes': 0.67; 'caused': 0.69; 'hour': 0.70; 'applying': 0.72; 'carefully': 0.74; 'received:fios.verizon.net': 0.84; '2013,': 0.91; 'apparent': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: a little more explicative error message?
Date Tue, 16 Jul 2013 04:25:06 -0400
References <ks2mkb$qqt$1@ger.gmane.org>
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:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <ks2mkb$qqt$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4762.1373963123.3114.python-list@python.org> (permalink)
Lines 102
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373963123 news.xs4all.nl 15936 [2001:888:2000:d::a6]:56638
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50733

Show key headers only | View raw


On 7/16/2013 1:44 AM, Vito De Tullio wrote:
> Hi
>
> I was writing a decorator and lost half an hour for a stupid bug in my code,
> but honestly the error the python interpreter returned to me doesn't
> helped...
>
> $ python3
> Python 3.3.0 (default, Feb 24 2013, 09:34:27)
> [GCC 4.7.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from functools import wraps
>>>> def dec(fun):
> ...  @wraps
> ...  def ret(*args, **kwargs):
> ...   return fun(*args, **kwargs)

At this point, when dec(fun) is called, the interpreter *successfully* 
executes ret = wraps(ret), which works, but is wrong, because wraps 
should be called with the wrapped function fun as its argument, not the 
wrapper function ret. The interpreter should instead execute
ret = partial(update_wrapper, wrapped = fun, ...)(ret)
which will update ret to look like fun.

> ...  return ret
> ...
>>>> @dec
> ... def fun(): pass

At this point, the interpreter *successfully* executes fun = dec(fun) = 
(wraps(ret))(fun), which causes ret = wraps(ret) before returning ret. 
Notice that when dec returns, the wraps code is over and done with. 
Instead the interpreter should execute
fun = (partial(update_wrapper, wrapped = fun, ...)(ret))(fun)

> ...
>>>> fun()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: update_wrapper() missing 1 required positional argument:
> 'wrapper'

Because fun has not been properly wrapped because you left out a call.

> Soo... at a first glance, no tricks... can you tell where is the error? :D

Not exactly, but it must have something to do with wraps, so the first 
thing I would do is to look at the wraps doc if I had not before. It 
only takes a couple of minutes.

 >>> from functools import wraps
 >>> help(wraps)
Help on function wraps in module functools:

wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', 
'__doc__', '__annotations__'), updated=('__dict__',))
     Decorator factory to apply update_wrapper() to a wrapper function

     Returns a decorator that invokes update_wrapper() with the decorated
        function as the wrapper argument and the arguments to wraps() as
        the remaining arguments. ...

This is pretty clear that wraps is not a decorator but a function that 
returns  decorator, which means that is must be called with an argument 
in the @del statement.

To really understand what is intended to happen so I could write the 
commentary above, I looked at the code to see
def wraps(wrapped, ...):
     '<docstring>'
     return partial(update_wrapper, wrapped=wrapped,
                    assigned=assigned, updated=updated)

> As I said, the error is totally mine, I just forgot to pass the function as
> parameter to wraps. But... what is "update_wrapper()"? and "wrapper"? There
> is no useful traceback or something... just... this.
>
> Ok, the documentation clearly says:
>
>      This is a convenience function to simplify applying partial() to
>      update_wrapper().
>
> So, again, shame on me... I just read carefully the doc *after* 20 minutes
> trying everything else...  still... I think should be useful if wraps()
> intercept this error saying something more explicit about the missing fun
> parameter...

How is a function that has already been called and has returned without 
apparent error supposed to catch a later error caused by its return not 
being correct, due to its input not being correct?

Your request is like asking a function f(x) to catch ZeroDivisionError 
if it return a 0 and that 0 is later used as a divisor after f returns, 
as in 1/f(x).

When you call wraps with a callable, there is no way for it to know that 
the callable in intended to the be the wrapper instead of the wrappee, 
unless it were clairvoyant ;-).

-- 
Terry Jan Reedy

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


Thread

Re: a little more explicative error message? Terry Reedy <tjreedy@udel.edu> - 2013-07-16 04:25 -0400

csiph-web