Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Function name unchanged in error message

Reply
Thread Tools

Function name unchanged in error message

 
 
andrew cooke
Guest
Posts: n/a
 
      01-29-2010

Is there any way to change the name of the function in an error
message? In the example below I'd like the error to refer to bar(),
for example (the motivation is related function decorators - I'd like
the wrapper function to give the same name)

>>> def foo():

.... return 7
....
>>> foo.__name__ = 'bar'
>>> foo(123)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes no arguments (1 given)

Thanks,
Andrew
 
Reply With Quote
 
 
 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      01-29-2010
andrew cooke wrote:
> Is there any way to change the name of the function in an error
> message? In the example below I'd like the error to refer to bar(),
> for example (the motivation is related function decorators - I'd like
> the wrapper function to give the same name)
>
>
>>>> def foo():
>>>>

> ... return 7
> ...
>
>>>> foo.__name__ = 'bar'
>>>> foo(123)
>>>>

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: foo() takes no arguments (1 given)
>
> Thanks,
> Andrew
>

In [9]: def foo():
...: return 7
...:

In [10]: bar = foo

In [11]: bar(54)

TypeError: foo() takes no arguments (1 given)

JM
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-29-2010
On Fri, Jan 29, 2010 at 5:30 AM, andrew cooke <(E-Mail Removed)> wrote:
> Is there any way to change the name of the function in an error
> message? ┬*In the example below I'd like the error to refer to bar(),
> for example (the motivation is related function decorators - I'd like
> the wrapper function to give the same name)
>
>>>> def foo():

> ... ┬* ┬* return 7
> ...
>>>> foo.__name__ = 'bar'
>>>> foo(123)

> Traceback (most recent call last):
> ┬*File "<stdin>", line 1, in <module>
> TypeError: foo() takes no arguments (1 given)


It gets weirder:

>>> print(foo)

<function bar at 0x37b830>

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-29-2010
andrew cooke wrote:

> Is there any way to change the name of the function in an error
> message? In the example below I'd like the error to refer to bar(),
> for example (the motivation is related function decorators - I'd like
> the wrapper function to give the same name)
>
>>>> def foo():

> ... return 7
> ...
>>>> foo.__name__ = 'bar'
>>>> foo(123)

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: foo() takes no arguments (1 given)


The name is looked up in the code object. As that is immutable you have to
make a new one:

argnames = 'argcount nlocals stacksize flags code consts names varnames
filename name firstlineno lnotab'.split()

def f(): return 42

code = type(f.func_code)
function = type(f)

def make_code(proto, **kw):
for name in argnames:
if name not in kw:
kw[name] = getattr(proto, "co_" + name)
values = [kw[name] for name in argnames]
return code(*values)

if __name__ == "__main__":
def foo():
print "foo"

c = make_code(foo.func_code, name="bar")
foo.func_code = c

foo(42)

Peter
 
Reply With Quote
 
exarkun@twistedmatrix.com
Guest
Posts: n/a
 
      01-29-2010
On 02:10 pm, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>On Fri, Jan 29, 2010 at 5:30 AM, andrew cooke <(E-Mail Removed)>
>wrote:
>>Is there any way to change the name of the function in an error
>>message? *In the example below I'd like the error to refer to bar(),
>>for example (the motivation is related function decorators - I'd like
>>the wrapper function to give the same name)
>>>>>def foo():

>>... * * return 7
>>...
>>>>>foo.__name__ = 'bar'
>>>>>foo(123)

>>Traceback (most recent call last):
>>*File "<stdin>", line 1, in <module>
>>TypeError: foo() takes no arguments (1 given)

>
>It gets weirder:
>>>>print(foo)

><function bar at 0x37b830>


The name is represented in (at least) two places, on the function object
and on the code object:

>>> def foo(): pass

... >>> foo.func_name
'foo'
>>> foo.func_code.co_name

'foo'
>>> foo.func_name = 'bar'
>>> foo

