Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Prev Previous commit
Add tests
  • Loading branch information
AA-Turner committed Apr 1, 2025
commit b67e4aaec6aa3c2069e466737e89cd493fe400e8
40 changes: 40 additions & 0 deletions Lib/test/test_textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
# $Id$
#

import re
import unittest

from textwrap import TextWrapper, wrap, fill, dedent, indent, shorten
from textwrap import _cached_regex as cached_regex


class BaseTestCase(unittest.TestCase):
Expand Down Expand Up @@ -712,6 +714,44 @@ def test_do_not_break_long_words_or_on_hyphens(self):
'ng_option_', 'indeed-', 'good-bye"']
self.check_wrap(self.text2, 10, expected)


class TextWrapperCachedRegexTestCase(BaseTestCase):
def test_attr_access(self):
wrapper = TextWrapper()
# these names are not part of the public interface,
# but are not prefixed with an underscore.
for attr in 'wordsep_re', 'wordsep_simple_re', 'sentence_end_re':
self.assertTrue(hasattr(wrapper, attr))
self.assertIsInstance(getattr(wrapper, attr), re.Pattern)
self.assertIsInstance(getattr(TextWrapper, attr), re.Pattern)

setattr(wrapper, attr, attr)
self.assertEqual(getattr(wrapper, attr), attr)
self.assertIsInstance(getattr(TextWrapper, attr), re.Pattern)

def test_cached_regex(self):
class Spam:
pat1 = cached_regex('pat1')
pat2 = cached_regex('pat2')

# both patterns are instances of cached_regex
self.assertIsInstance(Spam.__dict__['pat1'], cached_regex)
self.assertIsInstance(Spam.__dict__['pat2'], cached_regex)

# the attribute is replaced with a compiled pattern when accessed
self.assertEqual(Spam.pat1, re.compile('pat1'))
self.assertEqual(Spam.__dict__['pat1'], re.compile('pat1'))

# including when accessed from an instance
spam = Spam()
self.assertEqual(spam.__dict__, {})
self.assertIsInstance(spam.__class__.__dict__['pat2'], cached_regex)
self.assertEqual(spam.pat2, re.compile('pat2'))
self.assertEqual(Spam.pat2, re.compile('pat2'))
self.assertEqual(spam.__class__.__dict__['pat2'], re.compile('pat2'))
self.assertIs(spam.pat2, Spam.pat2)


class IndentTestCases(BaseTestCase):

# called before each test method
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy