classification
Title: getting a class source regresses in Python 3.9
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: t-vi
Priority: normal Keywords:

Created on 2020-12-17 09:06 by t-vi, last changed 2020-12-17 09:11 by t-vi.

Messages (2)
msg383223 - (view) Author: Thomas Viehmann (t-vi) * Date: 2020-12-17 09:06
getting a class source regresses in Python 3.9 onwards.
The following worked in Python 3.8, now it doesn't anymore for 3.9.1 and 3.10.0a2:

(save as foo.py)

import inspect

class Foo:
    def spam(self):
        global Bar
        class Bar:
            pass
        print(inspect.getsource(Bar))

Foo().spam()


It seems that getsource is very brittle for classes.
msg383224 - (view) Author: Thomas Viehmann (t-vi) * Date: 2020-12-17 09:11
I might add that this is a case I hit in the PyTorch test suite.

I tried to fix this a long time ago in a pricipled way in
https://bugs.python.org/issue33826

and Guido explained that fixing it was too heavy handed to be worthwile in

https://mail.python.org/archives/list/python-dev@python.org/thread/AC5B5VORS6FF6KJ3EG5DIV3ZLAQB6DIY/#UL376F3Y6FKJUZ2HZDA3ERFUDDOX67X4
History
Date User Action Args
2020-12-17 09:11:37t-visetmessages: + msg383224
2020-12-17 09:06:46t-vicreate