Commits

Victor Stinner committed 4d67a3b

Issue #3080: Refactor load_package(), don't use goto

Comments (0)

Files changed (1)

 load_package(PyObject *name, PyObject *pathname)
 {
     PyObject *m, *d, *bufobj;
-    PyObject *file = NULL;
-    PyObject *path_list = NULL;
+    PyObject *file = NULL, *path_list = NULL;
     int err;
     char buf[MAXPATHLEN+1];
     FILE *fp = NULL;
             name, pathname);
     file = get_sourcefile(pathname);
     if (file == NULL)
-        goto error;
+        return NULL;
     path_list = Py_BuildValue("[O]", file);
-    if (path_list == NULL)
-        goto error;
+    if (path_list == NULL) {
+        Py_DECREF(file);
+        return NULL;
+    }
     d = PyModule_GetDict(m);
     err = PyDict_SetItemString(d, "__file__", file);
+    Py_DECREF(file);
     if (err == 0)
         err = PyDict_SetItemString(d, "__path__", path_list);
-    if (err != 0)
-        goto error;
+    if (err != 0) {
+        Py_DECREF(path_list);
+        return NULL;
+    }
     fdp = find_module(name, initstr, path_list,
                       buf, sizeof(buf), &fp, NULL);
+    Py_DECREF(path_list);
     if (fdp == NULL) {
         if (PyErr_ExceptionMatches(PyExc_ImportError)) {
             PyErr_Clear();
             Py_INCREF(m);
+            return m;
         }
         else
-            m = NULL;
-        goto cleanup;
+            return NULL;
     }
     bufobj = PyUnicode_DecodeFSDefault(buf);
     if (bufobj != NULL) {
         m = NULL;
     if (fp != NULL)
         fclose(fp);
-    goto cleanup;
-
-  error:
-    m = NULL;
-  cleanup:
-    Py_XDECREF(path_list);
-    Py_XDECREF(file);
     return m;
 }