8000 gh-117865: Defer import of re in ast by JelleZijlstra · Pull Request #119546 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117865: Defer import of re in ast #119546

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
May 28, 2024
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
Next Next commit
gh-117865: Defer import of re in ast
This is used only by ast.get_source_segment(), so it seems sensible to avoid importing it.
  • Loading branch information
JelleZijlstra committed May 25, 2024
commit 9cbb64cf8a850dafebf68abb16a9cb9b12db247c
8 changes: 6 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
:license: Python License.
"""
import sys
import re
from _ast import *
from contextlib import contextmanager, nullcontext
from enum import IntEnum, auto, _simple_enum
Expand Down Expand Up @@ -325,12 +324,17 @@ def get_docstring(node, clean=True):
return text


_line_pattern = re.compile(r"(.*?(?:\r\n|\n|\r|$))")
_line_pattern = None
def _splitlines_no_ff(source, maxlines=None):
"""Split a string into lines ignoring form feed and other chars.

This mimics how the Python parser splits source code.
"""
global _line_pattern
if _line_pattern is None:
import re
_line_pattern = re.compile(r"(.*?(?:\r\n|\n|\r|$))")

lines = []
for lineno, match in enumerate(_line_pattern.finditer(source), 1):
if maxlines is not None and lineno > maxlines:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the import time of the :mod:`ast` module by deferring the import of
:mod:`re`. Patch by Jelle Zijlstra.
Loading
0