Issue 18656: setting function.__name__ doesn't affect repr() - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
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 2022-04-11 14:57 by admin. 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