summaryrefslogtreecommitdiff
path: root/django/utils/regex_helper.py
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2019-10-26 15:32:40 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-10-29 09:14:24 +0100
commitc4cba148d8356596da80c4d93a96fb335e4b0b6b (patch)
tree369a2a92cfd365d8ec84fd6f0c64714d5b01fde4 /django/utils/regex_helper.py
parent6c6d24a4feb632fc07d35c2ce23bb00f6ea9215c (diff)
Refs #30899 -- Moved _lazy_re_compile() to the django.utils.regex_helper.
Diffstat (limited to 'django/utils/regex_helper.py')
-rw-r--r--django/utils/regex_helper.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py
index 8d55a79272..e910cac4aa 100644
--- a/django/utils/regex_helper.py
+++ b/django/utils/regex_helper.py
@@ -5,6 +5,10 @@ Used internally by Django and not intended for external use.
This is not, and is not intended to be, a complete reg-exp decompiler. It
should be good enough for a large class of URLS, however.
"""
+import re
+
+from django.utils.functional import SimpleLazyObject
+
# Mapping of an escape character to a representative of that class. So, e.g.,
# "\w" is replaced by "x" in a reverse URL. A value of None means to ignore
# this sequence. Any missing key is mapped to itself.
@@ -331,3 +335,17 @@ def flatten_result(source):
for i in range(len(result)):
result[i] += piece
return result, result_args
+
+
+def _lazy_re_compile(regex, flags=0):
+ """Lazily compile a regex with flags."""
+ def _compile():
+ # Compile the regex if it was not passed pre-compiled.
+ if isinstance(regex, str):
+ return re.compile(regex, flags)
+ else:
+ assert not flags, (
+ 'flags must be empty if regex is passed pre-compiled'
+ )
+ return regex
+ return SimpleLazyObject(_compile)