Skip to content

Commit bb3605a

Browse files
committed
Make _TemplatePattern a singleton
1 parent 056dd07 commit bb3605a

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Lib/string.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ def capwords(s, sep=None):
5353

5454

5555
class _TemplatePattern:
56+
# This descriptor is overwritten in ``Template._compile_pattern()``.
5657
def __get__(self, instance, cls=None):
5758
if cls is None:
5859
return self
59-
# This descriptor is overwritten in ``_compile_pattern()``.
6060
return cls._compile_pattern()
61+
_TemplatePattern = _TemplatePattern()
6162

6263

6364
class Template:
@@ -72,7 +73,7 @@ class Template:
7273
braceidpattern = None
7374
flags = None # default: re.IGNORECASE
7475

75-
pattern = _TemplatePattern() # use a descriptor to compile the pattern
76+
pattern = _TemplatePattern # use a descriptor to compile the pattern
7677

7778
def __init_subclass__(cls):
7879
super().__init_subclass__()
@@ -83,7 +84,7 @@ def _compile_pattern(cls):
8384
import re # deferred import, for performance
8485

8586
cls_pattern = cls.__dict__.get('pattern')
86-
if cls_pattern and not isinstance(cls_pattern, _TemplatePattern):
87+
if cls_pattern is not None and cls_pattern is not _TemplatePattern:
8788
# Prefer a pattern defined on the class.
8889
pattern = cls_pattern
8990
else:

0 commit comments

Comments
 (0)