From 2beb9e2e38a571298d51cb234c8421d08970fac3 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Thu, 3 Jan 2019 17:37:55 +0100 Subject: [PATCH] Use re.fullmatch for Py3 and custom fullmatch for Py2 --- readthedocs/projects/forms.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/readthedocs/projects/forms.py b/readthedocs/projects/forms.py index 1b32c9d1315..8170b220e9a 100644 --- a/readthedocs/projects/forms.py +++ b/readthedocs/projects/forms.py @@ -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 @@ -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'), )