-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-118761: Optimise import time for textwrap #131956
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
Draft
AA-Turner
wants to merge
2
commits into
python:main
Choose a base branch
from
AA-Turner:opt-tw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,26 @@ | |
# Copyright (C) 2002 Python Software Foundation. | ||
# Written by Greg Ward <[email protected]> | ||
|
||
import re | ||
|
||
__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent', 'shorten'] | ||
|
||
|
||
class _cached_regex: | ||
def __init__(self, pattern): | ||
self.pattern = pattern | ||
|
||
def __set_name__(self, owner, name): | ||
self.attr_name = name | ||
|
||
def __get__(self, instance, owner=None): | ||
if owner is None: | ||
return self | ||
import re | ||
# replace this descriptor with the compiled pattern | ||
pat = re.compile(self.pattern) | ||
setattr(owner, self.attr_name, pat) | ||
return pat | ||
|
||
|
||
# Hardcode the recognized whitespace characters to the US-ASCII | ||
# whitespace characters. The main reason for doing this is that | ||
# some Unicode spaces (like \u00a0) are non-breaking whitespaces. | ||
|
@@ -73,41 +89,39 @@ class TextWrapper: | |
# (after stripping out empty strings). | ||
word_punct = r'[\w!"\'&.,?]' | ||
letter = r'[^\d\W]' | ||
whitespace = r'[%s]' % re.escape(_whitespace) | ||
nowhitespace = '[^' + whitespace[1:] | ||
wordsep_re = re.compile(r''' | ||
whitespace = fr'[{_whitespace}]' | ||
no_whitespace = f'[^{_whitespace}]' | ||
wordsep_re = _cached_regex(fr'''(?x) | ||
( # any whitespace | ||
%(ws)s+ | ||
{whitespace}+ | ||
| # em-dash between words | ||
(?<=%(wp)s) -{2,} (?=\w) | ||
(?<={word_punct}) -{{2,}} (?=\w) | ||
| # word, possibly hyphenated | ||
%(nws)s+? (?: | ||
{no_whitespace}+? (?: | ||
# hyphenated word | ||
-(?: (?<=%(lt)s{2}-) | (?<=%(lt)s-%(lt)s-)) | ||
(?= %(lt)s -? %(lt)s) | ||
-(?: (?<={letter}{{2}}-) | (?<={letter}-{letter}-)) | ||
(?= {letter} -? {letter}) | ||
| # end of word | ||
(?=%(ws)s|\Z) | ||
(?={whitespace}|\Z) | ||
| # em-dash | ||
(?<=%(wp)s) (?=-{2,}\w) | ||
(?<={word_punct}) (?=-{{2,}}\w) | ||
) | ||
)''' % {'wp': word_punct, 'lt': letter, | ||
'ws': whitespace, 'nws': nowhitespace}, | ||
re.VERBOSE) | ||
del word_punct, letter, nowhitespace | ||
)''') | ||
del word_punct, letter, no_whitespace | ||
|
||
# This less funky little regex just split on recognized spaces. E.g. | ||
# "Hello there -- you goof-ball, use the -b option!" | ||
# splits into | ||
# Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/ | ||
wordsep_simple_re = re.compile(r'(%s+)' % whitespace) | ||
wordsep_simple_re = _cached_regex(fr'({whitespace}+)') | ||
del whitespace | ||
|
||
# XXX this is not locale- or charset-aware -- string.lowercase | ||
# is US-ASCII only (and therefore English-only) | ||
sentence_end_re = re.compile(r'[a-z]' # lowercase letter | ||
r'[\.\!\?]' # sentence-ending punct. | ||
r'[\"\']?' # optional end-of-quote | ||
r'\Z') # end of chunk | ||
sentence_end_re = _cached_regex(r'[a-z]' # lowercase letter | ||
r'[\.\!\?]' # sentence-ending punct. | ||
r'[\"\']?' # optional end-of-quote | ||
r'\Z') # end of chunk | ||
|
||
def __init__(self, | ||
width=70, | ||
|
@@ -250,7 +264,7 @@ def _wrap_chunks(self, chunks): | |
""" | ||
lines = [] | ||
if self.width <= 0: | ||
raise ValueError("invalid width %r (must be > 0)" % self.width) | ||
raise ValueError(f"invalid width {self.width!r} (must be > 0)") | ||
if self.max_lines is not None: | ||
if self.max_lines > 1: | ||
indent = self.subsequent_indent | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.