Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/fpickle_setup.py
8814 views
1
"""
2
The purpose of this file is to allow instancemethods to be pickable.
3
This is needed in setup.py only and not installed anywhere, thus is not
4
a library file. It solves the issue from #11874.
5
"""
6
7
import types, copy_reg, copy
8
9
10
# The following four methods are taken from twisted.persisted.styles
11
# into sage.misc.fpickle, and then here. It was needed due to
12
# chicken vs egg issue, as we cannot use sage.misc.fpickle in
13
# setup.py of sage-***.spkg
14
def pickleMethod(method):
15
'support function for copy_reg to pickle method refs'
16
return unpickleMethod, (method.im_func.__name__,
17
method.im_self,
18
method.im_class)
19
20
def unpickleMethod(im_name,
21
im_self,
22
im_class):
23
'support function for copy_reg to unpickle method refs'
24
try:
25
unbound = getattr(im_class,im_name)
26
if im_self is None:
27
return unbound
28
bound=types.MethodType(unbound.im_func,
29
im_self)
30
return bound
31
except AttributeError:
32
# assert im_self is not None,"No recourse: no instance to guess from."
33
# Attempt a common fix before bailing -- if classes have
34
# changed around since we pickled this method, we may still be
35
# able to get it by looking on the instance's current class.
36
unbound = getattr(im_self.__class__,im_name)
37
if im_self is None:
38
return unbound
39
bound=types.MethodType(unbound.im_func,
40
im_self)
41
return bound
42
43
copy_reg.pickle(types.MethodType,
44
pickleMethod,
45
unpickleMethod)
46
47
oldModules = {}
48
49
def pickleModule(module):
50
'support function for copy_reg to pickle module refs'
51
return unpickleModule, (module.__name__,)
52
53
def unpickleModule(name):
54
'support function for copy_reg to unpickle module refs'
55
if oldModules.has_key(name):
56
name = oldModules[name]
57
return __import__(name,{},{},'x')
58
59
copy_reg.pickle(types.ModuleType,
60
pickleModule,
61
unpickleModule)
62
63