Skip to content

Commit f7ede84

Browse files
JelleZijlstraAA-Turner
authored andcommitted
pythongh-118761: Fix star-import of ast (python#132025)
Co-authored-by: Adam Turner <[email protected]>
1 parent e64ef34 commit f7ede84

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

Lib/_ast_unparse.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from contextlib import contextmanager, nullcontext
77
from enum import IntEnum, auto, _simple_enum
88

9-
__all__ = ('unparse',)
10-
119
# Large float and imaginary literals get turned into infinities in the AST.
1210
# We unparse those infinities to INFSTR.
1311
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
@@ -48,7 +46,7 @@ def next(self):
4846
_MULTI_QUOTES = ('"""', "'''")
4947
_ALL_QUOTES = (*_SINGLE_QUOTES, *_MULTI_QUOTES)
5048

51-
class _Unparser(NodeVisitor):
49+
class Unparser(NodeVisitor):
5250
"""Methods in this class recursively traverse an AST and
5351
output source code for the abstract syntax; original formatting
5452
is disregarded."""
@@ -1142,9 +1140,3 @@ def visit_MatchOr(self, node):
11421140
with self.require_parens(_Precedence.BOR, node):
11431141
self.set_precedence(_Precedence.BOR.next(), *node.patterns)
11441142
self.interleave(lambda: self.write(" | "), self.traverse, node.patterns)
1145-
1146-
1147-
def unparse(ast_obj):
1148-
unparser = _Unparser()
1149-
return unparser.visit(ast_obj)
1150-
unparse.__module__ = 'ast' # backwards compatibility

Lib/ast.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
:copyright: Copyright 2008 by Armin Ronacher.
2525
:license: Python License.
2626
"""
27-
import sys
2827
from _ast import *
2928

3029

@@ -621,8 +620,19 @@ class Param(expr_context):
621620
"""Deprecated AST node class. Unused in Python 3."""
622621

623622

623+
def unparse(ast_obj):
624+
global _Unparser
625+
try:
626+
unparser = _Unparser()
627+
except NameError:
628+
from _ast_unparse import Unparser as _Unparser
629+
unparser = _Unparser()
630+
return unparser.visit(ast_obj)
631+
632+
624633
def main():
625634
import argparse
635+
import sys
626636

627637
parser = argparse.ArgumentParser()
628638
parser.add_argument('infile', nargs='?', default='-',
@@ -651,15 +661,3 @@ def main():
651661

652662
if __name__ == '__main__':
653663
main()
654-
655-
def __dir__():
656-
dir_ = {n for n in globals() if not n.startswith('_') and n != 'sys'}
657-
return sorted(dir_ | {'unparse'})
658-
659-
def __getattr__(name):
660-
if name == 'unparse':
661-
global unparse
662-
from _ast_unparse import unparse
663-
return unparse
664-
665-
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

0 commit comments

Comments
 (0)