summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2007-11-18 19:35:23 (GMT)
committerChristian Heimes <christian@cheimes.de>2007-11-18 19:35:23 (GMT)
commit9cd177526afc086a300b548588880329c32f607d (patch)
treee842c29c3f11a1bcd885863fa8ea90e40f155bc8 /Modules
parent81ca7c784c4dd58dde691284321e3e220550a75c (diff)
downloadcpython-9cd177526afc086a300b548588880329c32f607d.zip
cpython-9cd177526afc086a300b548588880329c32f607d.tar.gz
cpython-9cd177526afc086a300b548588880329c32f607d.tar.bz2
Merged revisions 59005-59040 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk I've tried to fix test_cmd_line_script but I wasn't able to get all tests right. Nick, can you please have a look? ........ r59020 | facundo.batista | 2007-11-16 19:04:14 +0100 (Fri, 16 Nov 2007) | 12 lines Now in find, rfind, index, and rindex, you can use None as defaults, as usual with slicing (both with str and unicode strings). This fixes issue 1259. For str only the stringobject.c file was modified. But for unicode, I needed to repeat in the four functions a lot of code, so created a new function that does part of the job for them (and placed it in find.h, following a suggestion of Barry). Also added tests for this behaviour. ........ r59021 | facundo.batista | 2007-11-16 19:41:24 +0100 (Fri, 16 Nov 2007) | 4 lines Fix for stupid error (I need to remember to do a full 'make clean + make' cycle before the tests...). Sorry. ........ r59022 | facundo.batista | 2007-11-16 20:16:15 +0100 (Fri, 16 Nov 2007) | 3 lines Made _ParseTupleFinds only defined to unicodeobject.c ........ r59024 | raymond.hettinger | 2007-11-17 02:51:22 +0100 (Sat, 17 Nov 2007) | 1 line Fix signature in example ........ r59033 | brett.cannon | 2007-11-17 08:07:29 +0100 (Sat, 17 Nov 2007) | 5 lines Remove a confusing sentence about pth files and which directories are searched for them. Closes issue #1431. Thanks Giambattista Bloisi for the help. ........ r59039 | nick.coghlan | 2007-11-18 12:56:28 +0100 (Sun, 18 Nov 2007) | 1 line Patch #1739468: Directories and zipfiles containing __main__.py are now executable ........
Diffstat (limited to 'Modules')
-rw-r--r--Modules/main.c120
1 files changed, 80 insertions, 40 deletions
diff --git a/Modules/main.c b/Modules/main.c
index df4c6d8..6964337 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -147,7 +147,7 @@ static void RunStartupFile(PyCompilerFlags *cf)
}
-static int RunModule(char *module)
+static int RunModule(char *module, int set_argv0)
{
PyObject *runpy, *runmodule, *runargs, *result;
runpy = PyImport_ImportModule("runpy");
@@ -161,7 +161,7 @@ static int RunModule(char *module)
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");
@@ -183,6 +183,35 @@ static int RunModule(char *module)
return 0;
}
+static int RunMainFromImporter(char *filename)
+{
+ PyObject *argv0 = NULL, *importer = NULL;
+
+ if (
+ (argv0 = PyString_FromString(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_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
@@ -354,39 +383,6 @@ Py_Main(int argc, char **argv)
#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);
- fclose(fp);
- return 1;
- }
- }
- }
}
stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
@@ -481,19 +477,63 @@ Py_Main(int argc, char **argv)
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 ? "<stdin>" : 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 ? "<stdin>" : filename,
+ filename != NULL, &cf) != 0;
+ }
+
}
/* Check this environment variable at the end, to give programs the