summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-10-06 23:24:57 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-10-06 23:24:57 (GMT)
commit4726e40e00ad9c2e333c4dfc5005f410a520834c (patch)
treec696aaf5ed4c27d43d37049fd54ce8c8c843080d /Modules
parentc08ec9fdbaeabb2cedf1245f857a310bdceea7cc (diff)
downloadcpython-4726e40e00ad9c2e333c4dfc5005f410a520834c.zip
cpython-4726e40e00ad9c2e333c4dfc5005f410a520834c.tar.gz
cpython-4726e40e00ad9c2e333c4dfc5005f410a520834c.tar.bz2
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
Diffstat (limited to 'Modules')
-rw-r--r--Modules/main.c60
1 files changed, 35 insertions, 25 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 0043d85..5e9f82a 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