| 1 |
""" |
| 2 |
Module objects. |
| 3 |
""" |
| 4 |
|
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
from pypy.interpreter.error import OperationError, oefmt |
| 7 |
from rpython.rlib.objectmodel import we_are_translated, not_rpython |
| 8 |
|
| 9 |
|
| 10 |
class Module(W_Root): |
| 11 |
"""A module.""" |
| 12 |
|
| 13 |
_immutable_fields_ = ["w_dict", "w_userclass?"] |
| 14 |
|
| 15 |
_frozen = False |
| 16 |
w_userclass = None |
| 17 |
|
| 18 |
def __init__(self, space, w_name, w_dict=None): |
| 19 |
self.space = space |
| 20 |
if w_dict is None: |
| 21 |
w_dict = space.newdict(module=True) |
| 22 |
self.w_dict = w_dict |
| 23 |
self.w_name = w_name |
| 24 |
if w_name is not None: |
| 25 |
space.setitem(w_dict, space.new_interned_str('__name__'), w_name) |
| 26 |
self.startup_called = False |
| 27 |
|
| 28 |
def _cleanup_(self): |
| 29 |
"""Called by the annotator on prebuilt Module instances. |
| 30 |
We don't have many such modules, but for the ones that |
| 31 |
show up, remove their __file__ rather than translate it |
| 32 |
statically inside the executable.""" |
| 33 |
try: |
| 34 |
space = self.space |
| 35 |
space.delitem(self.w_dict, space.newtext('__file__')) |
| 36 |
except OperationError: |
| 37 |
pass |
| 38 |
|
| 39 |
@not_rpython |
| 40 |
def install(self): |
| 41 |
"""installs this module into space.builtin_modules""" |
| 42 |
modulename = self.space.text0_w(self.w_name) |
| 43 |
if modulename in self.space.builtin_modules: |
| 44 |
raise ValueError( |
| 45 |
"duplicate interp-level module enabled for the " |
| 46 |
"app-level module %r" % (modulename,)) |
| 47 |
self.space.builtin_modules[modulename] = self |
| 48 |
|
| 49 |
@not_rpython |
| 50 |
def setup_after_space_initialization(self): |
| 51 |
"""to allow built-in modules to do some more setup |
| 52 |
after the space is fully initialized.""" |
| 53 |
|
| 54 |
def init(self, space): |
| 55 |
"""This is called each time the module is imported or reloaded |
| 56 |
""" |
| 57 |
if not self.startup_called: |
| 58 |
if not we_are_translated(): |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if self._frozen: |
| 63 |
return |
| 64 |
self.startup_called = True |
| 65 |
self.startup(space) |
| 66 |
|
| 67 |
def startup(self, space): |
| 68 |
"""This is called at runtime on import to allow the module to |
| 69 |
do initialization when it is imported for the first time. |
| 70 |
""" |
| 71 |
|
| 72 |
def shutdown(self, space): |
| 73 |
"""This is called when the space is shut down, just after |
| 74 |
atexit functions, if the module has been imported. |
| 75 |
""" |
| 76 |
|
| 77 |
def getdict(self, space): |
| 78 |
return self.w_dict |
| 79 |
|
| 80 |
def descr_module__new__(space, w_subtype, __args__): |
| 81 |
module = space.allocate_instance(Module, w_subtype) |
| 82 |
Module.__init__(module, space, None) |
| 83 |
return module |
| 84 |
|
| 85 |
def descr_module__init__(self, w_name, w_doc=None): |
| 86 |
space = self.space |
| 87 |
self.w_name = w_name |
| 88 |
if w_doc is None: |
| 89 |
w_doc = space.w_None |
| 90 |
w_dict = self.w_dict |
| 91 |
space.setitem(w_dict, space.new_interned_str('__name__'), w_name) |
| 92 |
space.setitem(w_dict, space.new_interned_str('__doc__'), w_doc) |
| 93 |
init_extra_module_attrs(space, self) |
| 94 |
|
| 95 |
def descr__reduce__(self, space): |
| 96 |
w_name = space.finditem(self.w_dict, space.newtext('__name__')) |
| 97 |
if (w_name is None or |
| 98 |
not space.isinstance_w(w_name, space.w_text)): |
| 99 |
|
| 100 |
return space.w_None |
| 101 |
w_modules = space.sys.get('modules') |
| 102 |
if space.finditem(w_modules, w_name) is None: |
| 103 |
|
| 104 |
from pypy.interpreter.mixedmodule import MixedModule |
| 105 |
w_mod = space.getbuiltinmodule('_pickle_support') |
| 106 |
mod = space.interp_w(MixedModule, w_mod) |
| 107 |
new_inst = mod.get('module_new') |
| 108 |
return space.newtuple2(new_inst, |
| 109 |
space.newtuple2(w_name, |
| 110 |
self.getdict(space)), |
| 111 |
) |
| 112 |
|
| 113 |
w_import = space.builtin.get('__import__') |
| 114 |
tup_return = [ |
| 115 |
w_import, |
| 116 |
space.newtuple([ |
| 117 |
w_name, |
| 118 |
space.w_None, |
| 119 |
space.w_None, |
| 120 |
space.newtuple([space.newtext('')]) |
| 121 |
]) |
| 122 |
] |
| 123 |
|
| 124 |
return space.newtuple(tup_return) |
| 125 |
|
| 126 |
def descr_module__repr__(self, space): |
| 127 |
w_importlib = space.getbuiltinmodule('_frozen_importlib') |
| 128 |
return space.call_method(w_importlib, "_module_repr", self) |
| 129 |
|
| 130 |
def descr_getattribute(self, space, w_attr): |
| 131 |
from pypy.objspace.descroperation import object_getattribute |
| 132 |
from pypy.module.imp.importing import (is_spec_initializing, |
| 133 |
is_spec_uninitialized_submodule) |
| 134 |
try: |
| 135 |
return space.call_function(object_getattribute(space), self, w_attr) |
| 136 |
except OperationError as e: |
| 137 |
if not e.match(space, space.w_AttributeError): |
| 138 |
raise |
| 139 |
w_dict = self.w_dict |
| 140 |
w_getattr = space.finditem(w_dict, space.newtext('__getattr__')) |
| 141 |
if w_getattr is not None: |
| 142 |
return space.call_function(w_getattr, w_attr) |
| 143 |
w_name = space.finditem(self.w_dict, space.newtext('__name__')) |
| 144 |
w_spec = space.finditem(self.w_dict, space.newtext('__spec__')) |
| 145 |
if w_name is None: |
| 146 |
raise oefmt(space.w_AttributeError, |
| 147 |
"module has no attribute %R", w_attr) |
| 148 |
elif w_spec is not None and is_spec_initializing(space, w_spec): |
| 149 |
raise oefmt(space.w_AttributeError, |
| 150 |
"partially initialized " |
| 151 |
"module %R has no attribute %R " |
| 152 |
"(most likely due to a circular import)", |
| 153 |
w_name, w_attr |
| 154 |
) |
| 155 |
elif w_spec is not None and is_spec_uninitialized_submodule(space, w_spec, w_attr): |
| 156 |
raise oefmt(space.w_AttributeError, |
| 157 |
"cannot access submodule %R of module %R " |
| 158 |
"(most likely due to a circular import)", |
| 159 |
w_attr, w_name) |
| 160 |
else: |
| 161 |
raise oefmt(space.w_AttributeError, |
| 162 |
"module %R has no attribute %R", w_name, w_attr) |
| 163 |
|
| 164 |
def descr_module__dir__(self, space): |
| 165 |
w_dict = space.getattr(self, space.newtext('__dict__')) |
| 166 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 167 |
raise oefmt(space.w_TypeError, "%N.__dict__ is not a dictionary", |
| 168 |
self) |
| 169 |
w_dir = space.finditem(w_dict, space.newtext('__dir__')) |
| 170 |
if w_dir is not None: |
| 171 |
return space.call_function(w_dir) |
| 172 |
return space.call_function(space.w_list, w_dict) |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
def descr_get_annotations(self, space): |
| 177 |
w_dict = self.w_dict |
| 178 |
w_ann = space.finditem(w_dict, space.newtext('__annotations__')) |
| 179 |
if w_ann is None: |
| 180 |
w_ann = space.newdict() |
| 181 |
space.setitem(w_dict, space.newtext('__annotations__'), w_ann) |
| 182 |
return w_ann |
| 183 |
|
| 184 |
def descr_set_annotations(self, space, w_ann): |
| 185 |
space.setitem(self.w_dict, space.newtext('__annotations__'), w_ann) |
| 186 |
|
| 187 |
def descr_del_annotations(self, space): |
| 188 |
w_dict = self.w_dict |
| 189 |
w_ann = space.finditem(w_dict, space.newtext('__annotations__')) |
| 190 |
if w_ann is None: |
| 191 |
raise oefmt(space.w_AttributeError, "__annotations__") |
| 192 |
space.delitem(w_dict, space.newtext('__annotations__')) |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
def getclass(self, space): |
| 204 |
if self.w_userclass is None: |
| 205 |
return W_Root.getclass(self, space) |
| 206 |
return self.w_userclass |
| 207 |
|
| 208 |
def setclass(self, space, w_cls): |
| 209 |
self.w_userclass = w_cls |
| 210 |
|
| 211 |
def user_setup(self, space, w_subtype): |
| 212 |
self.w_userclass = w_subtype |
| 213 |
|
| 214 |
|
| 215 |
def init_extra_module_attrs(space, w_mod): |
| 216 |
w_dict = w_mod.getdict(space) |
| 217 |
if w_dict is None: |
| 218 |
return |
| 219 |
for extra in ['__package__', '__loader__', '__spec__']: |
| 220 |
w_attr = space.new_interned_str(extra) |
| 221 |
space.call_method(w_dict, 'setdefault', w_attr, space.w_None) |