File tree Expand file tree Collapse file tree 2 files changed +12
-22
lines changed Expand file tree Collapse file tree 2 files changed +12
-22
lines changed Original file line number Diff line number Diff line change 66from contextlib import contextmanager , nullcontext
77from 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
Original file line number Diff line number Diff line change 2424 :copyright: Copyright 2008 by Armin Ronacher.
2525 :license: Python License.
2626"""
27- import sys
2827from _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+
624633def 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
652662if __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} ' )
You can’t perform that action at this time.
0 commit comments