| 1 |
_OldJsonEncoder = None |
| 2 |
_oldOrjsonDumps = None |
| 3 |
_oldMutableMappingSubclasshook = None |
| 4 |
|
| 5 |
|
| 6 |
class MonkeypatchWarning(UserWarning): |
| 7 |
pass |
| 8 |
|
| 9 |
|
| 10 |
def checkCExtension(*, warn, warn_c = False): |
| 11 |
import frozendict as cool |
| 12 |
|
| 13 |
res = cool.c_ext |
| 14 |
|
| 15 |
if warn and res == warn_c: |
| 16 |
if warn_c: # pragma: no cover |
| 17 |
msg = "C Extension version, monkeypatch will be not applied" |
| 18 |
else: |
| 19 |
msg = "Pure Python version, monkeypatch will be not applied" |
| 20 |
|
| 21 |
import warnings |
| 22 |
|
| 23 |
warnings.warn(msg, MonkeypatchWarning) |
| 24 |
|
| 25 |
return res |
| 26 |
|
| 27 |
|
| 28 |
def patchOrUnpatchJson(*, patch, warn = True): # pragma: no cover |
| 29 |
if not checkCExtension(warn = warn): |
| 30 |
return |
| 31 |
|
| 32 |
from importlib import import_module |
| 33 |
self = import_module(__name__) |
| 34 |
import frozendict as cool |
| 35 |
import json |
| 36 |
|
| 37 |
OldJsonEncoder = self._OldJsonEncoder |
| 38 |
|
| 39 |
# noinspection PyUnresolvedReferences, PyProtectedMember |
| 40 |
FrozendictJsonEncoder = cool._getFrozendictJsonEncoder( |
| 41 |
OldJsonEncoder |
| 42 |
) |
| 43 |
|
| 44 |
if patch: |
| 45 |
DefaultJsonEncoder = FrozendictJsonEncoder |
| 46 |
else: |
| 47 |
DefaultJsonEncoder = OldJsonEncoder |
| 48 |
|
| 49 |
if DefaultJsonEncoder is None: |
| 50 |
default_json_encoder = None |
| 51 |
else: |
| 52 |
default_json_encoder = DefaultJsonEncoder( |
| 53 |
skipkeys = False, |
| 54 |
ensure_ascii = True, |
| 55 |
check_circular = True, |
| 56 |
allow_nan = True, |
| 57 |
indent = None, |
| 58 |
separators = None, |
| 59 |
default = None, |
| 60 |
) |
| 61 |
|
| 62 |
if patch: |
| 63 |
if OldJsonEncoder is None: |
| 64 |
self._OldJsonEncoder = json.encoder.JSONEncoder |
| 65 |
else: |
| 66 |
if OldJsonEncoder is None: |
| 67 |
raise ValueError( |
| 68 |
"Old json encoder is None " + |
| 69 |
"(maybe you already unpatched json?)" |
| 70 |
) |
| 71 |
|
| 72 |
self._OldJsonEncoder = None |
| 73 |
|
| 74 |
cool.FrozendictJsonEncoder = FrozendictJsonEncoder |
| 75 |
|
| 76 |
json.JSONEncoder = DefaultJsonEncoder |
| 77 |
json.encoder.JSONEncoder = DefaultJsonEncoder |
| 78 |
json._default_encoder = default_json_encoder |
| 79 |
|
| 80 |
|
| 81 |
def patchOrUnpatchOrjson(*, patch, warn = True): # pragma: no cover |
| 82 |
if not checkCExtension(warn = warn): |
| 83 |
return |
| 84 |
|
| 85 |
from importlib import import_module |
| 86 |
self = import_module(__name__) |
| 87 |
# noinspection PyUnresolvedReferences |
| 88 |
import orjson |
| 89 |
|
| 90 |
if self._oldOrjsonDumps is None: |
| 91 |
if not patch: |
| 92 |
raise ValueError( |
| 93 |
"Old orjson encoder is None " + |
| 94 |
"(maybe you already unpatched orjson?)" |
| 95 |
) |
| 96 |
|
| 97 |
oldOrjsonDumps = orjson.dumps |
| 98 |
else: |
| 99 |
oldOrjsonDumps = self._oldOrjsonDumps |
| 100 |
|
| 101 |
if patch: |
| 102 |
from frozendict import frozendict |
| 103 |
|
| 104 |
def frozendictOrjsonDumps(obj, *args, **kwargs): |
| 105 |
if isinstance(obj, frozendict): |
| 106 |
obj = dict(obj) |
| 107 |
|
| 108 |
return oldOrjsonDumps(obj, *args, **kwargs) |
| 109 |
|
| 110 |
defaultOrjsonDumps = frozendictOrjsonDumps |
| 111 |
newOldOrjsonDumps = oldOrjsonDumps |
| 112 |
else: |
| 113 |
defaultOrjsonDumps = oldOrjsonDumps |
| 114 |
newOldOrjsonDumps = None |
| 115 |
|
| 116 |
self._oldOrjsonDumps = newOldOrjsonDumps |
| 117 |
orjson.dumps = defaultOrjsonDumps |
| 118 |
orjson.orjson.dumps = defaultOrjsonDumps |
| 119 |
|
| 120 |
|
| 121 |
def patchOrUnpatchMutableMappingSubclasshook( |
| 122 |
*, |
| 123 |
patch, |
| 124 |
warn = True |
| 125 |
): # pragma: no cover |
| 126 |
warn_c = True |
| 127 |
|
| 128 |
if checkCExtension(warn = warn, warn_c = warn_c): |
| 129 |
return |
| 130 |
|
| 131 |
from importlib import import_module |
| 132 |
self = import_module(__name__) |
| 133 |
from collections.abc import MutableMapping |
| 134 |
from frozendict import frozendict |
| 135 |
|
| 136 |
if self._oldMutableMappingSubclasshook is None: |
| 137 |
if not patch: |
| 138 |
raise ValueError( |
| 139 |
"Old MutableMapping subclasshook is None " + |
| 140 |
"(maybe you already unpatched MutableMapping?)" |
| 141 |
) |
| 142 |
|
| 143 |
oldMutableMappingSubclasshook = MutableMapping.__subclasshook__ |
| 144 |
else: |
| 145 |
oldMutableMappingSubclasshook = self._oldMutableMappingSubclasshook |
| 146 |
|
| 147 |
if patch: |
| 148 |
# noinspection PyDecorator |
| 149 |
@classmethod |
| 150 |
def frozendictMutableMappingSubclasshook( |
| 151 |
klass, |
| 152 |
subclass, |
| 153 |
*args, |
| 154 |
**kwargs |
| 155 |
): |
| 156 |
if klass == MutableMapping: |
| 157 |
if issubclass(subclass, frozendict): |
| 158 |
return False |
| 159 |
|
| 160 |
return oldMutableMappingSubclasshook( |
| 161 |
subclass, |
| 162 |
*args, |
| 163 |
**kwargs |
| 164 |
) |
| 165 |
|
| 166 |
return NotImplemented |
| 167 |
|
| 168 |
defaultMutableMappingSubclasshook = frozendictMutableMappingSubclasshook |
| 169 |
newOldMutableMappingSubclasshook = oldMutableMappingSubclasshook |
| 170 |
else: |
| 171 |
defaultMutableMappingSubclasshook = oldMutableMappingSubclasshook |
| 172 |
newOldMutableMappingSubclasshook = None |
| 173 |
|
| 174 |
self._oldMutableMappingSubclasshook = newOldMutableMappingSubclasshook |
| 175 |
MutableMapping.__subclasshook__ = defaultMutableMappingSubclasshook |
| 176 |
|
| 177 |
try: |
| 178 |
# noinspection PyUnresolvedReferences, PyProtectedMember |
| 179 |
MutableMapping._abc_caches_clear() |
| 180 |
except AttributeError: |
| 181 |
# noinspection PyUnresolvedReferences, PyProtectedMember |
| 182 |
MutableMapping._abc_cache.discard(frozendict) |
| 183 |
# noinspection PyUnresolvedReferences, PyProtectedMember |
| 184 |
MutableMapping._abc_negative_cache.discard(frozendict) |
| 185 |
|
| 186 |
|
| 187 |
def patchOrUnpatchAll(*, patch, warn = True, raise_orjson = False): |
| 188 |
patchOrUnpatchJson(patch = patch, warn = warn) |
| 189 |
|
| 190 |
try: |
| 191 |
import orjson |
| 192 |
except ImportError: # pragma: no cover |
| 193 |
if raise_orjson: |
| 194 |
raise |
| 195 |
else: # pragma: no cover |
| 196 |
patchOrUnpatchOrjson(patch = patch, warn = warn) |
| 197 |
|
| 198 |
patchOrUnpatchMutableMappingSubclasshook(patch = patch, warn = warn) |
| 199 |
|
| 200 |
|
| 201 |
__all__ = ( |
| 202 |
patchOrUnpatchJson.__name__, |
| 203 |
patchOrUnpatchOrjson.__name__, |
| 204 |
patchOrUnpatchMutableMappingSubclasshook.__name__, |
| 205 |
patchOrUnpatchAll.__name__, |
| 206 |
MonkeypatchWarning.__name__, |
| 207 |
) |