use re instead of re2 if error "Argument 'pattern' has incorrect type (expected bytes, got PythonRePattern)"
This commit is contained in:
parent
a021bba811
commit
f3e8fc10a9
|
@ -1,5 +1,7 @@
|
||||||
|
import re
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
import re2 as re
|
import re2
|
||||||
from flask import render_template, request, redirect, url_for, flash
|
from flask import render_template, request, redirect, url_for, flash
|
||||||
from flask_login import login_required, current_user
|
from flask_login import login_required, current_user
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
|
@ -501,9 +503,7 @@ def domain_detail_auto_create(custom_domain_id):
|
||||||
auto_create_test_local = local
|
auto_create_test_local = local
|
||||||
|
|
||||||
for rule in custom_domain.auto_create_rules:
|
for rule in custom_domain.auto_create_rules:
|
||||||
rule: AutoCreateRule
|
if regex_match(rule.regex, local):
|
||||||
regex = re.compile(rule.regex)
|
|
||||||
if re.fullmatch(regex, local):
|
|
||||||
auto_create_test_result = (
|
auto_create_test_result = (
|
||||||
f"{local}@{custom_domain.domain} passes rule #{rule.order}"
|
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())
|
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
|
||||||
|
|
9
tests/dashboard/test_domain_detail.py
Normal file
9
tests/dashboard/test_domain_detail.py
Normal 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")
|
Loading…
Reference in a new issue