Skip to content

Commit

Permalink
Use re.fullmatch for Py3 and custom fullmatch for Py2
Browse files Browse the repository at this point in the history
  • Loading branch information
humitos committed Jan 3, 2019
1 parent dec39ba commit 2beb9e2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
unicode_literals,
)

import re
try:
# TODO: remove this when we deprecate Python2
# re.fullmatch is >= Py3.4 only
from re import fullmatch
except ImportError:
# https://stackoverflow.com/questions/30212413/backport-python-3-4s-regular-expression-fullmatch-to-python-2
import re

def fullmatch(regex, string, flags=0):
"""Emulate python-3.4 re.fullmatch()."""
return re.match("(?:" + regex + r")\Z", string, flags=flags)

from random import choice

from builtins import object
Expand Down Expand Up @@ -801,7 +812,7 @@ def clean_name(self):
raise forms.ValidationError(
_("Variable name can't contain spaces"),
)
elif not re.fullmatch('[a-zA-Z0-9_]+', name):
elif not fullmatch('[a-zA-Z0-9_]+', name):
raise forms.ValidationError(
_('Only letters, numbers and underscore are allowed'),
)
Expand Down

0 comments on commit 2beb9e2

Please sign in to comment.