Nick Coghlan added the comment:

With __qualname__ being mutable, I agree that adding __code__.co_qualname 
wouldn't be a substitute for that. Instead, similar to the relationship between 
__name__ and __code__.co_name, they would start out the same, but the function 
attribute may change later (e.g. through the use of functools.wraps).

However, that also highlights why we need to use the compile time qualname in 
the traceback, rather than the runtime one: because we currently use the 
compile time name from the code object rather than the runtime name from the 
function.

A test case to demonstrate that:

>>> def f():
...     1/0
... 
>>> import functools
>>> @functools.wraps(f)
... def g():
...     return f()
... 
>>> g()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in g
  File "<stdin>", line 2, in f
ZeroDivisionError: division by zero
>>> g.__qualname__
'f'
>>> g.__name__
'f'

Note that "g" still appears in the relevant traceback line, even though both 
__name__ and __qualname__ have been updated to refer to "f". For a traceback we 
want to know where the source of the function actually lives, not where it 
claims to be from for human introspection purposes.

As far as the comparison to __module__ goes, I think that's a different case - 
we couldn't add that to the code object even if we wanted to, because the 
compiler doesn't know the module name, it only knows the file name. It's also 
possible to take an already compiled python file, move it to a different 
directory, and have the module name change due to the new location in the 
package hierarchy. __qualname__ is different as, like __name__, it's entirely 
local to the module and hence available to the compiler at compile time.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue2786>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to