Skip to content

Commit 66652df

Browse files
committed
Error handling tweaks
1 parent 61c1906 commit 66652df

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

mypyc/codegen/emitmodule.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ def emit_module_init_func(
12661266
# CPyImport_ImportNative can detect that this module is already
12671267
# being initialized and avoid re-executing the module body.
12681268
emitter.emit_line(f'modname = PyUnicode_FromString("{module_name}");')
1269-
emitter.emit_line("if (modname == NULL) goto fail;")
1269+
emitter.emit_line("if (modname == NULL) CPyError_OutOfMemory();")
12701270
emitter.emit_line(
12711271
f"if (PyObject_SetItem(PyImport_GetModuleDict(), modname, {module_static}) < 0)"
12721272
)
@@ -1280,12 +1280,13 @@ def emit_module_init_func(
12801280
emitter.emit_line("{")
12811281
emitter.emit_line(" PyObject *exc_type, *exc_val, *exc_tb;")
12821282
emitter.emit_line(" PyErr_Fetch(&exc_type, &exc_val, &exc_tb);")
1283-
emitter.emit_line(f' modname = PyUnicode_FromString("{module_name}");')
1284-
emitter.emit_line(" if (modname != NULL) {")
1285-
emitter.emit_line(" PyObject_DelItem(PyImport_GetModuleDict(), modname);")
1286-
emitter.emit_line(" PyErr_Clear();")
1283+
emitter.emit_line(" if (modname == NULL) {")
1284+
emitter.emit_line(f' modname = PyUnicode_FromString("{module_name}");')
1285+
emitter.emit_line(" if (modname == NULL) CPyError_OutOfMemory();")
12871286
emitter.emit_line(" }")
1288-
emitter.emit_line(" Py_XDECREF(modname);")
1287+
emitter.emit_line(" PyObject_DelItem(PyImport_GetModuleDict(), modname);")
1288+
emitter.emit_line(" PyErr_Clear();")
1289+
emitter.emit_line(" Py_DECREF(modname);")
12891290
emitter.emit_line(f" Py_CLEAR({module_static});")
12901291
emitter.emit_line(" PyErr_Restore(exc_type, exc_val, exc_tb);")
12911292
emitter.emit_line("}")

mypyc/irbuild/mapper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ def is_native_module(self, module: str) -> bool:
233233
return module in self.group_map
234234

235235
def is_native_ref_expr(self, expr: RefExpr) -> bool:
236-
# TODO: What if native package has a non-native submodule?
237236
if expr.node is None:
238237
return False
239238
if "." in expr.node.fullname:

mypyc/lib-rt/misc_ops.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,16 @@ static int CPyImport_InitSpecClasses(void) {
12071207
Py_CLEAR(CPyImport_ExtFileLoaderClass);
12081208
return -1;
12091209
}
1210-
CPyImport_SpecKwnames = PyTuple_Pack(2,
1211-
PyUnicode_InternFromString("origin"),
1212-
PyUnicode_InternFromString("is_package"));
1210+
PyObject *origin_str = PyUnicode_InternFromString("origin");
1211+
PyObject *is_package_str = PyUnicode_InternFromString("is_package");
1212+
if (origin_str == NULL || is_package_str == NULL) {
1213+
CPyError_OutOfMemory();
1214+
}
1215+
CPyImport_SpecKwnames = PyTuple_Pack(2, origin_str, is_package_str);
1216+
Py_DECREF(origin_str);
1217+
Py_DECREF(is_package_str);
12131218
if (CPyImport_SpecKwnames == NULL) {
1214-
Py_CLEAR(CPyImport_ModuleSpecClass);
1215-
Py_CLEAR(CPyImport_ExtFileLoaderClass);
1216-
return -1;
1219+
CPyError_OutOfMemory();
12171220
}
12181221
return 0;
12191222
}
@@ -1258,9 +1261,7 @@ static int CPyImport_SetModuleFile(PyObject *modobj, PyObject *module_name,
12581261
PyObject *dot_str = PyUnicode_FromString(".");
12591262
PyObject *sep_str = PyUnicode_FromOrdinal(sep_char);
12601263
if (dot_str == NULL || sep_str == NULL) {
1261-
Py_XDECREF(dot_str);
1262-
Py_XDECREF(sep_str);
1263-
return -1;
1264+
CPyError_OutOfMemory();
12641265
}
12651266
PyObject *module_path = PyUnicode_Replace(module_name, dot_str, sep_str, -1);
12661267
Py_DECREF(dot_str);
@@ -1331,8 +1332,7 @@ static int CPyImport_SetModulePath(PyObject *modobj) {
13311332
}
13321333
PyObject *path_list = PyList_New(1);
13331334
if (path_list == NULL) {
1334-
Py_DECREF(dir);
1335-
return -1;
1335+
CPyError_OutOfMemory();
13361336
}
13371337
PyList_SET_ITEM(path_list, 0, dir); // steals ref to dir
13381338
int rc = PyObject_SetAttrString(modobj, "__path__", path_list);
@@ -1425,12 +1425,11 @@ PyObject *CPyImport_ImportNative(PyObject *module_name,
14251425
// Import the parent package first to preserve import ordering semantics.
14261426
PyObject *parent_name = PyUnicode_Substring(module_name, 0, dot);
14271427
if (parent_name == NULL) {
1428-
return NULL;
1428+
CPyError_OutOfMemory();
14291429
}
14301430
child_name = PyUnicode_Substring(module_name, dot + 1, name_len);
14311431
if (child_name == NULL) {
1432-
Py_DECREF(parent_name);
1433-
return NULL;
1432+
CPyError_OutOfMemory();
14341433
}
14351434
parent_module = PyImport_Import(parent_name);
14361435
Py_DECREF(parent_name);
@@ -1509,10 +1508,12 @@ PyObject *CPyImport_ImportNative(PyObject *module_name,
15091508
package_name = PyUnicode_Substring(module_name, 0, dot);
15101509
} else {
15111510
package_name = PyUnicode_FromString("");
1511+
if (package_name == NULL) {
1512+
CPyError_OutOfMemory();
1513+
}
15121514
}
1513-
if (package_name == NULL ||
1514-
PyObject_SetAttrString(modobj, "__package__", package_name) < 0) {
1515-
Py_XDECREF(package_name);
1515+
if (PyObject_SetAttrString(modobj, "__package__", package_name) < 0) {
1516+
Py_DECREF(package_name);
15161517
goto fail;
15171518
}
15181519
Py_DECREF(package_name);

0 commit comments

Comments
 (0)