<function bar at 0xb74f2cdc>
>>> foo.func_code.co_name = 'baz'

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: readonly attribute
>>>

new.function and new.code will let you construct new objects with
different values (and copying over whichever existing attributes you
want to preserve).

Jean-Paul
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      01-29-2010
On Jan 29, 2:30*pm, andrew cooke <(E-Mail Removed)> wrote:
> Is there any way to change the name of the function in an error
> message? *In the example below I'd like the error to refer to bar(),
> for example (the motivation is related function decorators - I'd like
> the wrapper function to give the same name)


Use the decorator module which does the right thing:
http://pypi.python.org/pypi/decorator
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-29-2010
En Fri, 29 Jan 2010 13:09:40 -0300, Michele Simionato
<(E-Mail Removed)> escribiˇ:

> On Jan 29, 2:30 pm, andrew cooke <(E-Mail Removed)> wrote:
>> Is there any way to change the name of the function in an error
>> message? In the example below I'd like the error to refer to bar(),
>> for example (the motivation is related function decorators - I'd like
>> the wrapper function to give the same name)

>
> Use the decorator module which does the right thing:
> http://pypi.python.org/pypi/decorator


The decorator module is a very fine addition to anyone's tool set -- but
in this case it is enough to use the wraps() function from the functools
standard module.

--
Gabriel Genellina

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-30-2010
Gabriel Genellina wrote:

> En Fri, 29 Jan 2010 13:09:40 -0300, Michele Simionato
> <(E-Mail Removed)> escribi├│:
>
>> On Jan 29, 2:30 pm, andrew cooke <(E-Mail Removed)> wrote:
>>> Is there any way to change the name of the function in an error
>>> message? In the example below I'd like the error to refer to bar(),
>>> for example (the motivation is related function decorators - I'd like
>>> the wrapper function to give the same name)

>>
>> Use the decorator module which does the right thing:
>> http://pypi.python.org/pypi/decorator

>
> The decorator module is a very fine addition to anyone's tool set -- but
> in this case it is enough to use the wraps() function from the functools
> standard module.


I don't know about the decorator module, but functools.wraps() doesn't
affect the error message:

>>> from functools import wraps
>>> def f(): pass

....
>>> @wraps(f)

.... def g(): pass
....
>>> g.__name__

'f'
>>> try: g(42)

.... except TypeError as e:
.... print e
....
g() takes no arguments (1 given)

Peter
 
Reply With Quote
 
andrew cooke
Guest
Posts: n/a
 
      01-30-2010
On Jan 29, 11:22*am, Peter Otten <(E-Mail Removed)> wrote:
> The name is looked up in the code object. As that is immutable you have to
> make a new one:

[details snipped]

thanks very much! sorry i didn't reply earlier - been travelling.

(also, thanks to any other replies - i'm just reading through at the
moment and this is the first one i've got to that will help me solve
it, but i don't mean to exclude anything later...!)

andrew
 
Reply With Quote
 
andrew cooke
Guest
Posts: n/a
 
      01-30-2010
On Jan 29, 5:37*pm, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> The decorator module is a very fine addition to anyone's tool set -- but *
> in this case it is enough to use the wraps() function from the functools *
> standard module.


ah, thanks! i thought something like this existed in the standard
lib, but couldn't find it.

andrew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
q; DataSet RowState unchanged =?Utf-8?B?SklNLkgu?= ASP .Net 1 05-24-2006 09:41 PM
constantly re-uploading (unchanged) DLL as site breaks (1.1) Kevin D. ASP .Net 4 03-08-2006 09:30 PM
How to keep URL in the web browser address bar unchanged ?? - URGENT.. Ahmed Jewahar ASP .Net 1 01-23-2006 02:23 PM
detecting unchanged images Roedy Green Java 7 06-27-2005 11:45 AM
Stop recompiling if unchanged header codes? Bryan Parkoff C++ 1 04-04-2005 11:43 PM



Advertisments