bpo-41631: _ast module uses again a global state by vstinner · Pull Request #21961 · python/cpython
One of the bugs was already present in 3.8 – it's possible to objects across subinterpreters. We can agree to ignore that in 3.9.0\ and say that multiple interpreters still aren't safe. That's fine with me at this point; I opened ericsnowcurrently/multi-core-python#68 to track this.
You are correct that _ast.AST subclasses like _ast.Constant can be modified and changes impact all interpreters.
I attempted to better isolate the _ast module in subinterpreter (fix ericsnowcurrently/multi-core-python#68 ). My intent was to fix some bugs by doing that, but I introduced other new bugs :-(
Example:
import _ast
from test.support import run_in_subinterp
run_in_subinterp('import _ast; _ast.Constant.x=1')
print(_ast.Constant.x)
This code displays 1 with Python 3.8 and with my PR (master branch): same behavior (not better, not worse).
Since it's not a regression compared to Python 3.8, I suggest to open a new issue if you consider that the _ast module should be enhanced.
But then, the second commit here doesn't solve anything: it makes only one of the types static, when are lots of heap types still remaining. Or is it supposed to solve a different problem?
My intent is to move back to Python 3.8 status quo (behavior) for the _ast.AST type. For example, with my change, it's not possible to add a new attribute to _ast.AST in the main interpreter or in a subinterpreter.
Example:
import _ast
_ast.AST.x = 1
This code raises TypeError: can't set attributes of built-in/extension type 'ast.AST' with Python 3.8 and with my PR (not better, not worse).