Skip to content

Use environment variables to use std::regex #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ capabilities.

Latest version documentation is automatically rendered by `Read the Docs`__.

__ http://json-schema.org/documentation.html
__ http://json-schema.org/docs.html
__ https://python-rapidjson.readthedocs.io/en/latest/


Expand Down Expand Up @@ -123,3 +123,27 @@ some aspects. See `this section`__ in the documentation for further details.
__ https://python-rapidjson.readthedocs.io/en/latest/quickstart.html#incompatibilities

.. _RapidJSON: http://rapidjson.org/

Regex Engine
------------

By default RapidJson uses a simple NFA regular expression engine for it's schema
validation, see the section `RapidJson Regular Expression`__, it is possible
to use ``std::regex`` instead of the original implementation by setting the
environmental variable ``RAPIDJSON_SCHEMA_USE_STDREGEX=1``.

.. code-block:: bash

$ export RAPIDJSON_SCHEMA_USE_STDREGEX=1
$ pip install python-rapidjson

After installation, you can verify that std::regex is being used by checking the
constant in the module:

.. code-block:: python

>>> import rapidjson
>>> rapidjson.RAPIDJSON_SCHEMA_USE_STDREGEX
1

__ http://rapidjson.org/md_doc_schema.html#Regex
24 changes: 24 additions & 0 deletions docs/validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
rapidjson.JSONDecodeError: Invalid JSON

============
Regex Engine
============
By default RapidJson uses a simple NFA regular expression engine for it's schema
validation, see the section `RapidJson Regular Expression`__, it is possible
to use ``std::regex`` instead of the original implementation by setting the
environmental variable ``RAPIDJSON_SCHEMA_USE_STDREGEX=1``.

.. code-block:: bash

$ export RAPIDJSON_SCHEMA_USE_STDREGEX=1
$ pip install python-rapidjson

After installation, you can verify that std::regex is being used by checking the
constant in the module:

.. code-block:: python

>>> import rapidjson
>>> rapidjson.RAPIDJSON_SCHEMA_USE_STDREGEX
1

__ http://rapidjson.org/md_doc_schema.html#Regex
1 change: 1 addition & 0 deletions rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4046,6 +4046,7 @@ module_exec(PyObject* m)
MM_COERCE_KEYS_TO_STRINGS)
|| PyModule_AddIntConstant(m, "MM_SKIP_NON_STRING_KEYS", MM_SKIP_NON_STRING_KEYS)
|| PyModule_AddIntConstant(m, "MM_SORT_KEYS", MM_SORT_KEYS)
|| PyModule_AddIntConstant(m, "RAPIDJSON_SCHEMA_USE_STDREGEX", RAPIDJSON_SCHEMA_USE_STDREGEX)

|| PyModule_AddStringConstant(m, "__version__",
STRINGIFY(PYTHON_RAPIDJSON_VERSION))
Expand Down
15 changes: 14 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os.path
import sys
from os import environ

try:
from setuptools import setup, Extension
Expand Down Expand Up @@ -52,13 +53,25 @@
with open('CHANGES.rst', encoding='utf-8') as f:
CHANGES = f.read()

if environ.get('RAPIDJSON_SCHEMA_USE_STDREGEX', None):
RAPIDJSON_SCHEMA_USE_STDREGEX = 1
RAPIDJSON_SCHEMA_USE_INTERNALREGEX = 0
else:
RAPIDJSON_SCHEMA_USE_STDREGEX = 0
RAPIDJSON_SCHEMA_USE_INTERNALREGEX = 1

extension_options = {
'sources': ['./rapidjson.cpp'],
'include_dirs': [rj_include_dir],
'define_macros': [('PYTHON_RAPIDJSON_VERSION', VERSION)],
'define_macros': [
('PYTHON_RAPIDJSON_VERSION', VERSION),
('RAPIDJSON_SCHEMA_USE_INTERNALREGEX', RAPIDJSON_SCHEMA_USE_INTERNALREGEX),
('RAPIDJSON_SCHEMA_USE_STDREGEX', RAPIDJSON_SCHEMA_USE_STDREGEX),
],
'depends': ['./rapidjson_exact_version.txt'],
}


if os.path.exists('rapidjson_exact_version.txt'):
with open('rapidjson_exact_version.txt', encoding='utf-8') as f:
extension_options['define_macros'].append(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,22 @@ def test_invalid(schema, json, details):
def test_additional_and_pattern_properties_valid(schema, json):
validate = rj.Validator(schema)
validate(json)


def test_std_regex_used():
schema = rj.dumps({
"type": "object",
"patternProperties": {
"^(?!a).+": {
"type": "string"
}
}
})
validate = rj.Validator(schema)
validate('{"b": "A string"}')
if rj.RAPIDJSON_SCHEMA_USE_STDREGEX:
with pytest.raises(ValueError) as error:
validate('{"b": 1}')
assert error.value.args == ('patternProperties', '#', '#/b')
else:
validate('{"b": 1}')
2 changes: 2 additions & 0 deletions typings/rapidjson/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import typing as t
__rapidjson_exact_version__: str
__rapidjson_version__: str

RAPIDJSON_SCHEMA_USE_STDREGEX: bool


_JSONType = t.Union[
str,
Expand Down