[Python-checkins] bpo-38870: Remove dependency on contextlib to avoid performance regression on import (GH-17376)

Miss Islington (bot) webhook-mailer at python.org
Mon Nov 25 06:49:22 EST 2019


https://github.com/python/cpython/commit/ded8888fbc33011dd39b7b1c86a5adfacc4943f3
commit: ded8888fbc33011dd39b7b1c86a5adfacc4943f3
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-11-25T03:49:17-08:00
summary:

bpo-38870: Remove dependency on contextlib to avoid performance regression on import (GH-17376)



https://bugs.python.org/issue38870



Automerge-Triggered-By: @pablogsal

files:
M Lib/ast.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 97914ebc66858..77eb24971ed24 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -26,7 +26,6 @@
 """
 import sys
 from _ast import *
-from contextlib import contextmanager
 
 
 def parse(source, filename='<unknown>', mode='exec', *,
@@ -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):



More information about the Python-checkins mailing list