Skip to content

Commit

Permalink
bpo-38870: Remove dependency on contextlib to avoid performance regre…
Browse files Browse the repository at this point in the history
…ssion on import (pythonGH-17376)

https://bugs.python.org/issue38870



Automerge-Triggered-By: @pablogsal
  • Loading branch information
pablogsal authored and miss-islington committed Nov 25, 2019
1 parent e11f25d commit ded8888
Showing 1 changed file with 13 additions and 7 deletions.
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

0 comments on commit ded8888

Please sign in to comment.