use re instead of re2 if error "Argument 'pattern' has incorrect type (expected bytes, got PythonRePattern)"

This commit is contained in:
Son 2021-12-11 19:38:22 +01:00
parent a021bba811
commit f3e8fc10a9
2 changed files with 26 additions and 4 deletions

View File

@ -1,5 +1,7 @@
import re
import arrow
import re2 as re
import re2
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
@ -501,9 +503,7 @@ def domain_detail_auto_create(custom_domain_id):
auto_create_test_local = local
for rule in custom_domain.auto_create_rules:
rule: AutoCreateRule
regex = re.compile(rule.regex)
if re.fullmatch(regex, local):
if regex_match(rule.regex, local):
auto_create_test_result = (
f"{local}@{custom_domain.domain} passes rule #{rule.order}"
)
@ -519,3 +519,16 @@ def domain_detail_auto_create(custom_domain_id):
)
return render_template("dashboard/domain_detail/auto-create.html", **locals())
def regex_match(rule_regex: str, local):
regex = re2.compile(rule_regex)
try:
if re2.fullmatch(regex, local):
return True
except TypeError: # re2 bug "Argument 'pattern' has incorrect type (expected bytes, got PythonRePattern)"
LOG.w("use re instead of re2 for %s %s", rule_regex, local)
regex = re.compile(rule_regex)
if re.fullmatch(regex, local):
return True
return False

View File

@ -0,0 +1,9 @@
from app.dashboard.views.domain_detail import regex_match
def test_regex_match(flask_client):
assert regex_match("prefix.*", "prefix-abcd")
# this generates re2 error "Argument 'pattern' has incorrect type (expected bytes, got PythonRePattern)"
# fallback to re
assert not regex_match("(?!abcd)s(\\.|-)?([a-z0-9]{4,6})", "abcd")