Skip to content
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

bpo-38870: Remove dependency on contextlib to avoid performance regression on import #17376

Merged
merged 1 commit into from Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions Lib/ast.py
Expand Up @@ -26,7 +26,6 @@
"""
import sys
from _ast import *
from contextlib import contextmanager


def parse(source, filename='<unknown>', mode='exec', *,
Expand Down Expand Up @@ -597,15 +596,22 @@ def buffer(self):
self._buffer.clear()
return value

@contextmanager
def block(self):
class _Block:
"""A context manager for preparing the source for blocks. It adds
the character':', increases the indentation on enter and decreases
the indentation on exit."""
self.write(":")
self._indent += 1
yield
self._indent -= 1
def __init__(self, unparser):
self.unparser = unparser

def __enter__(self):
self.unparser.write(":")
self.unparser._indent += 1

def __exit__(self, exc_type, exc_value, traceback):
self.unparser._indent -= 1

def block(self):
return self._Block(self)

def traverse(self, node):
if isinstance(node, list):
Expand Down