MCPcopy
hub / github.com/django/django / LocaleRegexDescriptor

Class LocaleRegexDescriptor

django/urls/resolvers.py:130–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130class LocaleRegexDescriptor:
131 def __get__(self, instance, cls=None):
132 """
133 Return a compiled regular expression based on the active language.
134 """
135 if instance is None:
136 return self
137 # As a performance optimization, if the given regex string is a regular
138 # string (not a lazily-translated string proxy), compile it once and
139 # avoid per-language compilation.
140 pattern = instance._regex
141 if isinstance(pattern, str):
142 instance.__dict__["regex"] = self._compile(pattern)
143 return instance.__dict__["regex"]
144 language_code = get_language()
145 if language_code not in instance._regex_dict:
146 instance._regex_dict[language_code] = self._compile(str(pattern))
147 return instance._regex_dict[language_code]
148
149 def _compile(self, regex):
150 try:
151 return re.compile(regex)
152 except re.error as e:
153 raise ImproperlyConfigured(
154 f'"{regex}" is not a valid regular expression: {e}'
155 ) from e
156
157
158class CheckURLMixin:

Callers 1

RegexPatternClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…