diff options
Diffstat (limited to 'Modules/main.c')
-rw-r--r-- | Modules/main.c | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/Modules/main.c b/Modules/main.c index 3cfc9c3bd4..9171070ab5 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -22,15 +22,11 @@ #include <crtdbg.h> #endif -#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS) +#if defined(MS_WINDOWS) #define PYTHONHOMEHELP "<prefix>\\lib" #else -#if defined(PYOS_OS2) && defined(PYCC_GCC) -#define PYTHONHOMEHELP "<prefix>/Lib" -#else #define PYTHONHOMEHELP "<prefix>/pythonX.X" #endif -#endif #include "pygetopt.h" @@ -47,7 +43,7 @@ static wchar_t **orig_argv; static int orig_argc; /* command line options */ -#define BASE_OPTS L"bBc:dEhiJm:OqRsStuvVW:xX:?" +#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?" #define PROGRAM_OPTS BASE_OPTS @@ -69,6 +65,7 @@ Options and arguments (and corresponding environment variables):\n\ static char *usage_2 = "\ -i : inspect interactively after running script; forces a prompt even\n\ if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ +-I : isolate Python from the user's environment (implies -E and -s)\n\ -m mod : run library module as a script (terminates option list)\n\ -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ -OO : remove doc-strings in addition to the -O optimizations\n\ @@ -146,7 +143,7 @@ static void RunStartupFile(PyCompilerFlags *cf) { char *startup = Py_GETENV("PYTHONSTARTUP"); if (startup != NULL && startup[0] != '\0') { - FILE *fp = fopen(startup, "r"); + FILE *fp = _Py_fopen(startup, "r"); if (fp != NULL) { (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); PyErr_Clear(); @@ -165,6 +162,32 @@ static void RunStartupFile(PyCompilerFlags *cf) } } +static void RunInteractiveHook(void) +{ + PyObject *sys, *hook, *result; + sys = PyImport_ImportModule("sys"); + if (sys == NULL) + goto error; + hook = PyObject_GetAttrString(sys, "__interactivehook__"); + Py_DECREF(sys); + if (hook == NULL) + PyErr_Clear(); + else { + result = PyObject_CallObject(hook, NULL); + Py_DECREF(hook); + if (result == NULL) + goto error; + else + Py_DECREF(result); + } + return; + +error: + PySys_WriteStderr("Failed calling sys.__interactivehook__\n"); + PyErr_Print(); + PyErr_Clear(); +} + static int RunModule(wchar_t *modname, int set_argv0) { @@ -172,17 +195,20 @@ static int RunModule(wchar_t *modname, int set_argv0) runpy = PyImport_ImportModule("runpy"); if (runpy == NULL) { fprintf(stderr, "Could not import runpy module\n"); + PyErr_Print(); return -1; } runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); if (runmodule == NULL) { fprintf(stderr, "Could not access runpy._run_module_as_main\n"); + PyErr_Print(); Py_DECREF(runpy); return -1; } module = PyUnicode_FromWideChar(modname, wcslen(modname)); if (module == NULL) { fprintf(stderr, "Could not convert module name to unicode\n"); + PyErr_Print(); Py_DECREF(runpy); Py_DECREF(runmodule); return -1; @@ -191,6 +217,7 @@ static int RunModule(wchar_t *modname, int set_argv0) if (runargs == NULL) { fprintf(stderr, "Could not create arguments for runpy._run_module_as_main\n"); + PyErr_Print(); Py_DECREF(runpy); Py_DECREF(runmodule); Py_DECREF(module); @@ -235,8 +262,10 @@ RunMainFromImporter(wchar_t *filename) /* 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) + if (sys_path == NULL) { + PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); goto error; + } if (PyList_SetItem(sys_path, 0, argv0)) { argv0 = NULL; goto error; @@ -366,7 +395,7 @@ Py_Main(int argc, wchar_t **argv) command to interpret. */ len = wcslen(_PyOS_optarg) + 1 + 1; - command = (wchar_t *)malloc(sizeof(wchar_t) * len); + command = (wchar_t *)PyMem_RawMalloc(sizeof(wchar_t) * len); if (command == NULL) Py_FatalError( "not enough memory to copy -c argument"); @@ -398,6 +427,12 @@ Py_Main(int argc, wchar_t **argv) Py_InteractiveFlag++; break; + case 'I': + Py_IsolatedFlag++; + Py_NoUserSiteDirectory++; + Py_IgnoreEnvironmentFlag++; + break; + /* case 'J': reserved for Jython */ case 'O': @@ -475,7 +510,7 @@ Py_Main(int argc, wchar_t **argv) return usage(0, argv[0]); if (version) { - fprintf(stderr, "Python %s\n", PY_VERSION); + printf("Python %s\n", PY_VERSION); return 0; } @@ -495,7 +530,7 @@ Py_Main(int argc, wchar_t **argv) *wp != L'\0') { wchar_t *buf, *warning; - buf = (wchar_t *)malloc((wcslen(wp) + 1) * sizeof(wchar_t)); + buf = (wchar_t *)PyMem_RawMalloc((wcslen(wp) + 1) * sizeof(wchar_t)); if (buf == NULL) Py_FatalError( "not enough memory to copy PYTHONWARNINGS"); @@ -505,7 +540,7 @@ Py_Main(int argc, wchar_t **argv) warning = wcstok(NULL, L",")) { PySys_AddWarnOption(warning); } - free(buf); + PyMem_RawFree(buf); } #else if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { @@ -514,12 +549,12 @@ Py_Main(int argc, wchar_t **argv) /* settle for strtok here as there's no one standard C89 wcstok */ - buf = (char *)malloc(strlen(p) + 1); + buf = (char *)PyMem_RawMalloc(strlen(p) + 1); if (buf == NULL) Py_FatalError( "not enough memory to copy PYTHONWARNINGS"); strcpy(buf, p); - oldloc = strdup(setlocale(LC_ALL, NULL)); + oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); setlocale(LC_ALL, ""); for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) { #ifdef __APPLE__ @@ -537,8 +572,8 @@ Py_Main(int argc, wchar_t **argv) Py_DECREF(unicode); } setlocale(LC_ALL, oldloc); - free(oldloc); - free(buf); + PyMem_RawFree(oldloc); + PyMem_RawFree(buf); } #endif @@ -608,7 +643,7 @@ Py_Main(int argc, wchar_t **argv) wchar_t* buffer; size_t len = strlen(p) + 1; - buffer = malloc(len * sizeof(wchar_t)); + buffer = PyMem_RawMalloc(len * sizeof(wchar_t)); if (buffer == NULL) { Py_FatalError( "not enough memory to copy PYTHONEXECUTABLE"); @@ -682,7 +717,7 @@ Py_Main(int argc, wchar_t **argv) if (command) { sts = run_command(command, &cf); - free(command); + PyMem_RawFree(command); } else if (module) { sts = (RunModule(module, 1) != 0); } @@ -691,6 +726,7 @@ Py_Main(int argc, wchar_t **argv) if (filename == NULL && stdin_is_interactive) { Py_InspectFlag = 0; /* do exit on SystemExit */ RunStartupFile(&cf); + RunInteractiveHook(); } /* XXX */ @@ -756,6 +792,7 @@ Py_Main(int argc, wchar_t **argv) if (Py_InspectFlag && stdin_is_interactive && (filename != NULL || command != NULL || module != NULL)) { Py_InspectFlag = 0; + RunInteractiveHook(); /* XXX */ sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; } |