|
35 | 35 | SetAttr, LoadStatic, InitStatic, NAMESPACE_MODULE, RaiseStandardError
|
36 | 36 | )
|
37 | 37 | from mypyc.ir.rtypes import (
|
38 |
| - RType, RTuple, RInstance, int_rprimitive, dict_rprimitive, |
| 38 | + RType, RTuple, RInstance, c_int_rprimitive, int_rprimitive, dict_rprimitive, |
39 | 39 | none_rprimitive, is_none_rprimitive, object_rprimitive, is_object_rprimitive,
|
40 | 40 | str_rprimitive, is_tagged, is_list_rprimitive, is_tuple_rprimitive, c_pyssize_t_rprimitive
|
41 | 41 | )
|
|
45 | 45 | from mypyc.primitives.list_ops import to_list, list_pop_last, list_get_item_unsafe_op
|
46 | 46 | from mypyc.primitives.dict_ops import dict_get_item_op, dict_set_item_op
|
47 | 47 | from mypyc.primitives.generic_ops import py_setattr_op, iter_op, next_op
|
48 |
| -from mypyc.primitives.misc_ops import import_op, check_unpack_count_op, get_module_dict_op |
| 48 | +from mypyc.primitives.misc_ops import ( |
| 49 | + import_op, check_unpack_count_op, get_module_dict_op, import_extra_args_op |
| 50 | +) |
49 | 51 | from mypyc.crash import catch_errors
|
50 | 52 | from mypyc.options import CompilerOptions
|
51 | 53 | from mypyc.errors import Errors
|
@@ -286,19 +288,45 @@ def add_to_non_ext_dict(self, non_ext: NonExtClassInfo,
|
286 | 288 | key_unicode = self.load_str(key)
|
287 | 289 | self.call_c(dict_set_item_op, [non_ext.dict, key_unicode, val], line)
|
288 | 290 |
|
| 291 | + def gen_import_from(self, id: str, line: int, imported: List[str]) -> None: |
| 292 | + self.imports[id] = None |
| 293 | + |
| 294 | + globals_dict = self.load_globals_dict() |
| 295 | + null = Integer(0, dict_rprimitive, line) |
| 296 | + names_to_import = self.new_list_op([self.load_str(name) for name in imported], line) |
| 297 | + |
| 298 | + level = Integer(0, c_int_rprimitive, line) |
| 299 | + value = self.call_c( |
| 300 | + import_extra_args_op, |
| 301 | + [self.load_str(id), globals_dict, null, names_to_import, level], |
| 302 | + line, |
| 303 | + ) |
| 304 | + self.add(InitStatic(value, id, namespace=NAMESPACE_MODULE)) |
| 305 | + |
289 | 306 | def gen_import(self, id: str, line: int) -> None:
|
290 | 307 | self.imports[id] = None
|
291 | 308 |
|
292 | 309 | needs_import, out = BasicBlock(), BasicBlock()
|
293 |
| - first_load = self.load_module(id) |
294 |
| - comparison = self.translate_is_op(first_load, self.none_object(), 'is not', line) |
295 |
| - self.add_bool_branch(comparison, out, needs_import) |
| 310 | + self.check_if_module_loaded(id, line, needs_import, out) |
296 | 311 |
|
297 | 312 | self.activate_block(needs_import)
|
298 | 313 | value = self.call_c(import_op, [self.load_str(id)], line)
|
299 | 314 | self.add(InitStatic(value, id, namespace=NAMESPACE_MODULE))
|
300 | 315 | self.goto_and_activate(out)
|
301 | 316 |
|
| 317 | + def check_if_module_loaded(self, id: str, line: int, |
| 318 | + needs_import: BasicBlock, out: BasicBlock) -> None: |
| 319 | + """Generate code that checks if the module `id` has been loaded yet. |
| 320 | +
|
| 321 | + Arguments: |
| 322 | + id: name of module to check if imported |
| 323 | + line: line number that the import occurs on |
| 324 | + needs_import: the BasicBlock that is run if the module has not been loaded yet |
| 325 | + out: the BasicBlock that is run if the module has already been loaded""" |
| 326 | + first_load = self.load_module(id) |
| 327 | + comparison = self.translate_is_op(first_load, self.none_object(), 'is not', line) |
| 328 | + self.add_bool_branch(comparison, out, needs_import) |
| 329 | + |
302 | 330 | def get_module(self, module: str, line: int) -> Value:
|
303 | 331 | # Python 3.7 has a nice 'PyImport_GetModule' function that we can't use :(
|
304 | 332 | mod_dict = self.call_c(get_module_dict_op, [], line)
|
|
0 commit comments