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

Side by Side Diff: PC/getpathp.c

Issue 3871: cross and native build of python for mingw32 with distutils
Patch Set: Created 7 years, 4 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:
View unified diff | Download patch
« no previous file with comments | « Objects/exceptions.c ('k') | PC/msvcrtmodule.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Return the initial module search path. */ 2 /* Return the initial module search path. */
3 /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */ 3 /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
4 4
5 /* ---------------------------------------------------------------- 5 /* ----------------------------------------------------------------
6 PATH RULES FOR WINDOWS: 6 PATH RULES FOR WINDOWS:
7 This describes how sys.path is formed on Windows. It describes the 7 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these 8 functionality, not the implementation (ie, the order in which these
9 are actually fetched is different) 9 are actually fetched is different)
10 10
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 * with a semicolon separated path prior to calling Py_Initialize. 87 * with a semicolon separated path prior to calling Py_Initialize.
88 */ 88 */
89 89
90 #ifndef LANDMARK 90 #ifndef LANDMARK
91 #define LANDMARK L"lib\\os.py" 91 #define LANDMARK L"lib\\os.py"
92 #endif 92 #endif
93 93
94 static wchar_t prefix[MAXPATHLEN+1]; 94 static wchar_t prefix[MAXPATHLEN+1];
95 static wchar_t progpath[MAXPATHLEN+1]; 95 static wchar_t progpath[MAXPATHLEN+1];
96 static wchar_t dllpath[MAXPATHLEN+1]; 96 static wchar_t dllpath[MAXPATHLEN+1];
97 static wchar_t mingw_dynload_path[MAXPATHLEN+1];
97 static wchar_t *module_search_path = NULL; 98 static wchar_t *module_search_path = NULL;
98 99
99 100
100 static int 101 static int
101 is_sep(wchar_t ch) /* determine if "ch" is a separator character */ 102 is_sep(wchar_t ch) /* determine if "ch" is a separator character */
102 { 103 {
103 #ifdef ALTSEP 104 #ifdef ALTSEP
104 return ch == SEP || ch == ALTSEP; 105 return ch == SEP || ch == ALTSEP;
105 #else 106 #else
106 return ch == SEP; 107 return ch == SEP;
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 wcsncpy(value, tok, MAXPATHLEN); 462 wcsncpy(value, tok, MAXPATHLEN);
462 result = 1; 463 result = 1;
463 break; 464 break;
464 } 465 }
465 } 466 }
466 } 467 }
467 } 468 }
468 } 469 }
469 } 470 }
470 return result; 471 return result;
472 }
473
474 // Threadsafe mingw path conversion call
475 // Returns true if path found and converted.
476 int mingw_posix2win_path(wchar_t *mingwpath, wchar_t *destination, int size)
477 {
478 FILE *fp;
479 int status;
480 wchar_t cmd[MAXPATHLEN+32]={0};
481 wchar_t results[MAXPATHLEN+1]={0};
482 wchar_t mingw_lib64_path[MAXPATHLEN+1]={0};
483
484 // not quite sure yet why swprintf with size fails. will have
485 // to use default swprintf for now.
486 swprintf(cmd, L"bash -c \"cd %ls 2>&1 && pwd -W\"", mingwpath);
487
488 /* Open the command for reading. */
489 fp = wpopen(cmd, L"r");
490
491 if (fp == NULL) {
492 return 0;
493 } else {
494 /* Read the output a line at a time - output it. */
495 while (fgetws(results, sizeof(results)-1, fp) != NULL) {
496 if (wcsstr(results, L"No such file")==NULL) {
497 results[wcslen(results)-1]='\0';
498 wcsncat(destination, results, size);
499 } else {
500 return 0;
501 }
502 }
503
504 /* close */
505 pclose(fp);
506 }
507
508 return 1;
509 }
510
511 void get_mingw_path(wchar_t *destination, int size)
512 {
513 static wchar_t mingw_path[MAXPATHLEN+1]={0};
514
515 //mingw64 path is mounted under /mingw in msys
516 if (wcslen(mingw_path)==0)
517 if (!mingw_posix2win_path(L"/mingw", mingw_path, sizeof(mingw_pa th)))
518 mingw_posix2win_path(L"/", mingw_path, sizeof(mingw_path ));
519
520 if (wcslen(mingw_path)>=0) {
521 wcsncat(destination, mingw_path, size);
522 }
523 }
524
525 void get_mingw_lib_path(wchar_t *destination, int size)
526 {
527 wchar_t mingw_base_path[MAXPATHLEN+1]={0};
528
529 get_mingw_path(mingw_base_path, sizeof(mingw_base_path));
530
531 if (wcslen(mingw_base_path)>=0) {
532 wcsncpy(destination, mingw_base_path, size);
533 wcsncat(destination, L"/lib", size);
534 }
535 }
536
537 void get_mingw_os_test_path(wchar_t *destination, int size)
538 {
539 wchar_t mingw_py_path[MAXPATHLEN+1]={0};
540
541 swprintf(mingw_py_path, L"lib/python%d.%d/", PY_MAJOR_VERSION,PY_MINOR_VERSI ON);
542
543 wcsncpy(destination, mingw_py_path, size);
544 wcsncat(destination, L"os.py", size);
545 }
546
547 void get_mingw_python_path(wchar_t *destination, int size)
548 {
549 wchar_t mingw_lib_path[MAXPATHLEN+1]={0};
550 wchar_t mingw_py_path[MAXPATHLEN+1]={0};
551
552 get_mingw_lib_path(mingw_lib_path, sizeof(mingw_lib_path));
553
554 swprintf(mingw_py_path, L"/python%d.%d", PY_MAJOR_VERSION,PY_MINOR_VERSION);
555
556 if (wcslen(mingw_lib_path)>=0) {
557 wcsncpy(destination, mingw_lib_path, size);
558 wcsncat(destination, mingw_py_path, size);
559 }
560 }
561
562 void get_mingw_libdynload_path(wchar_t *destination, int size)
563 {
564 wchar_t mingw_lib_path[MAXPATHLEN+1]={0};
565
566 get_mingw_python_path(mingw_lib_path, sizeof(mingw_lib_path));
567
568 if (wcslen(mingw_lib_path)>=0) {
569 wcsncpy(destination, mingw_lib_path, size);
570 wcsncat(destination, L"/lib-dynload", size);
571 }
471 } 572 }
472 573
473 static void 574 static void
474 calculate_path(void) 575 calculate_path(void)
475 { 576 {
476 wchar_t argv0_path[MAXPATHLEN+1]; 577 wchar_t argv0_path[MAXPATHLEN+1];
477 wchar_t *buf; 578 wchar_t *buf;
478 size_t bufsz; 579 size_t bufsz;
479 wchar_t *pythonhome = Py_GetPythonHome(); 580 wchar_t *pythonhome = Py_GetPythonHome();
480 wchar_t *envpath = NULL; 581 wchar_t *envpath = NULL;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 if (len > 4) { 663 if (len > 4) {
563 zip_path[len-3] = 'z'; /* change ending to "zip" */ 664 zip_path[len-3] = 'z'; /* change ending to "zip" */
564 zip_path[len-2] = 'i'; 665 zip_path[len-2] = 'i';
565 zip_path[len-1] = 'p'; 666 zip_path[len-1] = 'p';
566 } 667 }
567 else { 668 else {
568 zip_path[0] = 0; 669 zip_path[0] = 0;
569 } 670 }
570 671
571 skiphome = pythonhome==NULL ? 0 : 1; 672 skiphome = pythonhome==NULL ? 0 : 1;
673
674 # ifdef __MINGW32__
675 wchar_t mingw_lib_path[MAXPATHLEN+1]={0};
676 get_mingw_lib_path(mingw_lib_path, sizeof(mingw_lib_path));
677
678 wchar_t mingw_python_path[MAXPATHLEN+1]={0};
679 get_mingw_python_path(mingw_python_path, sizeof(mingw_python_path));
680
681 get_mingw_libdynload_path(mingw_dynload_path, sizeof(mingw_dynload_path) );
682 # endif
683
572 #ifdef Py_ENABLE_SHARED 684 #ifdef Py_ENABLE_SHARED
573 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); 685 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
574 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); 686 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
575 #endif 687 #endif
576 /* We only use the default relative PYTHONPATH if we havent 688 /* We only use the default relative PYTHONPATH if we havent
577 anything better to use! */ 689 anything better to use! */
578 skipdefault = envpath!=NULL || pythonhome!=NULL || \ 690 skipdefault = envpath!=NULL || pythonhome!=NULL || \
579 machinepath!=NULL || userpath!=NULL; 691 machinepath!=NULL || userpath!=NULL || \
692 (wcslen(mingw_lib_path)>=0);
580 #endif 693 #endif
581 694
582 /* We need to construct a path from the following parts. 695 /* We need to construct a path from the following parts.
583 (1) the PYTHONPATH environment variable, if set; 696 (1) the PYTHONPATH environment variable, if set;
584 (2) for Win32, the zip archive file path; 697 (2) for Win32, the zip archive file path;
585 (3) for Win32, the machinepath and userpath, if set; 698 (3) for Win32, the machinepath and userpath, if set;
586 (4) the PYTHONPATH config macro, with the leading "." 699 (4) the PYTHONPATH config macro, with the leading "."
587 of each component replaced with pythonhome, if set; 700 of each component replaced with pythonhome, if set;
588 (5) the directory containing the executable (argv0_path). 701 (5) the directory containing the executable (argv0_path).
589 The length calculation calculates #4 first. 702 The length calculation calculates #4 first.
590 Extra rules: 703 Extra rules:
591 - If PYTHONHOME is set (in any way) item (3) is ignored. 704 - If PYTHONHOME is set (in any way) item (3) is ignored.
592 - If registry values are used, (4) and (5) are ignored. 705 - If registry values are used, (4) and (5) are ignored.
593 */ 706 */
594 707
595 /* Calculate size of return buffer */ 708 /* Calculate size of return buffer */
596 if (pythonhome != NULL) { 709 if (pythonhome != NULL) {
597 wchar_t *p; 710 wchar_t *p;
598 bufsz = 1; 711 bufsz = 1;
599 for (p = PYTHONPATH; *p; p++) { 712 for (p = PYTHONPATH; *p; p++) {
600 if (*p == DELIM) 713 if (*p == DELIM)
601 bufsz++; /* number of DELIM plus one */ 714 bufsz++; /* number of DELIM plus one */
602 } 715 }
603 bufsz *= wcslen(pythonhome); 716 bufsz *= wcslen(pythonhome);
604 } 717 }
605 else 718 else
606 bufsz = 0; 719 bufsz = 0;
720
607 bufsz += wcslen(PYTHONPATH) + 1; 721 bufsz += wcslen(PYTHONPATH) + 1;
608 bufsz += wcslen(argv0_path) + 1; 722 bufsz += wcslen(argv0_path) + 1;
723
609 #ifdef MS_WINDOWS 724 #ifdef MS_WINDOWS
610 if (userpath) 725 if (userpath)
611 bufsz += wcslen(userpath) + 1; 726 bufsz += wcslen(userpath) + 1;
612 if (machinepath) 727 if (machinepath)
613 bufsz += wcslen(machinepath) + 1; 728 bufsz += wcslen(machinepath) + 1;
614 bufsz += wcslen(zip_path) + 1; 729 bufsz += wcslen(zip_path) + 1;
730
731 # ifdef __MINGW32__
732 if (wcslen(mingw_lib_path)>=0)
733 bufsz += wcslen(mingw_lib_path) + 1;
734 if (wcslen(mingw_python_path)>=0)
735 bufsz += wcslen(mingw_python_path) + 1;
736 if (wcslen(mingw_dynload_path)>=0)
737 bufsz += wcslen(mingw_dynload_path) + 1;
738 # endif
739
615 #endif 740 #endif
741
616 if (envpath != NULL) 742 if (envpath != NULL)
617 bufsz += wcslen(envpath) + 1; 743 bufsz += wcslen(envpath) + 1;
618 744
619 module_search_path = buf = malloc(bufsz*sizeof(wchar_t)); 745 module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
746
620 if (buf == NULL) { 747 if (buf == NULL) {
621 /* We can't exit, so print a warning and limp along */ 748 /* We can't exit, so print a warning and limp along */
622 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); 749 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
623 if (envpath) { 750 if (envpath) {
624 fprintf(stderr, "Using environment $PYTHONPATH.\n"); 751 fprintf(stderr, "Using environment $PYTHONPATH.\n");
625 module_search_path = envpath; 752 module_search_path = envpath;
626 } 753 }
627 else { 754 else {
628 fprintf(stderr, "Using default static path.\n"); 755 fprintf(stderr, "Using default static path.\n");
629 module_search_path = PYTHONPATH; 756 module_search_path = PYTHONPATH;
(...skipping 23 matching lines...) Expand all
653 buf = wcschr(buf, L'\0'); 780 buf = wcschr(buf, L'\0');
654 *buf++ = DELIM; 781 *buf++ = DELIM;
655 free(userpath); 782 free(userpath);
656 } 783 }
657 if (machinepath) { 784 if (machinepath) {
658 wcscpy(buf, machinepath); 785 wcscpy(buf, machinepath);
659 buf = wcschr(buf, L'\0'); 786 buf = wcschr(buf, L'\0');
660 *buf++ = DELIM; 787 *buf++ = DELIM;
661 free(machinepath); 788 free(machinepath);
662 } 789 }
790 #ifdef __MINGW32__
791 if (search_for_prefix(argv0_path, LANDMARK) == 0)
792 {
793 if (wcslen(mingw_lib_path)>0) {
794 wcscpy(buf, mingw_lib_path);
795 buf = wcschr(buf, L'\0');
796 *buf++ = DELIM;
797 }
798
799 if (wcslen(mingw_python_path)>0) {
800 wcscpy(buf, mingw_python_path);
801 buf = wcschr(buf, L'\0');
802 *buf++ = DELIM;
803 }
804
805 if (wcslen(mingw_dynload_path)>0) {
806 wcscpy(buf, mingw_dynload_path);
807 buf = wcschr(buf, L'\0');
808 *buf++ = DELIM;
809 }
810
811 wchar_t mingw_path[MAXPATHLEN+1]={0};
812 wchar_t os_test_path[MAXPATHLEN+1]={0};
813
814 get_mingw_path(mingw_path, sizeof(mingw_path));
815 get_mingw_os_test_path(os_test_path, sizeof(os_test_path));
816
817 search_for_prefix(mingw_path, os_test_path);
818 }
819 #endif
663 if (pythonhome == NULL) { 820 if (pythonhome == NULL) {
664 if (!skipdefault) { 821 if (!skipdefault) {
665 wcscpy(buf, PYTHONPATH); 822 wcscpy(buf, PYTHONPATH);
666 buf = wcschr(buf, L'\0'); 823 buf = wcschr(buf, L'\0');
667 } 824 }
668 } 825 }
669 #else 826 #else
670 if (pythonhome == NULL) { 827 if (pythonhome == NULL) {
671 wcscpy(buf, PYTHONPATH); 828 wcscpy(buf, PYTHONPATH);
672 buf = wcschr(buf, L'\0'); 829 buf = wcschr(buf, L'\0');
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 Py_GetPrefix(void) 928 Py_GetPrefix(void)
772 { 929 {
773 if (!module_search_path) 930 if (!module_search_path)
774 calculate_path(); 931 calculate_path();
775 return prefix; 932 return prefix;
776 } 933 }
777 934
778 wchar_t * 935 wchar_t *
779 Py_GetExecPrefix(void) 936 Py_GetExecPrefix(void)
780 { 937 {
938 if (wcslen(mingw_dynload_path)>0)
939 return mingw_dynload_path;
940
781 return Py_GetPrefix(); 941 return Py_GetPrefix();
782 } 942 }
783 943
784 wchar_t * 944 wchar_t *
785 Py_GetProgramFullPath(void) 945 Py_GetProgramFullPath(void)
786 { 946 {
787 if (!module_search_path) 947 if (!module_search_path)
788 calculate_path(); 948 calculate_path();
789 return progpath; 949 return progpath;
790 } 950 }
(...skipping 25 matching lines...) Expand all
816 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 976 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
817 if (hPython3 != NULL) 977 if (hPython3 != NULL)
818 return 1; 978 return 1;
819 979
820 /* Check sys.prefix\DLLs\python3.dll */ 980 /* Check sys.prefix\DLLs\python3.dll */
821 wcscpy(py3path, Py_GetPrefix()); 981 wcscpy(py3path, Py_GetPrefix());
822 wcscat(py3path, L"\\DLLs\\python3.dll"); 982 wcscat(py3path, L"\\DLLs\\python3.dll");
823 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 983 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
824 return hPython3 != NULL; 984 return hPython3 != NULL;
825 } 985 }
OLDNEW
« no previous file with comments | « Objects/exceptions.c ('k') | PC/msvcrtmodule.c » ('j') | no next file with comments »

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