summaryrefslogtreecommitdiffstats
path: root/Modules/main.c
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2006-03-15 11:00:26 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2006-03-15 11:00:26 (GMT)
commite2ebb2d7f777db2de72cfeb0e3c489ac4cc5c400 (patch)
tree19301755936baa73509f43a9f06fbcd334bae8e5 /Modules/main.c
parent8ea61f1a83985e38627318d19ec7a3febdb5cacd (diff)
downloadcpython-e2ebb2d7f777db2de72cfeb0e3c489ac4cc5c400.zip
cpython-e2ebb2d7f777db2de72cfeb0e3c489ac4cc5c400.tar.gz
cpython-e2ebb2d7f777db2de72cfeb0e3c489ac4cc5c400.tar.bz2
Implement PEP 338 which has been marked as accepted by GvR
Diffstat (limited to 'Modules/main.c')
-rw-r--r--Modules/main.c79
1 files changed, 37 insertions, 42 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 8e7c50b..913e82e 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -132,27 +132,42 @@ static void RunStartupFile(PyCompilerFlags *cf)
}
}
-/* Get the path to a top-level module */
-static struct filedescr * FindModule(const char *module,
- FILE **fp, char **filename)
-{
- struct filedescr *fdescr = NULL;
- *fp = NULL;
- *filename = malloc(MAXPATHLEN);
-
- if (*filename == NULL)
- return NULL;
- /* Find the actual module source code */
- fdescr = _PyImport_FindModule(module, NULL,
- *filename, MAXPATHLEN, fp, NULL);
-
- if (fdescr == NULL) {
- free(*filename);
- *filename = NULL;
+static int RunModule(char *module)
+{
+ PyObject *runpy, *runmodule, *runargs, *result;
+ runpy = PyImport_ImportModule("runpy");
+ if (runpy == NULL) {
+ fprintf(stderr, "Could not import runpy module\n");
+ return -1;
}
-
- return fdescr;
+ runmodule = PyObject_GetAttrString(runpy, "run_module");
+ if (runmodule == NULL) {
+ fprintf(stderr, "Could not access runpy.run_module\n");
+ Py_DECREF(runpy);
+ return -1;
+ }
+ runargs = Py_BuildValue("sOsO", module,
+ Py_None, "__main__", Py_True);
+ if (runargs == NULL) {
+ fprintf(stderr,
+ "Could not create arguments for runpy.run_module\n");
+ Py_DECREF(runpy);
+ Py_DECREF(runmodule);
+ return -1;
+ }
+ result = PyObject_Call(runmodule, runargs, NULL);
+ if (result == NULL) {
+ PyErr_Print();
+ }
+ Py_DECREF(runpy);
+ Py_DECREF(runmodule);
+ Py_DECREF(runargs);
+ if (result == NULL) {
+ return -1;
+ }
+ Py_DECREF(result);
+ return 0;
}
/* Main program */
@@ -441,28 +456,9 @@ Py_Main(int argc, char **argv)
}
if (module != NULL) {
- /* Backup _PyOS_optind and find the real file */
- struct filedescr *fdescr = NULL;
+ /* Backup _PyOS_optind and force sys.arv[0] = module */
_PyOS_optind--;
- if ((fdescr = FindModule(module, &fp, &filename))) {
- argv[_PyOS_optind] = filename;
- } else {
- fprintf(stderr, "%s: module %s not found\n",
- argv[0], module);
- return 2;
- }
- if (!fp) {
- fprintf(stderr,
- "%s: module %s has no associated file\n",
- argv[0], module);
- return 2;
- }
- if (!_PyImport_IsScript(fdescr)) {
- fprintf(stderr,
- "%s: module %s not usable as script\n (%s)\n",
- argv[0], module, filename);
- return 2;
- }
+ argv[_PyOS_optind] = module;
}
PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
@@ -481,9 +477,8 @@ Py_Main(int argc, char **argv)
sts = PyRun_SimpleStringFlags(command, &cf) != 0;
free(command);
} else if (module) {
- sts = PyRun_AnyFileExFlags(fp, filename, 1, &cf) != 0;
+ sts = RunModule(module);
free(module);
- free(filename);
}
else {
if (filename == NULL && stdin_is_interactive) {