|
53 | 53 | LoadAddress, |
54 | 54 | LoadErrorValue, |
55 | 55 | LoadLiteral, |
| 56 | + LoadStatic, |
56 | 57 | MethodCall, |
57 | 58 | RaiseStandardError, |
58 | 59 | Register, |
|
63 | 64 | ) |
64 | 65 | from mypyc.ir.rtypes import ( |
65 | 66 | RInstance, |
| 67 | + c_pyssize_t_rprimitive, |
66 | 68 | exc_rtuple, |
67 | 69 | is_tagged, |
68 | 70 | none_rprimitive, |
|
100 | 102 | check_stop_op, |
101 | 103 | coro_op, |
102 | 104 | import_from_many_op, |
| 105 | + import_many_op, |
103 | 106 | send_op, |
104 | 107 | type_op, |
105 | 108 | yield_from_except_op, |
@@ -220,32 +223,69 @@ def transform_operator_assignment_stmt(builder: IRBuilder, stmt: OperatorAssignm |
220 | 223 | def transform_import(builder: IRBuilder, node: Import) -> None: |
221 | 224 | if node.is_mypy_only: |
222 | 225 | return |
223 | | - globals = builder.load_globals_dict() |
224 | | - for node_id, as_name in node.ids: |
225 | | - builder.gen_import(node_id, node.line) |
226 | | - |
227 | | - # Update the globals dict with the appropriate module: |
228 | | - # * For 'import foo.bar as baz' we add 'foo.bar' with the name 'baz' |
229 | | - # * For 'import foo.bar' we add 'foo' with the name 'foo' |
230 | | - # Typically we then ignore these entries and access things directly |
231 | | - # via the module static, but we will use the globals version for modules |
232 | | - # that mypy couldn't find, since it doesn't analyze module references |
233 | | - # from those properly. |
234 | | - |
235 | | - # TODO: Don't add local imports to the global namespace |
236 | | - |
237 | | - # Miscompiling imports inside of functions, like below in import from. |
238 | | - if as_name: |
239 | | - name = as_name |
240 | | - base = node_id |
241 | | - else: |
242 | | - base = name = node_id.split(".")[0] |
243 | 226 |
|
244 | | - obj = builder.get_module(base, node.line) |
| 227 | + # Imports (not from imports!) are processed in an odd way so they can be |
| 228 | + # table-driven and compact. Here's how it works: |
| 229 | + # |
| 230 | + # Import nodes are divided in groups (in the prebuild visitor). Each group |
| 231 | + # consists of consecutive Import nodes: |
| 232 | + # |
| 233 | + # import mod <| group #1 |
| 234 | + # import mod2 | |
| 235 | + # |
| 236 | + # def foo() -> None: |
| 237 | + # import mod3 <- group #2 |
| 238 | + # |
| 239 | + # import mod4 <- group #3 |
| 240 | + # |
| 241 | + # Every time we encounter the first import of a group, build IR to call a |
| 242 | + # helper function that will perform all of the group's imports in one go. |
| 243 | + if node not in builder.module_import_groups: |
| 244 | + return |
245 | 245 |
|
246 | | - builder.gen_method_call( |
247 | | - globals, "__setitem__", [builder.load_str(name), obj], result_type=None, line=node.line |
248 | | - ) |
| 246 | + modules = [] |
| 247 | + statics = [] |
| 248 | + # To show the right line number on failure, we have to add the traceback |
| 249 | + # entry within the helper function (which is admittedly ugly). To drive |
| 250 | + # this, we'll need the line number corresponding to each import. |
| 251 | + import_lines = [] |
| 252 | + for import_node in builder.module_import_groups[node]: |
| 253 | + for mod_id, as_name in import_node.ids: |
| 254 | + builder.imports[mod_id] = None |
| 255 | + import_lines.append(Integer(import_node.line, c_pyssize_t_rprimitive)) |
| 256 | + |
| 257 | + module_static = LoadStatic(object_rprimitive, mod_id, namespace=NAMESPACE_MODULE) |
| 258 | + static_ptr = builder.add(LoadAddress(object_pointer_rprimitive, module_static)) |
| 259 | + statics.append(static_ptr) |
| 260 | + # TODO: Don't add local imports to the global namespace |
| 261 | + # Update the globals dict with the appropriate module: |
| 262 | + # * For 'import foo.bar as baz' we add 'foo.bar' with the name 'baz' |
| 263 | + # * For 'import foo.bar' we add 'foo' with the name 'foo' |
| 264 | + # Typically we then ignore these entries and access things directly |
| 265 | + # via the module static, but we will use the globals version for |
| 266 | + # modules that mypy couldn't find, since it doesn't analyze module |
| 267 | + # references from those properly. |
| 268 | + if as_name or "." not in mod_id: |
| 269 | + globals_base = None |
| 270 | + else: |
| 271 | + globals_base = mod_id.split(".")[0] |
| 272 | + modules.append((mod_id, as_name, globals_base)) |
| 273 | + |
| 274 | + static_array_ptr = builder.builder.setup_rarray(object_pointer_rprimitive, statics) |
| 275 | + import_line_ptr = builder.builder.setup_rarray(c_pyssize_t_rprimitive, import_lines) |
| 276 | + function = "<module>" if builder.fn_info.name == "<top level>" else builder.fn_info.name |
| 277 | + builder.call_c( |
| 278 | + import_many_op, |
| 279 | + [ |
| 280 | + builder.add(LoadLiteral(tuple(modules), object_rprimitive)), |
| 281 | + static_array_ptr, |
| 282 | + builder.load_globals_dict(), |
| 283 | + builder.load_str(builder.module_path), |
| 284 | + builder.load_str(function), |
| 285 | + import_line_ptr, |
| 286 | + ], |
| 287 | + NO_TRACEBACK_LINE_NO, |
| 288 | + ) |
249 | 289 |
|
250 | 290 |
|
251 | 291 | def transform_import_from(builder: IRBuilder, node: ImportFrom) -> None: |
|
0 commit comments