8000 gh-118761: Fix star-import of ast (alternative) by JelleZijlstra · Pull Request #132025 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118761: Fix star-import of ast (alternative) #132025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions Lib/_ast_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from contextlib import contextmanager, nullcontext
from enum import IntEnum, auto, _simple_enum

__all__ = ('unparse',)

# Large float and imaginary literals get turned into infinities in the AST.
# We unparse those infinities to INFSTR.
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
Expand Down Expand Up @@ -48,7 +46,7 @@ def next(self):
_MULTI_QUOTES = ('"""', "'''")
_ALL_QUOTES = (*_SINGLE_QUOTES, *_MULTI_QUOTES)

class _Unparser(NodeVisitor):
class Unparser(NodeVisitor):
"""Methods in this class recursively traverse an AST and
output source code for the abstract syntax; original formatting
is disregarded."""
Expand Down Expand Up @@ -1142,9 +1140,3 @@ def visit_MatchOr(self, node):
with self.require_parens(_Precedence.BOR, node):
self.set_precedence(_Precedence.BOR.next(), *node.patterns)
self.interleave(lambda: self.write(" | "), self.traverse, node.patterns)


def unparse(ast_obj):
unparser = _Unparser()
return unparser.visit(ast_obj)
unparse.__module__ = 'ast' # backwards compatibility
21 changes: 9 additions & 12 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ class AugStore(expr_context):
class Param(expr_context):
"""Deprecated AST node class. Unused in Python 3."""

_Unparser = None

def unparse(ast_obj):
global _Unparser
if _Unparser is None:
from _ast_unparse import Unparser as _Unparser
unparser = _Unparser()
return unparser.visit(ast_obj)


def main():
import argparse
Expand Down Expand Up @@ -651,15 +660,3 @@ def main():

if __name__ == '__main__':
main()

def __dir__():
dir_ = {n for n in globals() if not n.startswith('_') and n != 'sys'}
return sorted(dir_ | {'unparse'})

def __getattr__(name):
if name == 'unparse':
global unparse
from _ast_unparse import unparse
return unparse

raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
Loading
0