diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2019-11-25 11:49:17 (GMT) |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-11-25 11:49:17 (GMT) |
commit | ded8888fbc33011dd39b7b1c86a5adfacc4943f3 (patch) | |
tree | 4d7c802cc23b815d6894645d3b7a8ede0f28cbd5 /Lib/ast.py | |
parent | e11f25dbd85709ed370e83348da07be03e9b3d7a (diff) | |
download | cpython-ded8888fbc33011dd39b7b1c86a5adfacc4943f3.zip cpython-ded8888fbc33011dd39b7b1c86a5adfacc4943f3.tar.gz cpython-ded8888fbc33011dd39b7b1c86a5adfacc4943f3.tar.bz2 |
bpo-38870: Remove dependency on contextlib to avoid performance regression on import (GH-17376)
https://bugs.python.org/issue38870
Automerge-Triggered-By: @pablogsal
Diffstat (limited to 'Lib/ast.py')
-rw-r--r-- | Lib/ast.py | 20 |
1 files changed, 13 insertions, 7 deletions
@@ -26,7 +26,6 @@ """ import sys from _ast import * -from contextlib import contextmanager def parse(source, filename='<unknown>', mode='exec', *, @@ -597,15 +596,22 @@ class _Unparser(NodeVisitor): 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): |