-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassbasedmodule.py
More file actions
31 lines (24 loc) · 790 Bytes
/
classbasedmodule.py
File metadata and controls
31 lines (24 loc) · 790 Bytes
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
'''
Class based module.
Allow a module to be overtaken by a class.
'''
from weakref import ref
import sys
__all__ = ['class_as_module']
_uniq = 0
_references = {}
def class_as_module(cls):
global _uniq
# We need to make sure that there remains a reference to the original
# module, because otherwise Python is more than happy to clean up the
# contents of the module by setting all entries to `None`.
count = _uniq
def callback():
del reference[_count]
_uniq += 1
inst = cls()
_references[count] = (ref(inst, callback), sys.modules[cls.__module__])
# Now that we have made sure that a reference to the old module remains, we
# can finally overwrite the value in `sys.modules`.
sys.modules[cls.__module__] = inst
return cls