Skip to content

Commit f908715

Browse files
committed
Simplify data format
1 parent 07d514c commit f908715

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

mypyc/irbuild/statement.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,20 @@ def transform_import(builder: IRBuilder, node: Import) -> None:
257257
module_static = LoadStatic(object_rprimitive, mod_id, namespace=NAMESPACE_MODULE)
258258
static_ptr = builder.add(LoadAddress(object_pointer_rprimitive, module_static))
259259
statics.append(static_ptr)
260-
# TODO: Don't add local imports to the global namespace
261260
# Update the globals dict with the appropriate module:
262261
# * For 'import foo.bar as baz' we add 'foo.bar' with the name 'baz'
263262
# * For 'import foo.bar' we add 'foo' with the name 'foo'
264263
# Typically we then ignore these entries and access things directly
265264
# via the module static, but we will use the globals version for
266265
# modules that mypy couldn't find, since it doesn't analyze module
267266
# references from those properly.
268-
if as_name or "." not in mod_id:
269-
globals_base = None
267+
# TODO: Don't add local imports to the global namespace
268+
if as_name:
269+
globals_id = mod_id
270+
globals_name = as_name
270271
else:
271-
globals_base = mod_id.split(".")[0]
272-
modules.append((mod_id, as_name, globals_base))
272+
globals_id = globals_name = mod_id.split(".")[0]
273+
modules.append((mod_id, globals_id, globals_name))
273274

274275
static_array_ptr = builder.builder.setup_rarray(object_pointer_rprimitive, statics)
275276
import_line_ptr = builder.builder.setup_rarray(c_pyssize_t_rprimitive, import_lines)
@@ -304,9 +305,9 @@ def transform_import_from(builder: IRBuilder, node: ImportFrom) -> None:
304305
builder.imports[id] = None
305306

306307
names = [name for name, _ in node.names]
307-
as_names = [as_name for _, as_name in node.names]
308+
as_names = [as_name or name for name, as_name in node.names]
308309
names_literal = builder.add(LoadLiteral(tuple(names), object_rprimitive))
309-
if all(n is None for n in as_names):
310+
if as_names == names:
310311
# Reuse names tuple to reduce verbosity.
311312
as_names_literal = names_literal
312313
else:

mypyc/lib-rt/misc_ops.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,8 @@ CPy_Super(PyObject *builtins, PyObject *self) {
669669
return result;
670670
}
671671

672-
static bool import_single(PyObject *mod_id,
673-
PyObject *as_name,
674-
PyObject **mod_static,
675-
PyObject *globals_base,
676-
PyObject *globals) {
672+
static bool import_single(PyObject *mod_id, PyObject **mod_static,
673+
PyObject *globals_id, PyObject *globals_name, PyObject *globals) {
677674
if (*mod_static == Py_None) {
678675
CPyModule *mod = PyImport_Import(mod_id);
679676
if (mod == NULL) {
@@ -682,16 +679,6 @@ static bool import_single(PyObject *mod_id,
682679
*mod_static = mod;
683680
}
684681

685-
if (as_name == Py_None) {
686-
as_name = mod_id;
687-
}
688-
PyObject *globals_id, *globals_name;
689-
if (globals_base == Py_None) {
690-
globals_id = mod_id;
691-
globals_name = as_name;
692-
} else {
693-
globals_id = globals_name = globals_base;
694-
}
695682
PyObject *mod_dict = PyImport_GetModuleDict();
696683
CPyModule *globals_mod = CPyDict_GetItem(mod_dict, globals_id);
697684
if (globals_mod == NULL) {
@@ -712,10 +699,10 @@ bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *gl
712699
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(modules); i++) {
713700
PyObject *module = PyTuple_GET_ITEM(modules, i);
714701
PyObject *mod_id = PyTuple_GET_ITEM(module, 0);
715-
PyObject *as_name = PyTuple_GET_ITEM(module, 1);
716-
PyObject *globals_base = PyTuple_GET_ITEM(module, 2);
702+
PyObject *globals_id = PyTuple_GET_ITEM(module, 1);
703+
PyObject *globals_name = PyTuple_GET_ITEM(module, 2);
717704

718-
if (!import_single(mod_id, as_name, statics[i], globals_base, globals)) {
705+
if (!import_single(mod_id, statics[i], globals_id, globals_name, globals)) {
719706
const char *path = PyUnicode_AsUTF8(tb_path);
720707
if (path == NULL) {
721708
path = "<unable to display>";
@@ -774,9 +761,6 @@ PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *
774761
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(names); i++) {
775762
PyObject *name = PyTuple_GET_ITEM(names, i);
776763
PyObject *as_name = PyTuple_GET_ITEM(as_names, i);
777-
if (as_name == Py_None) {
778-
as_name = name;
779-
}
780764
PyObject *obj = CPyImport_ImportFrom(mod, mod_id, name, as_name);
781765
if (obj == NULL) {
782766
Py_DECREF(mod);

mypyc/test-data/irbuild-basic.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ L2:
745745
r10 = load_address r9
746746
r11 = [1, 2, 3, 4]
747747
r12 = load_address r11
748-
r13 = (('sys', None, None), ('enum', 'enum2', None), ('collections.abc', None, 'collections'), ('collections.abc', 'abc2', None))
748+
r13 = (('sys', 'sys', 'sys'), ('enum', 'enum', 'enum2'), ('collections.abc', 'collections', 'collections'), ('collections.abc', 'collections.abc', 'abc2'))
749749
r14 = __main__.globals :: static
750750
r15 = 'main'
751751
r16 = '<module>'
@@ -760,7 +760,7 @@ L2:
760760
r25 = load_address r24
761761
r26 = [6]
762762
r27 = load_address r26
763-
r28 = (('single', None, None),)
763+
r28 = (('single', 'single', 'single'),)
764764
r29 = __main__.globals :: static
765765
r30 = 'main'
766766
r31 = '<module>'
@@ -3498,7 +3498,7 @@ L0:
34983498
r2 = load_address r1
34993499
r3 = [2]
35003500
r4 = load_address r3
3501-
r5 = (('p.m', None, 'p'),)
3501+
r5 = (('p.m', 'p', 'p'),)
35023502
r6 = __main__.globals :: static
35033503
r7 = 'main'
35043504
r8 = 'f'

0 commit comments

Comments
 (0)