Skip to content

Commit 57ebbf3

Browse files
authored
Make google-re2 a requirement, not optional (cloud-custodian#119)
Currently, for Python 3.13 there were no pre-built binaries. For sys_platform=='darwin' and platform_machine=='arm64', there's a build error with google-re2; the fallback is to use the built-in re module.
1 parent 96be97c commit 57ebbf3

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

README.rst

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,19 @@ re2
4242
---
4343

4444
CEL specifies that regular expressions use re2 syntax,
45-
https://github.com/google/re2/wiki/Syntax. To keep its dependencies minimal and
46-
this implementation easily embeddable, cel-python uses the Python standard
47-
library ``re`` syntax by default. If a ``re2`` package is installed or the
48-
``re2`` extra is provided, cel-python will use ``re2`` syntax instead.
45+
https://github.com/google/re2/wiki/Syntax.
46+
As of the 0.4.0 release, the Google-RE2 module is part of the CEL distribution.
4947

50-
::
51-
52-
python -m pip install cel-python[re2]
53-
54-
.. warning:: Apple Silicon
48+
.. warning:: Apple Silicon and Python 3.13
5549

5650
See https://github.com/google/re2/issues/453,
5751
https://github.com/google/re2/issues/346,
5852
https://github.com/google/re2/issues/516
5953

60-
As of the 0.4.0 release, the google-re2 package does not build for Python 3.13 on the "darwin" platform with the "arm64" architecture.
54+
Google-RE2 does not build for Python 3.13 on the "darwin" platform with the "arm64" architecture.
55+
Currently, there is no pre-built binary for Python 3.13.
56+
57+
The built-in ``re`` is used as a fall-back, and does work for all but a few edge cases.
6158

6259
Command Line
6360
============

pyproject.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ dependencies = [
3333
"pendulum>=3.1.0",
3434
"pyyaml>=6.0.2",
3535
"jmespath>=1.0.1",
36+
"google-re2>=1.1.20240702 ; python_version!='3.13' or sys_platform!='darwin' or platform_machine!='arm64'",
3637
"tomli >= 1.1.0 ; python_version < '3.11'",
3738
]
3839

3940
[project.scripts]
4041
cel-python = "cel_python:main"
4142

42-
[project.optional-dependencies]
43-
re2 = [
44-
"google-re2",
45-
]
46-
4743
[build-system]
4844
requires = ["hatchling"]
4945
build-backend = "hatchling.build"

src/celpy/evaluation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@
8484
import celpy.celtypes
8585
from celpy.celparser import tree_dump
8686

87-
_USE_RE2 = False # Used by the test suite.
8887
try:
8988
import re2
9089

91-
_USE_RE2 = True # Used by the test suite.
92-
9390
def function_matches(text: str, pattern: str) -> "Result":
91+
"""Implementation of the ``match()`` function using ``re2``"""
9492
try:
9593
m = re2.search(pattern, text)
9694
except re2.error as ex:
@@ -99,8 +97,12 @@ def function_matches(text: str, pattern: str) -> "Result":
9997
return celpy.celtypes.BoolType(m is not None)
10098

10199
except ImportError: # pragma: no cover
100+
# There is a build issue with python_version=='3.13' and sys_platform=='darwin'
101+
# See https://github.com/google/re2/issues/516
102+
# We fall back to using re, which passes the essential tests
102103

103104
def function_matches(text: str, pattern: str) -> "Result":
105+
"""Alternative implementation of the ``match()`` function for systems where ``re2`` can't be installed."""
104106
try:
105107
m = re.search(pattern, text)
106108
except re.error as ex:

tests/test_evaluation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ def test_operator_in():
169169
assert isinstance(operator_in(celtypes.IntType(-1), container_2), CELEvalError)
170170

171171

172-
@pytest.mark.skipif(not celpy.evaluation._USE_RE2, reason="Not using RE2")
172+
@pytest.mark.skipif("re2" not in celpy.evaluation.function_matches.__globals__, reason="Not using RE2")
173173
def test_function_matches_re2():
174174
empty_string = celtypes.StringType("")
175175
# re-specific patterns which behave differently than re2
176176
assert function_matches(empty_string, "^\\z")
177177
assert isinstance(function_matches(empty_string, "^\\Z"), CELEvalError)
178178

179179

180-
@pytest.mark.skipif(celpy.evaluation._USE_RE2, reason="Using RE2")
180+
@pytest.mark.skipif("re2" in celpy.evaluation.function_matches.__globals__, reason="Using RE2")
181181
def test_function_matches_re():
182182
empty_string = celtypes.StringType("")
183183
# re2-specific patterns which behave differently than standard re

type_check/lineprecision.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ celpy.adapter 142 34 3 9 90 6
66
celpy.c7nlib 1663 344 16 152 1151 0
77
celpy.celparser 411 138 67 23 183 0
88
celpy.celtypes 1480 385 15 234 809 37
9-
celpy.evaluation 3873 1136 249 244 2228 16
9+
celpy.evaluation 3875 1134 249 244 2232 16
1010
xlate 0 0 0 0 0 0
1111
xlate.c7n_to_cel 1755 397 103 144 1105 6

0 commit comments

Comments
 (0)