Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,19 @@ re2
---

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

::

python -m pip install cel-python[re2]

.. warning:: Apple Silicon
.. warning:: Apple Silicon and Python 3.13

See https://github.com/google/re2/issues/453,
https://github.com/google/re2/issues/346,
https://github.com/google/re2/issues/516

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.
Google-RE2 does not build for Python 3.13 on the "darwin" platform with the "arm64" architecture.
Currently, there is no pre-built binary for Python 3.13.

The built-in ``re`` is used as a fall-back, and does work for all but a few edge cases.

Command Line
============
Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ dependencies = [
"pendulum>=3.1.0",
"pyyaml>=6.0.2",
"jmespath>=1.0.1",
"google-re2>=1.1.20240702 ; python_version!='3.13' or sys_platform!='darwin' or platform_machine!='arm64'",
"tomli >= 1.1.0 ; python_version < '3.11'",
]

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

[project.optional-dependencies]
re2 = [
"google-re2",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down
8 changes: 5 additions & 3 deletions src/celpy/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@
import celpy.celtypes
from celpy.celparser import tree_dump

_USE_RE2 = False # Used by the test suite.
try:
import re2

_USE_RE2 = True # Used by the test suite.

def function_matches(text: str, pattern: str) -> "Result":
"""Implementation of the ``match()`` function using ``re2``"""
try:
m = re2.search(pattern, text)
except re2.error as ex:
Expand All @@ -99,8 +97,12 @@ def function_matches(text: str, pattern: str) -> "Result":
return celpy.celtypes.BoolType(m is not None)

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

def function_matches(text: str, pattern: str) -> "Result":
"""Alternative implementation of the ``match()`` function for systems where ``re2`` can't be installed."""
try:
m = re.search(pattern, text)
except re.error as ex:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ def test_operator_in():
assert isinstance(operator_in(celtypes.IntType(-1), container_2), CELEvalError)


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


@pytest.mark.skipif(celpy.evaluation._USE_RE2, reason="Using RE2")
@pytest.mark.skipif("re2" in celpy.evaluation.function_matches.__globals__, reason="Using RE2")
def test_function_matches_re():
empty_string = celtypes.StringType("")
# re2-specific patterns which behave differently than standard re
Expand Down
2 changes: 1 addition & 1 deletion type_check/lineprecision.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ celpy.adapter 142 34 3 9 90 6
celpy.c7nlib 1663 344 16 152 1151 0
celpy.celparser 411 138 67 23 183 0
celpy.celtypes 1480 385 15 234 809 37
celpy.evaluation 3873 1136 249 244 2228 16
celpy.evaluation 3875 1134 249 244 2232 16
xlate 0 0 0 0 0 0
xlate.c7n_to_cel 1755 397 103 144 1105 6
Loading