gh-137855: Improve import time of textwrap by lazy importing re modules - #150156
gh-137855: Improve import time of textwrap by lazy importing re modules#150156XiaoWan-zi wants to merge 1 commit into
Conversation
picnixz
left a comment
There was a problem hiding this comment.
This is a nontrivial change that may break subclasses so closing, sorry.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Summary
textwrapimport time by lazy-importingre(PEP 810) instead of loading it at module import.TextWrapperword-separator regexes from class body to_compile_wordseps(), called on firstTextWrapper()instantiation. Compiled patterns remain class attributes, shared across instances (same semantics as before).LazyImportTest.test_lazy_importusingensure_lazy_imports()to guard against regressions.Part of the ongoing stdlib import-time work tracked in Improve import time of various stdlib modules #137855.
Motivation
textwrapis imported eagerly by many modules (e.g. tests,traceback,optparsepaths) often only fordedent()/indent(), which do not needre. Previously,import reat module level andre.compile()in theTextWrapperclass body causedreto load on everyimport textwrap.Before
After