Index: Python/import.c =================================================================== --- Python/import.c (revision 57534) +++ Python/import.c (working copy) @@ -104,7 +104,6 @@ }; #endif -static PyTypeObject NullImporterType; /* Forward reference */ /* Initialize things */ @@ -167,7 +166,7 @@ /* adding sys.path_hooks and sys.path_importer_cache, setting up zipimport */ - if (PyType_Ready(&NullImporterType) < 0) + if (PyType_Ready(&Py_NullImporterType) < 0) goto error; if (Py_VerboseFlag) @@ -1088,7 +1087,7 @@ } if (importer == NULL) { importer = PyObject_CallFunctionObjArgs( - (PyObject *)&NullImporterType, p, NULL + (PyObject *)&Py_NullImporterType, p, NULL ); if (importer == NULL) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { @@ -1106,6 +1105,18 @@ return importer; } +PyAPI_FUNC(PyObject *) +PyImport_GetImporter(PyObject *path) { + PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; + + (path_importer_cache = PySys_GetObject("path_importer_cache")) && + (path_hooks = PySys_GetObject("path_hooks")) && + (importer = get_path_importer(path_importer_cache, path_hooks, path)); + + Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ + return importer; +} + /* Search the path (default sys.path) for a module. Return the corresponding filedescr struct, and (via return arguments) the pathname and an open file. Return NULL if the module is not found. */ @@ -3033,7 +3044,7 @@ }; -static PyTypeObject NullImporterType = { +PyTypeObject Py_NullImporterType = { PyVarObject_HEAD_INIT(NULL, 0) "imp.NullImporter", /*tp_name*/ sizeof(NullImporter), /*tp_basicsize*/ @@ -3080,7 +3091,7 @@ { PyObject *m, *d; - if (PyType_Ready(&NullImporterType) < 0) + if (PyType_Ready(&Py_NullImporterType) < 0) goto failure; m = Py_InitModule4("imp", imp_methods, doc_imp, @@ -3102,8 +3113,8 @@ if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; - Py_INCREF(&NullImporterType); - PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType); + Py_INCREF(&Py_NullImporterType); + PyModule_AddObject(m, "NullImporter", (PyObject *)&Py_NullImporterType); failure: ; } Index: Include/import.h =================================================================== --- Include/import.h (revision 57534) +++ Include/import.h (working copy) @@ -24,6 +24,7 @@ #define PyImport_ImportModuleEx(n, g, l, f) \ PyImport_ImportModuleLevel(n, g, l, f, -1) +PyAPI_FUNC(PyObject *) PyImport_GetImporter(PyObject *path); PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name); PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m); PyAPI_FUNC(void) PyImport_Cleanup(void); @@ -42,6 +43,7 @@ void (*initfunc)(void); }; +PyAPI_DATA(PyTypeObject) Py_NullImporterType; PyAPI_DATA(struct _inittab *) PyImport_Inittab; PyAPI_FUNC(int) PyImport_AppendInittab(char *name, void (*initfunc)(void)); Index: Modules/main.c =================================================================== --- Modules/main.c (revision 57534) +++ Modules/main.c (working copy) @@ -141,7 +141,7 @@ } -static int RunModule(char *module) +static int RunModule(char *module, int set_argv0) { PyObject *runpy, *runmodule, *runargs, *result; runpy = PyImport_ImportModule("runpy"); @@ -155,7 +155,7 @@ Py_DECREF(runpy); return -1; } - runargs = Py_BuildValue("(s)", module); + runargs = Py_BuildValue("(si)", module, set_argv0); if (runargs == NULL) { fprintf(stderr, "Could not create arguments for runpy._run_module_as_main\n"); @@ -177,6 +177,35 @@ return 0; } +static int RunMainFromImporter(char *filename) +{ + PyObject *argv0 = NULL, *importer = NULL; + + if ( + (argv0 = PyString_FromString(filename)) && + (importer = PyImport_GetImporter(argv0)) && + (importer->ob_type != &Py_NullImporterType) + ) { + /* 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_CLEAR(importer); + sys_path = NULL; + return RunModule("__main__", 0) != 0; + } + } + PyErr_Clear(); + Py_CLEAR(argv0); + Py_CLEAR(importer); + return -1; +} + + /* Wait until threading._shutdown completes, provided the threading module was imported in the first place. The shutdown routine will wait until all non-daemon @@ -388,38 +417,6 @@ #else filename = argv[_PyOS_optind]; #endif - if (filename != NULL) { - if ((fp = fopen(filename, "r")) == NULL) { -#ifdef HAVE_STRERROR - fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", - argv[0], filename, errno, strerror(errno)); -#else - fprintf(stderr, "%s: can't open file '%s': Errno %d\n", - argv[0], filename, errno); -#endif - return 2; - } - else if (skipfirstline) { - int ch; - /* Push back first newline so line numbers - remain the same */ - while ((ch = getc(fp)) != EOF) { - if (ch == '\n') { - (void)ungetc(ch, fp); - break; - } - } - } - { - /* XXX: does this work on Win/Win64? (see posix_fstat) */ - struct stat sb; - if (fstat(fileno(fp), &sb) == 0 && - S_ISDIR(sb.st_mode)) { - fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); - return 1; - } - } - } } stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); @@ -514,19 +511,63 @@ sts = PyRun_SimpleStringFlags(command, &cf) != 0; free(command); } else if (module) { - sts = RunModule(module); + sts = RunModule(module, 1); free(module); } else { + if (filename == NULL && stdin_is_interactive) { Py_InspectFlag = 0; /* do exit on SystemExit */ RunStartupFile(&cf); } /* XXX */ - sts = PyRun_AnyFileExFlags( - fp, - filename == NULL ? "" : filename, - filename != NULL, &cf) != 0; + + sts = -1; /* keep track of whether we've already run __main__ */ + + if (filename != NULL) { + sts = RunMainFromImporter(filename); + } + + if (sts==-1 && filename!=NULL) { + if ((fp = fopen(filename, "r")) == NULL) { +#ifdef HAVE_STRERROR + fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", + argv[0], filename, errno, strerror(errno)); +#else + fprintf(stderr, "%s: can't open file '%s': Errno %d\n", + argv[0], filename, errno); +#endif + return 2; + } + else if (skipfirstline) { + int ch; + /* Push back first newline so line numbers + remain the same */ + while ((ch = getc(fp)) != EOF) { + if (ch == '\n') { + (void)ungetc(ch, fp); + break; + } + } + } + { + /* XXX: does this work on Win/Win64? (see posix_fstat) */ + struct stat sb; + if (fstat(fileno(fp), &sb) == 0 && + S_ISDIR(sb.st_mode)) { + fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); + return 1; + } + } + } + + if (sts==-1) { + sts = PyRun_AnyFileExFlags( + fp, + filename == NULL ? "" : filename, + filename != NULL, &cf) != 0; + } + } /* Check this environment variable at the end, to give programs the