Skip to content

Commit 23f4169

Browse files
committed
Reconstruct regexes in generated code
Previously, regexes where being pickled in generated code, which makes it more difficult to understand and debug. To improve this situation, this change proposes reconstructing the call to the `re.compile` function.
1 parent 1e21491 commit 23f4169

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

fastjsonschema/generator.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from collections import OrderedDict
22
import re
3-
import pickle
43

54
from .exceptions import JsonSchemaValueException, JsonSchemaDefinitionException
65
from .indent import indent
@@ -101,12 +100,16 @@ def global_state_code(self):
101100
'',
102101
'',
103102
])
103+
regex_patterns = (
104+
repr(k) + ": " + _repr_regex(v)
105+
for k, v in self._compile_regexps.items()
106+
)
104107
return '\n'.join(self._extra_imports_lines + [
105-
'import re, pickle',
108+
'import re',
106109
'from fastjsonschema import JsonSchemaValueException',
107110
'',
108111
'',
109-
'REGEX_PATTERNS = pickle.loads(' + str(pickle.dumps(self._compile_regexps)) + ')',
112+
'REGEX_PATTERNS = {\n ' + ",\n ".join(regex_patterns) + "\n}",
110113
'',
111114
])
112115

@@ -293,3 +296,7 @@ def create_variable_is_dict(self):
293296
return
294297
self._variables.add(variable_name)
295298
self.l('{variable}_is_dict = isinstance({variable}, dict)')
299+
300+
301+
def _repr_regex(regex):
302+
return "re.compile({!r}, {!r})".format(regex.pattern, regex.flags)

0 commit comments

Comments
 (0)