Skip to content

Commit

Permalink
Merge pull request #135 from abravalheri/replace-pickle-in-generated-…
Browse files Browse the repository at this point in the history
…code

Reconstruct regexes in generated code instead of pickling
  • Loading branch information
horejsek authored Dec 28, 2021
2 parents e4b9c46 + f200032 commit f3c8187
Show file tree
Hide file tree
Showing 9 changed files with 93,835 additions and 3 deletions.
22 changes: 19 additions & 3 deletions fastjsonschema/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import OrderedDict
import re
import pickle

from .exceptions import JsonSchemaValueException, JsonSchemaDefinitionException
from .indent import indent
Expand Down Expand Up @@ -103,11 +102,11 @@ def global_state_code(self):
'',
])
return '\n'.join(self._extra_imports_lines + [
'import re, pickle',
'import re',
'from fastjsonschema import JsonSchemaValueException',
'',
'',
'REGEX_PATTERNS = pickle.loads(' + str(pickle.dumps(self._compile_regexps)) + ')',
'REGEX_PATTERNS = ' + serialize_regexes(self._compile_regexps),
'',
])

Expand Down Expand Up @@ -294,3 +293,20 @@ def create_variable_is_dict(self):
return
self._variables.add(variable_name)
self.l('{variable}_is_dict = isinstance({variable}, dict)')


def serialize_regexes(patterns_dict):
# Unfortunately using `pprint.pformat` is causing errors
# specially with big regexes
regex_patterns = (
repr(k) + ": " + repr_regex(v)
for k, v in patterns_dict.items()
)
return '{\n ' + ",\n ".join(regex_patterns) + "\n}"


def repr_regex(regex):
all_flags = ("A", "I", "DEBUG", "L", "M", "S", "X")
flags = " | ".join(f"re.{f}" for f in all_flags if regex.flags & getattr(re, f))
flags = ", " + flags if flags else ""
return "re.compile({!r}{})".format(regex.pattern, flags)
1 change: 1 addition & 0 deletions tests/examples/issue-109-regex-only/invalid.error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data must match pattern ^[ \r\n\t\S]+$
1 change: 1 addition & 0 deletions tests/examples/issue-109-regex-only/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
5 changes: 5 additions & 0 deletions tests/examples/issue-109-regex-only/string.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"pattern": "^[ \\r\\n\\t\\S]+$"
}
1 change: 1 addition & 0 deletions tests/examples/issue-109-regex-only/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"42"
Loading

0 comments on commit f3c8187

Please sign in to comment.