From 4726e40e00ad9c2e333c4dfc5005f410a520834c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 6 Oct 2010 23:24:57 +0000 Subject: [PATCH] Rewrite RunMainFromImporter() * fix argv0 reference counter if PyList_SetItem() fails * don't use complex if conditions, but a simple indentation and "goto error" * simplify error handling (remove Py_XDECREF(importer) from the error label) * don't set sys_path to NULL (it's useless, sys_path is a borrowed reference and sys_path is not a static variable) * try to write only one instruction per line for better readability --- Modules/main.c | 60 +++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/Modules/main.c b/Modules/main.c index 0043d8530b..5e9f82a287 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -228,35 +228,45 @@ static int RunModule(wchar_t *modname, int set_argv0) return 0; } -static int RunMainFromImporter(wchar_t *filename) +static int +RunMainFromImporter(wchar_t *filename) { - PyObject *argv0 = NULL, *importer = NULL; + PyObject *argv0 = NULL, *importer, *sys_path; + int sts; - if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) && - (importer = PyImport_GetImporter(argv0)) && - (importer->ob_type != &PyNullImporter_Type)) - { - /* argv0 is usable as an import source, so - put it in sys.path[0] and import __main__ */ - PyObject *sys_path = NULL; - if ((sys_path = PySys_GetObject("path")) && - !PyList_SetItem(sys_path, 0, argv0)) - { - Py_INCREF(argv0); - Py_DECREF(importer); - sys_path = NULL; - return RunModule(L"__main__", 0) != 0; - } - } - Py_XDECREF(argv0); - Py_XDECREF(importer); - if (PyErr_Occurred()) { - PyErr_Print(); - return 1; - } - else { + argv0 = PyUnicode_FromWideChar(filename, wcslen(filename)); + if (argv0 == NULL) + goto error; + + importer = PyImport_GetImporter(argv0); + if (importer == NULL) + goto error; + + if (importer->ob_type == &PyNullImporter_Type) { + Py_DECREF(argv0); + Py_DECREF(importer); return -1; } + Py_DECREF(importer); + + /* argv0 is usable as an import source, so put it in sys.path[0] + and import __main__ */ + sys_path = PySys_GetObject("path"); + if (sys_path == NULL) + goto error; + if (PyList_SetItem(sys_path, 0, argv0)) { + argv0 = NULL; + goto error; + } + Py_INCREF(argv0); + + sts = RunModule(L"__main__", 0); + return sts != 0; + +error: + Py_XDECREF(argv0); + PyErr_Print(); + return 1; } static int -- 2.49.0