Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(164417)

Delta Between Two Patch Sets: Modules/main.c

Issue 18338: python --version should send output to STDOUT
Left Patch Set: Created 6 years, 5 months ago
Right Patch Set: Created 6 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_cmd_line.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Python interpreter main program */ 1 /* Python interpreter main program */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "osdefs.h" 4 #include "osdefs.h"
5 5
6 #include <locale.h> 6 #include <locale.h>
7 7
8 #ifdef __VMS 8 #ifdef __VMS
9 #error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" 9 #error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
10 #include <unixlib.h> 10 #include <unixlib.h>
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 _PyOS_ResetGetOpt(); 384 _PyOS_ResetGetOpt();
385 385
386 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { 386 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
387 if (c == 'c') { 387 if (c == 'c') {
388 size_t len; 388 size_t len;
389 /* -c is the last option; following arguments 389 /* -c is the last option; following arguments
390 that look like options are left for the 390 that look like options are left for the
391 command to interpret. */ 391 command to interpret. */
392 392
393 len = wcslen(_PyOS_optarg) + 1 + 1; 393 len = wcslen(_PyOS_optarg) + 1 + 1;
394 command = (wchar_t *)malloc(sizeof(wchar_t) * len); 394 command = (wchar_t *)PyMem_RawMalloc(sizeof(wchar_t) * len);
395 if (command == NULL) 395 if (command == NULL)
396 Py_FatalError( 396 Py_FatalError(
397 "not enough memory to copy -c argument"); 397 "not enough memory to copy -c argument");
398 wcscpy(command, _PyOS_optarg); 398 wcscpy(command, _PyOS_optarg);
399 command[len - 2] = '\n'; 399 command[len - 2] = '\n';
400 command[len - 1] = 0; 400 command[len - 1] = 0;
401 break; 401 break;
402 } 402 }
403 403
404 if (c == 'm') { 404 if (c == 'm') {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 return usage(2, argv[0]); 493 return usage(2, argv[0]);
494 /*NOTREACHED*/ 494 /*NOTREACHED*/
495 495
496 } 496 }
497 } 497 }
498 498
499 if (help) 499 if (help)
500 return usage(0, argv[0]); 500 return usage(0, argv[0]);
501 501
502 if (version) { 502 if (version) {
503 fprintf(stdout, "Python %s\n", PY_VERSION); 503 printf("Python %s\n", PY_VERSION);
504 return 0; 504 return 0;
505 } 505 }
506 506
507 if (!Py_InspectFlag && 507 if (!Py_InspectFlag &&
508 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') 508 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
509 Py_InspectFlag = 1; 509 Py_InspectFlag = 1;
510 if (!saw_unbuffered_flag && 510 if (!saw_unbuffered_flag &&
511 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') 511 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
512 Py_UnbufferedStdioFlag = 1; 512 Py_UnbufferedStdioFlag = 1;
513 513
514 if (!Py_NoUserSiteDirectory && 514 if (!Py_NoUserSiteDirectory &&
515 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0') 515 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
516 Py_NoUserSiteDirectory = 1; 516 Py_NoUserSiteDirectory = 1;
517 517
518 #ifdef MS_WINDOWS 518 #ifdef MS_WINDOWS
519 if (!Py_IgnoreEnvironmentFlag && (wp = _wgetenv(L"PYTHONWARNINGS")) && 519 if (!Py_IgnoreEnvironmentFlag && (wp = _wgetenv(L"PYTHONWARNINGS")) &&
520 *wp != L'\0') { 520 *wp != L'\0') {
521 wchar_t *buf, *warning; 521 wchar_t *buf, *warning;
522 522
523 buf = (wchar_t *)malloc((wcslen(wp) + 1) * sizeof(wchar_t)); 523 buf = (wchar_t *)PyMem_RawMalloc((wcslen(wp) + 1) * sizeof(wchar_t));
524 if (buf == NULL) 524 if (buf == NULL)
525 Py_FatalError( 525 Py_FatalError(
526 "not enough memory to copy PYTHONWARNINGS"); 526 "not enough memory to copy PYTHONWARNINGS");
527 wcscpy(buf, wp); 527 wcscpy(buf, wp);
528 for (warning = wcstok(buf, L","); 528 for (warning = wcstok(buf, L",");
529 warning != NULL; 529 warning != NULL;
530 warning = wcstok(NULL, L",")) { 530 warning = wcstok(NULL, L",")) {
531 PySys_AddWarnOption(warning); 531 PySys_AddWarnOption(warning);
532 } 532 }
533 free(buf); 533 PyMem_RawFree(buf);
534 } 534 }
535 #else 535 #else
536 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { 536 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
537 char *buf, *oldloc; 537 char *buf, *oldloc;
538 PyObject *unicode; 538 PyObject *unicode;
539 539
540 /* settle for strtok here as there's no one standard 540 /* settle for strtok here as there's no one standard
541 C89 wcstok */ 541 C89 wcstok */
542 buf = (char *)malloc(strlen(p) + 1); 542 buf = (char *)PyMem_RawMalloc(strlen(p) + 1);
543 if (buf == NULL) 543 if (buf == NULL)
544 Py_FatalError( 544 Py_FatalError(
545 "not enough memory to copy PYTHONWARNINGS"); 545 "not enough memory to copy PYTHONWARNINGS");
546 strcpy(buf, p); 546 strcpy(buf, p);
547 oldloc = strdup(setlocale(LC_ALL, NULL)); 547 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
548 setlocale(LC_ALL, ""); 548 setlocale(LC_ALL, "");
549 for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) { 549 for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) {
550 #ifdef __APPLE__ 550 #ifdef __APPLE__
551 /* Use utf-8 on Mac OS X */ 551 /* Use utf-8 on Mac OS X */
552 unicode = PyUnicode_FromString(p); 552 unicode = PyUnicode_FromString(p);
553 #else 553 #else
554 unicode = PyUnicode_DecodeLocale(p, "surrogateescape"); 554 unicode = PyUnicode_DecodeLocale(p, "surrogateescape");
555 #endif 555 #endif
556 if (unicode == NULL) { 556 if (unicode == NULL) {
557 /* ignore errors */ 557 /* ignore errors */
558 PyErr_Clear(); 558 PyErr_Clear();
559 continue; 559 continue;
560 } 560 }
561 PySys_AddWarnOptionUnicode(unicode); 561 PySys_AddWarnOptionUnicode(unicode);
562 Py_DECREF(unicode); 562 Py_DECREF(unicode);
563 } 563 }
564 setlocale(LC_ALL, oldloc); 564 setlocale(LC_ALL, oldloc);
565 free(oldloc); 565 PyMem_RawFree(oldloc);
566 free(buf); 566 PyMem_RawFree(buf);
567 } 567 }
568 #endif 568 #endif
569 569
570 if (command == NULL && module == NULL && _PyOS_optind < argc && 570 if (command == NULL && module == NULL && _PyOS_optind < argc &&
571 wcscmp(argv[_PyOS_optind], L"-") != 0) 571 wcscmp(argv[_PyOS_optind], L"-") != 0)
572 { 572 {
573 #ifdef __VMS 573 #ifdef __VMS
574 filename = decc$translate_vms(argv[_PyOS_optind]); 574 filename = decc$translate_vms(argv[_PyOS_optind]);
575 if (filename == (char *)0 || filename == (char *)-1) 575 if (filename == (char *)0 || filename == (char *)-1)
576 filename = argv[_PyOS_optind]; 576 filename = argv[_PyOS_optind];
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 actual Python executable. This is needed to keep the Finder happy, 626 actual Python executable. This is needed to keep the Finder happy,
627 or rather, to work around Apple's overly strict requirements of 627 or rather, to work around Apple's overly strict requirements of
628 the process name. However, we still need a usable sys.executable, 628 the process name. However, we still need a usable sys.executable,
629 so the actual executable path is passed in an environment variable. 629 so the actual executable path is passed in an environment variable.
630 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap 630 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
631 script. */ 631 script. */
632 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') { 632 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') {
633 wchar_t* buffer; 633 wchar_t* buffer;
634 size_t len = strlen(p) + 1; 634 size_t len = strlen(p) + 1;
635 635
636 buffer = malloc(len * sizeof(wchar_t)); 636 buffer = PyMem_RawMalloc(len * sizeof(wchar_t));
637 if (buffer == NULL) { 637 if (buffer == NULL) {
638 Py_FatalError( 638 Py_FatalError(
639 "not enough memory to copy PYTHONEXECUTABLE"); 639 "not enough memory to copy PYTHONEXECUTABLE");
640 } 640 }
641 641
642 mbstowcs(buffer, p, len); 642 mbstowcs(buffer, p, len);
643 Py_SetProgramName(buffer); 643 Py_SetProgramName(buffer);
644 /* buffer is now handed off - do not free */ 644 /* buffer is now handed off - do not free */
645 } else { 645 } else {
646 #ifdef WITH_NEXT_FRAMEWORK 646 #ifdef WITH_NEXT_FRAMEWORK
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 PyObject *v; 700 PyObject *v;
701 v = PyImport_ImportModule("readline"); 701 v = PyImport_ImportModule("readline");
702 if (v == NULL) 702 if (v == NULL)
703 PyErr_Clear(); 703 PyErr_Clear();
704 else 704 else
705 Py_DECREF(v); 705 Py_DECREF(v);
706 } 706 }
707 707
708 if (command) { 708 if (command) {
709 sts = run_command(command, &cf); 709 sts = run_command(command, &cf);
710 free(command); 710 PyMem_RawFree(command);
711 } else if (module) { 711 } else if (module) {
712 sts = (RunModule(module, 1) != 0); 712 sts = (RunModule(module, 1) != 0);
713 } 713 }
714 else { 714 else {
715 715
716 if (filename == NULL && stdin_is_interactive) { 716 if (filename == NULL && stdin_is_interactive) {
717 Py_InspectFlag = 0; /* do exit on SystemExit */ 717 Py_InspectFlag = 0; /* do exit on SystemExit */
718 RunStartupFile(&cf); 718 RunStartupFile(&cf);
719 RunInteractiveHook(); 719 RunInteractiveHook();
720 } 720 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 void 815 void
816 Py_GetArgcArgv(int *argc, wchar_t ***argv) 816 Py_GetArgcArgv(int *argc, wchar_t ***argv)
817 { 817 {
818 *argc = orig_argc; 818 *argc = orig_argc;
819 *argv = orig_argv; 819 *argv = orig_argv;
820 } 820 }
821 821
822 #ifdef __cplusplus 822 #ifdef __cplusplus
823 } 823 }
824 #endif 824 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+