Changeset 391 for python/trunk/Modules/main.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/main.c
r2 r391 41 41 42 42 /* command line options */ 43 #define BASE_OPTS "3bBc:dEhiJm:OQ: sStuUvVW:xX?"43 #define BASE_OPTS "3bBc:dEhiJm:OQ:RsStuUvVW:xX?" 44 44 45 45 #ifndef RISCOS … … 72 72 -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ 73 73 -OO : remove doc-strings in addition to the -O optimizations\n\ 74 -R : use a pseudo-random salt to make hash() values of various types be\n\ 75 unpredictable between separate invocations of the interpreter, as\n\ 76 a defense against denial-of-service attacks\n\ 74 77 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\ 75 78 -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\ … … 84 87 -V : print the Python version number and exit (also --version)\n\ 85 88 -W arg : warning control; arg is action:message:category:module:lineno\n\ 89 also PYTHONWARNINGS=arg\n\ 86 90 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ 87 91 "; … … 102 106 PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\ 103 107 "; 108 static char *usage_6 = "\ 109 PYTHONHASHSEED: if this variable is set to 'random', the effect is the same\n\ 110 as specifying the -R option: a random value is used to seed the hashes of\n\ 111 str, bytes and datetime objects. It can also be set to an integer\n\ 112 in the range [0,4294967295] to get hash values with a predictable seed.\n\ 113 "; 104 114 105 115 … … 107 117 usage(int exitcode, char* program) 108 118 { 109 FILE *f = exitcode ? stderr : stdout; 110 111 fprintf(f, usage_line, program); 112 if (exitcode) 113 fprintf(f, "Try `python -h' for more information.\n"); 114 else { 115 fprintf(f, usage_1); 116 fprintf(f, usage_2); 117 fprintf(f, usage_3); 118 fprintf(f, usage_4, DELIM); 119 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP); 120 } 119 FILE *f = exitcode ? stderr : stdout; 120 121 fprintf(f, usage_line, program); 122 if (exitcode) 123 fprintf(f, "Try `python -h' for more information.\n"); 124 else { 125 fputs(usage_1, f); 126 fputs(usage_2, f); 127 fputs(usage_3, f); 128 fprintf(f, usage_4, DELIM); 129 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP); 130 fputs(usage_6, f); 131 } 121 132 #if defined(__VMS) 122 123 124 125 126 127 128 129 133 if (exitcode == 0) { 134 /* suppress 'error' message */ 135 return 1; 136 } 137 else { 138 /* STS$M_INHIB_MSG + SS$_ABORT */ 139 return 0x1000002c; 140 } 130 141 #else 131 132 #endif 133 142 return exitcode; 143 #endif 144 /*NOTREACHED*/ 134 145 } 135 146 136 147 static void RunStartupFile(PyCompilerFlags *cf) 137 148 { 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 149 char *startup = Py_GETENV("PYTHONSTARTUP"); 150 if (startup != NULL && startup[0] != '\0') { 151 FILE *fp = fopen(startup, "r"); 152 if (fp != NULL) { 153 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); 154 PyErr_Clear(); 155 fclose(fp); 156 } else { 157 int save_errno; 158 save_errno = errno; 159 PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); 160 errno = save_errno; 161 PyErr_SetFromErrnoWithFilename(PyExc_IOError, 162 startup); 163 PyErr_Print(); 164 PyErr_Clear(); 165 } 166 } 156 167 } 157 168 … … 159 170 static int RunModule(char *module, int set_argv0) 160 171 { 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 172 PyObject *runpy, *runmodule, *runargs, *result; 173 runpy = PyImport_ImportModule("runpy"); 174 if (runpy == NULL) { 175 fprintf(stderr, "Could not import runpy module\n"); 176 return -1; 177 } 178 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); 179 if (runmodule == NULL) { 180 fprintf(stderr, "Could not access runpy._run_module_as_main\n"); 181 Py_DECREF(runpy); 182 return -1; 183 } 184 runargs = Py_BuildValue("(si)", module, set_argv0); 185 if (runargs == NULL) { 186 fprintf(stderr, 187 "Could not create arguments for runpy._run_module_as_main\n"); 188 Py_DECREF(runpy); 189 Py_DECREF(runmodule); 190 return -1; 191 } 192 result = PyObject_Call(runmodule, runargs, NULL); 193 if (result == NULL) { 194 PyErr_Print(); 195 } 196 Py_DECREF(runpy); 197 Py_DECREF(runmodule); 198 Py_DECREF(runargs); 199 if (result == NULL) { 200 return -1; 201 } 202 Py_DECREF(result); 203 return 0; 193 204 } 194 205 195 206 static int RunMainFromImporter(char *filename) 196 207 { 197 198 199 if ((argv0 = PyString_FromString(filename)) && 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 208 PyObject *argv0 = NULL, *importer = NULL; 209 210 if ((argv0 = PyString_FromString(filename)) && 211 (importer = PyImport_GetImporter(argv0)) && 212 (importer->ob_type != &PyNullImporter_Type)) 213 { 214 /* argv0 is usable as an import source, so 215 put it in sys.path[0] and import __main__ */ 216 PyObject *sys_path = NULL; 217 if ((sys_path = PySys_GetObject("path")) && 218 !PyList_SetItem(sys_path, 0, argv0)) 219 { 220 Py_INCREF(argv0); 221 Py_DECREF(importer); 222 sys_path = NULL; 223 return RunModule("__main__", 0) != 0; 224 } 225 } 226 Py_XDECREF(argv0); 227 Py_XDECREF(importer); 228 if (PyErr_Occurred()) { 229 PyErr_Print(); 230 return 1; 231 } 232 return -1; 222 233 } 223 234 … … 228 239 Py_Main(int argc, char **argv) 229 240 { 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 orig_argc = argc;/* For Py_GetArgcArgv() */248 241 int c; 242 int sts; 243 char *command = NULL; 244 char *filename = NULL; 245 char *module = NULL; 246 FILE *fp = stdin; 247 char *p; 248 int unbuffered = 0; 249 int skipfirstline = 0; 250 int stdin_is_interactive = 0; 251 int help = 0; 252 int version = 0; 253 int saw_unbuffered_flag = 0; 254 PyCompilerFlags cf; 255 256 cf.cf_flags = 0; 257 258 orig_argc = argc; /* For Py_GetArgcArgv() */ 259 orig_argv = argv; 249 260 250 261 #ifdef RISCOS 251 Py_RISCOSWimpFlag = 0; 252 #endif 253 254 PySys_ResetWarnOptions(); 255 256 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { 257 if (c == 'c') { 258 /* -c is the last option; following arguments 259 that look like options are left for the 260 command to interpret. */ 261 command = (char *)malloc(strlen(_PyOS_optarg) + 2); 262 if (command == NULL) 263 Py_FatalError( 264 "not enough memory to copy -c argument"); 265 strcpy(command, _PyOS_optarg); 266 strcat(command, "\n"); 267 break; 268 } 269 270 if (c == 'm') { 271 /* -m is the last option; following arguments 272 that look like options are left for the 273 module to interpret. */ 274 module = (char *)malloc(strlen(_PyOS_optarg) + 2); 275 if (module == NULL) 276 Py_FatalError( 277 "not enough memory to copy -m argument"); 278 strcpy(module, _PyOS_optarg); 279 break; 280 } 281 282 switch (c) { 283 case 'b': 284 Py_BytesWarningFlag++; 285 break; 286 287 case 'd': 288 Py_DebugFlag++; 289 break; 290 291 case '3': 292 Py_Py3kWarningFlag++; 293 if (!Py_DivisionWarningFlag) 294 Py_DivisionWarningFlag = 1; 295 break; 296 297 case 'Q': 298 if (strcmp(_PyOS_optarg, "old") == 0) { 299 Py_DivisionWarningFlag = 0; 300 break; 301 } 302 if (strcmp(_PyOS_optarg, "warn") == 0) { 303 Py_DivisionWarningFlag = 1; 304 break; 305 } 306 if (strcmp(_PyOS_optarg, "warnall") == 0) { 307 Py_DivisionWarningFlag = 2; 308 break; 309 } 310 if (strcmp(_PyOS_optarg, "new") == 0) { 311 /* This only affects __main__ */ 312 cf.cf_flags |= CO_FUTURE_DIVISION; 313 /* And this tells the eval loop to treat 314 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */ 315 _Py_QnewFlag = 1; 316 break; 317 } 318 fprintf(stderr, 319 "-Q option should be `-Qold', " 320 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n"); 321 return usage(2, argv[0]); 322 /* NOTREACHED */ 323 324 case 'i': 325 Py_InspectFlag++; 326 Py_InteractiveFlag++; 327 break; 328 329 /* case 'J': reserved for Jython */ 330 331 case 'O': 332 Py_OptimizeFlag++; 333 break; 334 335 case 'B': 336 Py_DontWriteBytecodeFlag++; 337 break; 338 339 case 's': 340 Py_NoUserSiteDirectory++; 341 break; 342 343 case 'S': 344 Py_NoSiteFlag++; 345 break; 346 347 case 'E': 348 Py_IgnoreEnvironmentFlag++; 349 break; 350 351 case 't': 352 Py_TabcheckFlag++; 353 break; 354 355 case 'u': 356 unbuffered++; 357 saw_unbuffered_flag = 1; 358 break; 359 360 case 'v': 361 Py_VerboseFlag++; 362 break; 262 Py_RISCOSWimpFlag = 0; 263 #endif 264 265 /* Hash randomization needed early for all string operations 266 (including -W and -X options). */ 267 _PyOS_opterr = 0; /* prevent printing the error in 1st pass */ 268 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { 269 if (c == 'm' || c == 'c') { 270 /* -c / -m is the last option: following arguments are 271 not interpreter options. */ 272 break; 273 } 274 switch (c) { 275 case 'E': 276 Py_IgnoreEnvironmentFlag++; 277 break; 278 case 'R': 279 Py_HashRandomizationFlag++; 280 break; 281 } 282 } 283 /* The variable is only tested for existence here; _PyRandom_Init will 284 check its value further. */ 285 if (!Py_HashRandomizationFlag && 286 (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') 287 Py_HashRandomizationFlag = 1; 288 289 _PyRandom_Init(); 290 291 PySys_ResetWarnOptions(); 292 _PyOS_ResetGetOpt(); 293 294 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { 295 if (c == 'c') { 296 /* -c is the last option; following arguments 297 that look like options are left for the 298 command to interpret. */ 299 command = (char *)malloc(strlen(_PyOS_optarg) + 2); 300 if (command == NULL) 301 Py_FatalError( 302 "not enough memory to copy -c argument"); 303 strcpy(command, _PyOS_optarg); 304 strcat(command, "\n"); 305 break; 306 } 307 308 if (c == 'm') { 309 /* -m is the last option; following arguments 310 that look like options are left for the 311 module to interpret. */ 312 module = (char *)malloc(strlen(_PyOS_optarg) + 2); 313 if (module == NULL) 314 Py_FatalError( 315 "not enough memory to copy -m argument"); 316 strcpy(module, _PyOS_optarg); 317 break; 318 } 319 320 switch (c) { 321 case 'b': 322 Py_BytesWarningFlag++; 323 break; 324 325 case 'd': 326 Py_DebugFlag++; 327 break; 328 329 case '3': 330 Py_Py3kWarningFlag++; 331 if (!Py_DivisionWarningFlag) 332 Py_DivisionWarningFlag = 1; 333 break; 334 335 case 'Q': 336 if (strcmp(_PyOS_optarg, "old") == 0) { 337 Py_DivisionWarningFlag = 0; 338 break; 339 } 340 if (strcmp(_PyOS_optarg, "warn") == 0) { 341 Py_DivisionWarningFlag = 1; 342 break; 343 } 344 if (strcmp(_PyOS_optarg, "warnall") == 0) { 345 Py_DivisionWarningFlag = 2; 346 break; 347 } 348 if (strcmp(_PyOS_optarg, "new") == 0) { 349 /* This only affects __main__ */ 350 cf.cf_flags |= CO_FUTURE_DIVISION; 351 /* And this tells the eval loop to treat 352 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */ 353 _Py_QnewFlag = 1; 354 break; 355 } 356 fprintf(stderr, 357 "-Q option should be `-Qold', " 358 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n"); 359 return usage(2, argv[0]); 360 /* NOTREACHED */ 361 362 case 'i': 363 Py_InspectFlag++; 364 Py_InteractiveFlag++; 365 break; 366 367 /* case 'J': reserved for Jython */ 368 369 case 'O': 370 Py_OptimizeFlag++; 371 break; 372 373 case 'B': 374 Py_DontWriteBytecodeFlag++; 375 break; 376 377 case 's': 378 Py_NoUserSiteDirectory++; 379 break; 380 381 case 'S': 382 Py_NoSiteFlag++; 383 break; 384 385 case 'E': 386 /* Already handled above */ 387 break; 388 389 case 't': 390 Py_TabcheckFlag++; 391 break; 392 393 case 'u': 394 unbuffered++; 395 saw_unbuffered_flag = 1; 396 break; 397 398 case 'v': 399 Py_VerboseFlag++; 400 break; 363 401 364 402 #ifdef RISCOS 365 case 'w': 366 Py_RISCOSWimpFlag = 1; 367 break; 368 #endif 369 370 case 'x': 371 skipfirstline = 1; 372 break; 373 374 /* case 'X': reserved for implementation-specific arguments */ 375 376 case 'U': 377 Py_UnicodeFlag++; 378 break; 379 case 'h': 380 case '?': 381 help++; 382 break; 383 case 'V': 384 version++; 385 break; 386 387 case 'W': 388 PySys_AddWarnOption(_PyOS_optarg); 389 break; 390 391 /* This space reserved for other options */ 392 393 default: 394 return usage(2, argv[0]); 395 /*NOTREACHED*/ 396 397 } 398 } 399 400 if (help) 401 return usage(0, argv[0]); 402 403 if (version) { 404 fprintf(stderr, "Python %s\n", PY_VERSION); 405 return 0; 406 } 407 408 if (!Py_InspectFlag && 409 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') 410 Py_InspectFlag = 1; 411 if (!saw_unbuffered_flag && 412 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') 413 unbuffered = 1; 414 415 if (!Py_NoUserSiteDirectory && 416 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0') 417 Py_NoUserSiteDirectory = 1; 418 419 if (command == NULL && module == NULL && _PyOS_optind < argc && 420 strcmp(argv[_PyOS_optind], "-") != 0) 421 { 403 case 'w': 404 Py_RISCOSWimpFlag = 1; 405 break; 406 #endif 407 408 case 'x': 409 skipfirstline = 1; 410 break; 411 412 /* case 'X': reserved for implementation-specific arguments */ 413 414 case 'U': 415 Py_UnicodeFlag++; 416 break; 417 case 'h': 418 case '?': 419 help++; 420 break; 421 case 'V': 422 version++; 423 break; 424 425 case 'W': 426 PySys_AddWarnOption(_PyOS_optarg); 427 break; 428 429 case 'R': 430 /* Already handled above */ 431 break; 432 433 /* This space reserved for other options */ 434 435 default: 436 return usage(2, argv[0]); 437 /*NOTREACHED*/ 438 439 } 440 } 441 442 if (help) 443 return usage(0, argv[0]); 444 445 if (version) { 446 fprintf(stderr, "Python %s\n", PY_VERSION); 447 return 0; 448 } 449 450 if (Py_Py3kWarningFlag && !Py_TabcheckFlag) 451 /* -3 implies -t (but not -tt) */ 452 Py_TabcheckFlag = 1; 453 454 if (!Py_InspectFlag && 455 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') 456 Py_InspectFlag = 1; 457 if (!saw_unbuffered_flag && 458 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') 459 unbuffered = 1; 460 461 if (!Py_NoUserSiteDirectory && 462 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0') 463 Py_NoUserSiteDirectory = 1; 464 465 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { 466 char *buf, *warning; 467 468 buf = (char *)malloc(strlen(p) + 1); 469 if (buf == NULL) 470 Py_FatalError( 471 "not enough memory to copy PYTHONWARNINGS"); 472 strcpy(buf, p); 473 for (warning = strtok(buf, ","); 474 warning != NULL; 475 warning = strtok(NULL, ",")) 476 PySys_AddWarnOption(warning); 477 free(buf); 478 } 479 480 if (command == NULL && module == NULL && _PyOS_optind < argc && 481 strcmp(argv[_PyOS_optind], "-") != 0) 482 { 422 483 #ifdef __VMS 423 424 425 484 filename = decc$translate_vms(argv[_PyOS_optind]); 485 if (filename == (char *)0 || filename == (char *)-1) 486 filename = argv[_PyOS_optind]; 426 487 427 488 #else 428 429 #endif 430 431 432 433 434 489 filename = argv[_PyOS_optind]; 490 #endif 491 } 492 493 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); 494 495 if (unbuffered) { 435 496 #if defined(MS_WINDOWS) || defined(__CYGWIN__) 436 437 497 _setmode(fileno(stdin), O_BINARY); 498 _setmode(fileno(stdout), O_BINARY); 438 499 #endif 439 500 #ifdef HAVE_SETVBUF 440 441 442 501 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); 502 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); 503 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); 443 504 #else /* !HAVE_SETVBUF */ 444 445 446 505 setbuf(stdin, (char *)NULL); 506 setbuf(stdout, (char *)NULL); 507 setbuf(stderr, (char *)NULL); 447 508 #endif /* !HAVE_SETVBUF */ 448 449 509 } 510 else if (Py_InteractiveFlag) { 450 511 #ifdef MS_WINDOWS 451 452 453 512 /* Doesn't have to have line-buffered -- use unbuffered */ 513 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ 514 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); 454 515 #else /* !MS_WINDOWS */ 455 516 #ifdef HAVE_SETVBUF 456 457 517 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); 518 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); 458 519 #endif /* HAVE_SETVBUF */ 459 520 #endif /* !MS_WINDOWS */ 460 461 521 /* Leave stderr alone - it should be unbuffered anyway. */ 522 } 462 523 #ifdef __VMS 463 464 465 524 else { 525 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ); 526 } 466 527 #endif /* __VMS */ 467 528 468 529 #ifdef __APPLE__ 469 470 471 472 473 474 475 476 477 478 479 480 481 530 /* On MacOS X, when the Python interpreter is embedded in an 531 application bundle, it gets executed by a bootstrapping script 532 that does os.execve() with an argv[0] that's different from the 533 actual Python executable. This is needed to keep the Finder happy, 534 or rather, to work around Apple's overly strict requirements of 535 the process name. However, we still need a usable sys.executable, 536 so the actual executable path is passed in an environment variable. 537 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap 538 script. */ 539 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') 540 Py_SetProgramName(p); 541 else 542 Py_SetProgramName(argv[0]); 482 543 #else 483 Py_SetProgramName(argv[0]); 484 #endif 485 Py_Initialize(); 486 487 if (Py_VerboseFlag || 488 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) { 489 fprintf(stderr, "Python %s on %s\n", 490 Py_GetVersion(), Py_GetPlatform()); 491 if (!Py_NoSiteFlag) 492 fprintf(stderr, "%s\n", COPYRIGHT); 493 } 494 495 if (command != NULL) { 496 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ 497 _PyOS_optind--; 498 argv[_PyOS_optind] = "-c"; 499 } 500 501 if (module != NULL) { 502 /* Backup _PyOS_optind and force sys.argv[0] = '-c' 503 so that PySys_SetArgv correctly sets sys.path[0] to ''*/ 504 _PyOS_optind--; 505 argv[_PyOS_optind] = "-c"; 506 } 507 508 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); 509 510 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) && 511 isatty(fileno(stdin))) { 512 PyObject *v; 513 v = PyImport_ImportModule("readline"); 514 if (v == NULL) 515 PyErr_Clear(); 516 else 517 Py_DECREF(v); 518 } 519 520 if (command) { 521 sts = PyRun_SimpleStringFlags(command, &cf) != 0; 522 free(command); 523 } else if (module) { 524 sts = RunModule(module, 1); 525 free(module); 526 } 527 else { 528 529 if (filename == NULL && stdin_is_interactive) { 530 Py_InspectFlag = 0; /* do exit on SystemExit */ 531 RunStartupFile(&cf); 532 } 533 /* XXX */ 534 535 sts = -1; /* keep track of whether we've already run __main__ */ 536 537 if (filename != NULL) { 538 sts = RunMainFromImporter(filename); 539 } 540 541 if (sts==-1 && filename!=NULL) { 542 if ((fp = fopen(filename, "r")) == NULL) { 543 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", 544 argv[0], filename, errno, strerror(errno)); 545 546 return 2; 547 } 548 else if (skipfirstline) { 549 int ch; 550 /* Push back first newline so line numbers 551 remain the same */ 552 while ((ch = getc(fp)) != EOF) { 553 if (ch == '\n') { 554 (void)ungetc(ch, fp); 555 break; 556 } 557 } 558 } 559 { 560 /* XXX: does this work on Win/Win64? (see posix_fstat) */ 561 struct stat sb; 562 if (fstat(fileno(fp), &sb) == 0 && 563 S_ISDIR(sb.st_mode)) { 564 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); 565 fclose(fp); 566 return 1; 567 } 568 } 569 } 570 571 if (sts==-1) { 572 sts = PyRun_AnyFileExFlags( 573 fp, 574 filename == NULL ? "<stdin>" : filename, 575 filename != NULL, &cf) != 0; 576 } 577 578 } 579 580 /* Check this environment variable at the end, to give programs the 581 * opportunity to set it from Python. 582 */ 583 if (!Py_InspectFlag && 584 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') 585 { 586 Py_InspectFlag = 1; 587 } 588 589 if (Py_InspectFlag && stdin_is_interactive && 590 (filename != NULL || command != NULL || module != NULL)) { 591 Py_InspectFlag = 0; 592 /* XXX */ 593 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; 594 } 595 596 Py_Finalize(); 544 Py_SetProgramName(argv[0]); 545 #endif 546 Py_Initialize(); 547 548 if (Py_VerboseFlag || 549 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) { 550 fprintf(stderr, "Python %s on %s\n", 551 Py_GetVersion(), Py_GetPlatform()); 552 if (!Py_NoSiteFlag) 553 fprintf(stderr, "%s\n", COPYRIGHT); 554 } 555 556 if (command != NULL) { 557 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ 558 _PyOS_optind--; 559 argv[_PyOS_optind] = "-c"; 560 } 561 562 if (module != NULL) { 563 /* Backup _PyOS_optind and force sys.argv[0] = '-c' 564 so that PySys_SetArgv correctly sets sys.path[0] to '' 565 rather than looking for a file called "-m". See 566 tracker issue #8202 for details. */ 567 _PyOS_optind--; 568 argv[_PyOS_optind] = "-c"; 569 } 570 571 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); 572 573 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) && 574 isatty(fileno(stdin))) { 575 PyObject *v; 576 v = PyImport_ImportModule("readline"); 577 if (v == NULL) 578 PyErr_Clear(); 579 else 580 Py_DECREF(v); 581 } 582 583 if (command) { 584 sts = PyRun_SimpleStringFlags(command, &cf) != 0; 585 free(command); 586 } else if (module) { 587 sts = (RunModule(module, 1) != 0); 588 free(module); 589 } 590 else { 591 592 if (filename == NULL && stdin_is_interactive) { 593 Py_InspectFlag = 0; /* do exit on SystemExit */ 594 RunStartupFile(&cf); 595 } 596 /* XXX */ 597 598 sts = -1; /* keep track of whether we've already run __main__ */ 599 600 if (filename != NULL) { 601 sts = RunMainFromImporter(filename); 602 } 603 604 if (sts==-1 && filename!=NULL) { 605 if ((fp = fopen(filename, "r")) == NULL) { 606 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", 607 argv[0], filename, errno, strerror(errno)); 608 609 return 2; 610 } 611 else if (skipfirstline) { 612 int ch; 613 /* Push back first newline so line numbers 614 remain the same */ 615 while ((ch = getc(fp)) != EOF) { 616 if (ch == '\n') { 617 (void)ungetc(ch, fp); 618 break; 619 } 620 } 621 } 622 { 623 /* XXX: does this work on Win/Win64? (see posix_fstat) */ 624 struct stat sb; 625 if (fstat(fileno(fp), &sb) == 0 && 626 S_ISDIR(sb.st_mode)) { 627 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); 628 fclose(fp); 629 return 1; 630 } 631 } 632 } 633 634 if (sts==-1) { 635 /* call pending calls like signal handlers (SIGINT) */ 636 if (Py_MakePendingCalls() == -1) { 637 PyErr_Print(); 638 sts = 1; 639 } else { 640 sts = PyRun_AnyFileExFlags( 641 fp, 642 filename == NULL ? "<stdin>" : filename, 643 filename != NULL, &cf) != 0; 644 } 645 } 646 647 } 648 649 /* Check this environment variable at the end, to give programs the 650 * opportunity to set it from Python. 651 */ 652 if (!Py_InspectFlag && 653 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') 654 { 655 Py_InspectFlag = 1; 656 } 657 658 if (Py_InspectFlag && stdin_is_interactive && 659 (filename != NULL || command != NULL || module != NULL)) { 660 Py_InspectFlag = 0; 661 /* XXX */ 662 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; 663 } 664 665 Py_Finalize(); 597 666 #ifdef RISCOS 598 599 667 if (Py_RISCOSWimpFlag) 668 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */ 600 669 #endif 601 670 602 671 #ifdef __INSURE__ 603 604 605 606 607 608 609 610 611 612 672 /* Insure++ is a memory analysis tool that aids in discovering 673 * memory leaks and other memory problems. On Python exit, the 674 * interned string dictionary is flagged as being in use at exit 675 * (which it is). Under normal circumstances, this is fine because 676 * the memory will be automatically reclaimed by the system. Under 677 * memory debugging, it's a huge source of useless noise, so we 678 * trade off slower shutdown for less distraction in the memory 679 * reports. -baw 680 */ 681 _Py_ReleaseInternedStrings(); 613 682 #endif /* __INSURE__ */ 614 683 615 684 return sts; 616 685 } 617 686 … … 626 695 Py_GetArgcArgv(int *argc, char ***argv) 627 696 { 628 629 697 *argc = orig_argc; 698 *argv = orig_argv; 630 699 } 631 700
Note:
See TracChangeset
for help on using the changeset viewer.