|
1 | 1 | from collections import OrderedDict
|
2 | 2 | import re
|
3 |
| -import pickle |
4 | 3 |
|
5 | 4 | from .exceptions import JsonSchemaValueException, JsonSchemaDefinitionException
|
6 | 5 | from .indent import indent
|
@@ -103,11 +102,11 @@ def global_state_code(self):
|
103 | 102 | '',
|
104 | 103 | ])
|
105 | 104 | return '\n'.join(self._extra_imports_lines + [
|
106 |
| - 'import re, pickle', |
| 105 | + 'import re', |
107 | 106 | 'from fastjsonschema import JsonSchemaValueException',
|
108 | 107 | '',
|
109 | 108 | '',
|
110 |
| - 'REGEX_PATTERNS = pickle.loads(' + str(pickle.dumps(self._compile_regexps)) + ')', |
| 109 | + 'REGEX_PATTERNS = ' + serialize_regexes(self._compile_regexps), |
111 | 110 | '',
|
112 | 111 | ])
|
113 | 112 |
|
@@ -294,3 +293,20 @@ def create_variable_is_dict(self):
|
294 | 293 | return
|
295 | 294 | self._variables.add(variable_name)
|
296 | 295 | self.l('{variable}_is_dict = isinstance({variable}, dict)')
|
| 296 | + |
| 297 | + |
| 298 | +def serialize_regexes(patterns_dict): |
| 299 | + # Unfortunately using `pprint.pformat` is causing errors |
| 300 | + # specially with big regexes |
| 301 | + regex_patterns = ( |
| 302 | + repr(k) + ": " + repr_regex(v) |
| 303 | + for k, v in patterns_dict.items() |
| 304 | + ) |
| 305 | + return '{\n ' + ",\n ".join(regex_patterns) + "\n}" |
| 306 | + |
| 307 | + |
| 308 | +def repr_regex(regex): |
| 309 | + all_flags = ("A", "I", "DEBUG", "L", "M", "S", "X") |
| 310 | + flags = " | ".join(f"re.{f}" for f in all_flags if regex.flags & getattr(re, f)) |
| 311 | + flags = ", " + flags if flags else "" |
| 312 | + return "re.compile({!r}{})".format(regex.pattern, flags) |
0 commit comments