forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkeypatch_copyreg.py
More file actions
63 lines (37 loc) · 1.37 KB
/
monkeypatch_copyreg.py
File metadata and controls
63 lines (37 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''This module monkey-patches the pickling dispatch table using `copyreg`.'''
# todo: alters global state, yuck! Maybe check before if it's already set to
# something?
import copyreg
import types
from python_toolbox import import_tools
###############################################################################
def reduce_method(method):
'''Reducer for methods.'''
return (
getattr,
(
method.__self__ or method.__self__.__class__,
# `im_self` for bound methods, `im_class` for unbound methods.
method.__func__.__name__
)
)
copyreg.pickle(types.MethodType, reduce_method)
###############################################################################
def reduce_module(module):
'''Reducer for modules.'''
return (import_tools.normal_import, (module.__name__,))
copyreg.pickle(types.ModuleType, reduce_module)
###############################################################################
def _get_ellipsis():
'''Get the `Ellipsis`.'''
return Ellipsis
def reduce_ellipsis(ellipsis):
'''Reducer for `Ellipsis`.'''
return (
_get_ellipsis,
()
)
copyreg.pickle(type(Ellipsis), reduce_ellipsis)
###############################################################################