File tree 2 files changed +12
-22
lines changed
2 files changed +12
-22
lines changed Original file line number Diff line number Diff line change 6
6
from contextlib import contextmanager , nullcontext
7
7
from enum import IntEnum , auto , _simple_enum
8
8
9
- __all__ = ('unparse' ,)
10
-
11
9
# Large float and imaginary literals get turned into infinities in the AST.
12
10
# We unparse those infinities to INFSTR.
13
11
_INFSTR = "1e" + repr (sys .float_info .max_10_exp + 1 )
@@ -48,7 +46,7 @@ def next(self):
48
46
_MULTI_QUOTES = ('"""' , "'''" )
49
47
_ALL_QUOTES = (* _SINGLE_QUOTES , * _MULTI_QUOTES )
50
48
51
- class _Unparser (NodeVisitor ):
49
+ class Unparser (NodeVisitor ):
52
50
"""Methods in this class recursively traverse an AST and
53
51
output source code for the abstract syntax; original formatting
54
52
is disregarded."""
@@ -1142,9 +1140,3 @@ def visit_MatchOr(self, node):
1142
1140
with self .require_parens (_Precedence .BOR , node ):
1143
1141
self .set_precedence (_Precedence .BOR .next (), * node .patterns )
1144
1142
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 24
24
:copyright: Copyright 2008 by Armin Ronacher.
25
25
:license: Python License.
26
26
"""
27
- import sys
28
27
from _ast import *
29
28
30
29
@@ -621,8 +620,19 @@ class Param(expr_context):
621
620
"""Deprecated AST node class. Unused in Python 3."""
622
621
623
622
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
+
624
633
def main ():
625
634
import argparse
635
+ import sys
626
636
627
637
parser = argparse .ArgumentParser ()
628
638
parser .add_argument ('infile' , nargs = '?' , default = '-' ,
@@ -651,15 +661,3 @@ def main():
651
661
652
662
if __name__ == '__main__' :
653
663
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