Skip to content

Commit

Permalink
adding version compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheukting committed Jul 18, 2023
1 parent f7e2e88 commit 4fdba6c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
16 changes: 13 additions & 3 deletions hypothesis-python/src/hypothesis/strategies/_internal/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import operator
import re
import sys

try: # pragma: no cover
import re._constants as sre
Expand All @@ -18,7 +19,8 @@
import sre_constants as sre
import sre_parse

from hypothesis import reject, strategies as st
from hypothesis import reject
from hypothesis import strategies as st
from hypothesis.internal.charmap import as_general_categories, categories
from hypothesis.internal.compat import int_to_byte

Expand Down Expand Up @@ -50,6 +52,14 @@
GROUP_CACHE_STRATEGY = st.shared(st.builds(dict), key="hypothesis.regex.group_cache")


if sys.version_info[:2] < (3, 11):
ATOMIC_GROUP = object()
POSSESSIVE_REPEAT = object()
else:
ATOMIC_GROUP = sre.ATOMIC_GROUP
POSSESSIVE_REPEAT = sre.POSSESSIVE_REPEAT


@st.composite
def update_group(draw, group_name, strategy):
cache = draw(GROUP_CACHE_STRATEGY)
Expand Down Expand Up @@ -474,7 +484,7 @@ def recurse(codes):
# Regex 'a|b|c' (branch)
return st.one_of([recurse(branch) for branch in value[1]])

elif code in [sre.MIN_REPEAT, sre.MAX_REPEAT, sre.POSSESSIVE_REPEAT]:
elif code in [sre.MIN_REPEAT, sre.MAX_REPEAT, POSSESSIVE_REPEAT]:
# Regexes 'a?', 'a*', 'a+' and their non-greedy variants
# (repeaters)
at_least, at_most, subregex = value
Expand All @@ -494,7 +504,7 @@ def recurse(codes):
recurse(value[1]),
recurse(value[2]) if value[2] else st.just(empty),
)
elif code == sre.ATOMIC_GROUP:
elif code == ATOMIC_GROUP:
return _strategy(value, context, is_unicode)

else:
Expand Down
20 changes: 9 additions & 11 deletions hypothesis-python/tests/nocover/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

import re
import string
import sys
from functools import reduce

from hypothesis import assume, given, reject, strategies as st
import pytest

from hypothesis import assume, given, reject
from hypothesis import strategies as st
from hypothesis.strategies._internal.regex import base_regex_strategy


Expand Down Expand Up @@ -86,31 +90,25 @@ def test_fuzz_stuff(data):
assert regex.search(ex)


@pytest.mark.skipif(sys.version_info[:2] < (3, 11), reason="new syntax")
@given(st.data())
def test_regex_atomic_point(data):
pattern = "a(?>bc|b)c"
flags = 0

try:
regex = re.compile(pattern, flags=flags)
except (re.error, FutureWarning):
# Possible nested sets, e.g. "[[", trigger a FutureWarning
reject()
regex = re.compile(pattern, flags=flags)

ex = data.draw(st.from_regex(regex))
assert regex.search(ex)


@pytest.mark.skipif(sys.version_info[:2] < (3, 11), reason="new syntax")
@given(st.data())
def test_regex_processive(data):
pattern = '"[^"]*+"'
flags = 0

try:
regex = re.compile(pattern, flags=flags)
except (re.error, FutureWarning):
# Possible nested sets, e.g. "[[", trigger a FutureWarning
reject()
regex = re.compile(pattern, flags=flags)

ex = data.draw(st.from_regex(regex))
assert regex.search(ex)
Expand Down

0 comments on commit 4fdba6c

Please sign in to comment.