classification
Title: setting function.__name__ doesn't affect repr()
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: function with modified __name__ uses original name when there's an arg error
View: 4322
Assigned To: Nosy List: benjamin.peterson, gvanrossum, jcea, madison.may, mark.dickinson, pitrou, vstinner
Priority: normal Keywords:

Created on 2013-08-04 16:55 by gvanrossum, last changed 2013-08-04 21:05 by benjamin.peterson. This issue is now closed.

Files
File name Uploaded Description Edit
functools_wraps_oddity.py gvanrossum, 2013-08-04 20:50
Messages (8)
msg194394 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2013-08-04 16:55
In Python 3.2 and earlier:

>>> def f(): pass
...
>>> f.__name__ = 'g'
>>> f
<function g at 0x100487628>

However in Python 3.3 and later, the last line shows 'f' instead of 'g'.
msg194395 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-08-04 17:02
I guess this is a direct consequence of PEP 3155 [1].  From the PEP:

"""
The repr() and str() of functions and classes is modified to use __qualname__ rather than __name__.
"""

[1] http://www.python.org/dev/peps/pep-3155/
msg194396 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-08-04 17:12
Python 3.4.0a0 (default:62658d9d8926+, Aug  1 2013, 23:05:18) 
[GCC 4.4.1 20090725 (Red Hat 4.4.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(): pass
... 
>>> func
<function func at 0xb7698a54>

>>> func.__qualname__="PEP 3155"
>>> func
<function PEP 3155 at 0xb7698a54>
msg194402 - (view) Author: Madison May (madison.may) * Date: 2013-08-04 18:05
Yup, here are the relevant lines of the diff for PEP 3155:

@@ -568,7 +607,7 @@
 func_repr(PyFunctionObject *op)
 {
     return PyUnicode_FromFormat("<function %U at %p>",
-                               op->func_name, op);
+                               op->func_qualname, op);
 }
msg194420 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2013-08-04 20:50
Sorry. The actual problem reported was that the traceback uses the wrong name. I can still reproduce this in 3.4; attached is a repro.
msg194421 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-08-04 20:58
That's because argument error messages use the code's co_name rather than the functions. (This is a dupe of another bug about this that I can't find.)
msg194422 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-08-04 21:02
http://bugs.python.org/issue4322 ?
msg194423 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-08-04 21:05
Exactly
History
Date User Action Args
2013-08-04 21:05:14benjamin.petersonsetstatus: open -> closed
superseder: function with modified __name__ uses original name when there's an arg error
resolution: duplicate
messages: + msg194423
2013-08-04 21:02:27mark.dickinsonsetmessages: + msg194422
2013-08-04 20:58:43benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg194421
2013-08-04 20:50:08gvanrossumsetfiles: + functools_wraps_oddity.py

messages: + msg194420
2013-08-04 18:05:27madison.maysetnosy: + madison.may
messages: + msg194402
2013-08-04 17:12:08vstinnersetnosy: + vstinner
messages: + msg194396
2013-08-04 17:11:13jceasetnosy: + jcea
2013-08-04 17:02:50mark.dickinsonsetnosy: + mark.dickinson, pitrou
messages: + msg194395
2013-08-04 16:55:25gvanrossumcreate