| 1 |
from pypy.interpreter.module import Module, init_extra_module_attrs |
| 2 |
from pypy.interpreter.function import Function, BuiltinFunction |
| 3 |
from pypy.interpreter import gateway |
| 4 |
from pypy.interpreter.error import OperationError |
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
|
| 7 |
from rpython.rlib.objectmodel import not_rpython |
| 8 |
|
| 9 |
import sys |
| 10 |
|
| 11 |
class MixedModule(Module): |
| 12 |
applevel_name = None |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
w_initialdict = None |
| 18 |
lazy = False |
| 19 |
submodule_name = None |
| 20 |
|
| 21 |
@not_rpython |
| 22 |
def __init__(self, space, w_name): |
| 23 |
Module.__init__(self, space, w_name) |
| 24 |
init_extra_module_attrs(space, self) |
| 25 |
self.lazy = True |
| 26 |
self.lazy_initial_values_w = {} |
| 27 |
self.__class__.buildloaders() |
| 28 |
self.loaders = self.loaders.copy() |
| 29 |
self.submodules_w = [] |
| 30 |
|
| 31 |
@not_rpython |
| 32 |
def install(self): |
| 33 |
"""Install this module, and its submodules into |
| 34 |
space.builtin_modules""" |
| 35 |
Module.install(self) |
| 36 |
if hasattr(self, "submodules"): |
| 37 |
space = self.space |
| 38 |
pkgname = space.text_w(self.w_name) |
| 39 |
for sub_name, module_cls in self.submodules.iteritems(): |
| 40 |
if module_cls.submodule_name is None: |
| 41 |
module_cls.submodule_name = sub_name |
| 42 |
else: |
| 43 |
assert module_cls.submodule_name == sub_name |
| 44 |
name = "%s.%s" % (pkgname, sub_name) |
| 45 |
module_cls.applevel_name = name |
| 46 |
w_name = space.newtext(name) |
| 47 |
m = module_cls(space, w_name) |
| 48 |
m.install() |
| 49 |
self.submodules_w.append(m) |
| 50 |
|
| 51 |
def init(self, space): |
| 52 |
"""This is called each time the module is imported or reloaded |
| 53 |
""" |
| 54 |
if self.w_initialdict is not None: |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
space.call_method(self.w_dict, 'update', self.w_initialdict) |
| 59 |
|
| 60 |
for w_submodule in self.submodules_w: |
| 61 |
name = space.text0_w(w_submodule.w_name) |
| 62 |
space.setitem(self.w_dict, space.newtext(name.split(".")[-1]), w_submodule) |
| 63 |
space.getbuiltinmodule(name) |
| 64 |
|
| 65 |
if self.w_initialdict is None: |
| 66 |
Module.init(self, space) |
| 67 |
if not self.lazy and self.w_initialdict is None: |
| 68 |
self.save_module_content_for_future_reload() |
| 69 |
|
| 70 |
def save_module_content_for_future_reload(self): |
| 71 |
|
| 72 |
|
| 73 |
w_dict = self.getdict(self.space) |
| 74 |
self.w_initialdict = self.space.call_method(w_dict, 'copy') |
| 75 |
|
| 76 |
@classmethod |
| 77 |
@not_rpython |
| 78 |
def get_applevel_name(cls): |
| 79 |
if cls.applevel_name is not None: |
| 80 |
return cls.applevel_name |
| 81 |
else: |
| 82 |
return cls.__module__.split('.')[-2] |
| 83 |
|
| 84 |
def get(self, name): |
| 85 |
space = self.space |
| 86 |
w_value = self.getdictvalue(space, name) |
| 87 |
if w_value is None: |
| 88 |
raise OperationError(space.w_AttributeError, space.newtext(name)) |
| 89 |
return w_value |
| 90 |
|
| 91 |
def call(self, name, *args_w): |
| 92 |
w_builtin = self.get(name) |
| 93 |
return self.space.call_function(w_builtin, *args_w) |
| 94 |
|
| 95 |
def getdictvalue(self, space, name): |
| 96 |
w_value = space.finditem_str(self.w_dict, name) |
| 97 |
if self.lazy and w_value is None: |
| 98 |
return self._load_lazily(space, name) |
| 99 |
return w_value |
| 100 |
|
| 101 |
def setdictvalue(self, space, attr, w_value): |
| 102 |
if self.lazy and attr not in self.lazy_initial_values_w: |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
w_initial_value = self._load_lazily(space, attr) |
| 108 |
self.lazy_initial_values_w[attr] = w_initial_value |
| 109 |
space.setitem_str(self.w_dict, attr, w_value) |
| 110 |
return True |
| 111 |
|
| 112 |
def _load_lazily(self, space, name): |
| 113 |
w_name = space.new_interned_str(name) |
| 114 |
try: |
| 115 |
loader = self.loaders[name] |
| 116 |
except KeyError: |
| 117 |
return None |
| 118 |
else: |
| 119 |
w_value = loader(space) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func = w_value |
| 127 |
if (isinstance(func, Function) and |
| 128 |
type(func) is not BuiltinFunction): |
| 129 |
try: |
| 130 |
bltin = func._builtinversion_ |
| 131 |
except AttributeError: |
| 132 |
bltin = BuiltinFunction(func) |
| 133 |
bltin.w_module = self.w_name |
| 134 |
bltin.w_moduleobj = self |
| 135 |
func._builtinversion_ = bltin |
| 136 |
bltin.name = name |
| 137 |
bltin.qualname = bltin.name |
| 138 |
w_value = bltin |
| 139 |
space.setitem(self.w_dict, w_name, w_value) |
| 140 |
return w_value |
| 141 |
|
| 142 |
def getdict(self, space): |
| 143 |
if self.lazy: |
| 144 |
self._force_lazy_dict_now() |
| 145 |
return self.w_dict |
| 146 |
|
| 147 |
def _force_lazy_dict_now(self): |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
space = self.space |
| 153 |
for name in self.loaders: |
| 154 |
w_value = self.get(name) |
| 155 |
space.setitem(self.w_dict, space.new_interned_str(name), w_value) |
| 156 |
self.lazy = False |
| 157 |
self.save_module_content_for_future_reload() |
| 158 |
for key, w_initial_value in self.lazy_initial_values_w.items(): |
| 159 |
w_key = space.new_interned_str(key) |
| 160 |
if w_initial_value is not None: |
| 161 |
space.setitem(self.w_initialdict, w_key, w_initial_value) |
| 162 |
else: |
| 163 |
if space.finditem(self.w_initialdict, w_key) is not None: |
| 164 |
space.delitem(self.w_initialdict, w_key) |
| 165 |
del self.lazy_initial_values_w |
| 166 |
|
| 167 |
def reset_lazy_initial_values(self): |
| 168 |
|
| 169 |
if self.lazy: |
| 170 |
self.lazy_initial_values_w = {} |
| 171 |
|
| 172 |
def _cleanup_(self): |
| 173 |
self.getdict(self.space) |
| 174 |
self.w_initialdict = None |
| 175 |
self.startup_called = False |
| 176 |
self._frozen = True |
| 177 |
|
| 178 |
@classmethod |
| 179 |
@not_rpython |
| 180 |
def buildloaders(cls): |
| 181 |
if not hasattr(cls, 'loaders'): |
| 182 |
|
| 183 |
|
| 184 |
cls.loaders = loaders = {} |
| 185 |
pkgroot = cls.__module__.rsplit('.', 1)[0] |
| 186 |
appname = cls.get_applevel_name() |
| 187 |
for name, spec in cls.interpleveldefs.items(): |
| 188 |
loaders[name] = getinterpevalloader(pkgroot, spec) |
| 189 |
for name, spec in cls.appleveldefs.items(): |
| 190 |
loaders[name] = getappfileloader(pkgroot, appname, spec) |
| 191 |
assert '__file__' not in loaders |
| 192 |
if '__doc__' not in loaders: |
| 193 |
loaders['__doc__'] = cls.get__doc__ |
| 194 |
|
| 195 |
def extra_interpdef(self, name, spec): |
| 196 |
cls = self.__class__ |
| 197 |
pkgroot = cls.__module__.rsplit('.', 1)[0] |
| 198 |
loader = getinterpevalloader(pkgroot, spec) |
| 199 |
space = self.space |
| 200 |
w_obj = loader(space) |
| 201 |
space.setattr(self, space.newtext(name), w_obj) |
| 202 |
|
| 203 |
@classmethod |
| 204 |
def get__doc__(cls, space): |
| 205 |
return space.newtext_or_none(cls.__doc__) |
| 206 |
|
| 207 |
def setdictvalue_dont_introduce_cell(self, name, w_value): |
| 208 |
""" unofficial interface in MixedModules to override an existing value |
| 209 |
in the module but without introducing a cell (in the sense of |
| 210 |
celldict.py) for it. Should be used sparingly, since it will trigger a |
| 211 |
JIT recompile of all code that uses this module.""" |
| 212 |
from pypy.objspace.std.celldict import remove_cell |
| 213 |
self.setdictvalue(self.space, name, w_value) |
| 214 |
remove_cell(self.w_dict, self.space, name) |
| 215 |
|
| 216 |
|
| 217 |
@not_rpython |
| 218 |
def getinterpevalloader(pkgroot, spec): |
| 219 |
def ifileloader(space): |
| 220 |
d = {'space': space} |
| 221 |
|
| 222 |
while 1: |
| 223 |
try: |
| 224 |
value = eval(spec, d) |
| 225 |
except NameError as ex: |
| 226 |
name = ex.args[0].split("'")[1] |
| 227 |
if name in d: |
| 228 |
raise |
| 229 |
try: |
| 230 |
d[name] = __import__(pkgroot+'.'+name, None, None, [name]) |
| 231 |
except ImportError: |
| 232 |
etype, evalue, etb = sys.exc_info() |
| 233 |
try: |
| 234 |
d[name] = __import__(name, None, None, [name]) |
| 235 |
except ImportError: |
| 236 |
|
| 237 |
|
| 238 |
raise etype, evalue, etb |
| 239 |
else: |
| 240 |
|
| 241 |
if hasattr(value, 'func_code'): |
| 242 |
return gateway.interp2app(value).get_function(space) |
| 243 |
|
| 244 |
try: |
| 245 |
is_type = issubclass(value, W_Root) |
| 246 |
except TypeError: |
| 247 |
is_type = False |
| 248 |
if is_type: |
| 249 |
return space.gettypefor(value) |
| 250 |
|
| 251 |
assert isinstance(value, W_Root), ( |
| 252 |
"interpleveldef %s.%s must return a wrapped object " |
| 253 |
"(got %r instead)" % (pkgroot, spec, value)) |
| 254 |
return value |
| 255 |
return ifileloader |
| 256 |
|
| 257 |
applevelcache = {} |
| 258 |
@not_rpython |
| 259 |
def getappfileloader(pkgroot, appname, spec): |
| 260 |
|
| 261 |
|
| 262 |
modname, attrname = spec.split('.') |
| 263 |
impbase = pkgroot + '.' + modname |
| 264 |
try: |
| 265 |
app = applevelcache[impbase] |
| 266 |
except KeyError: |
| 267 |
import imp |
| 268 |
pkg = __import__(pkgroot, None, None, ['__doc__']) |
| 269 |
file, fn, (suffix, mode, typ) = imp.find_module(modname, pkg.__path__) |
| 270 |
assert typ == imp.PY_SOURCE |
| 271 |
source = file.read() |
| 272 |
file.close() |
| 273 |
if fn.endswith('.pyc'): |
| 274 |
fn = fn[:-1] |
| 275 |
app = gateway.applevel(source, filename=fn, modname=appname) |
| 276 |
applevelcache[impbase] = app |
| 277 |
|
| 278 |
def afileloader(space): |
| 279 |
return app.wget(space, attrname) |
| 280 |
return afileloader |