Skip to content

Commit e7a2ea9

Browse files
committed
Use re.fullmatch for Py3 and custom fullmatch for Py2
1 parent dec39ba commit e7a2ea9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

readthedocs/projects/forms.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99
unicode_literals,
1010
)
1111

12-
import re
12+
try:
13+
# TODO: remove this when we deprecate Python2
14+
# re.fullmatch is >= Py3.4 only
15+
from re import fullmatch
16+
except ImportError:
17+
# https://stackoverflow.com/questions/30212413/backport-python-3-4s-regular-expression-fullmatch-to-python-2
18+
import re
19+
20+
def fullmatch(regex, string, flags=0):
21+
"""Emulate python-3.4 re.fullmatch().""" # noqa
22+
return re.match("(?:" + regex + r")\Z", string, flags=flags)
23+
1324
from random import choice
1425

1526
from builtins import object
@@ -801,7 +812,7 @@ def clean_name(self):
801812
raise forms.ValidationError(
802813
_("Variable name can't contain spaces"),
803814
)
804-
elif not re.fullmatch('[a-zA-Z0-9_]+', name):
815+
elif not fullmatch('[a-zA-Z0-9_]+', name):
805816
raise forms.ValidationError(
806817
_('Only letters, numbers and underscore are allowed'),
807818
)

0 commit comments

Comments
 (0)