1
1
/* Return the initial module search path. */
2
2
3
3
#include "Python.h"
4
+ #include "internal/pystate.h"
4
5
#include "osdefs.h"
5
6
6
7
#include <sys/types.h>
@@ -114,14 +115,6 @@ extern "C" {
114
115
? _Py_INIT_USER_ERR("cannot decode " #NAME) \
115
116
: _Py_INIT_NO_MEMORY()
116
117
117
-
118
- typedef struct {
119
- wchar_t * prefix ;
120
- wchar_t * exec_prefix ;
121
- wchar_t * program_name ;
122
- wchar_t * module_search_path ;
123
- } PyPathConfig ;
124
-
125
118
typedef struct {
126
119
wchar_t * path_env ; /* PATH environment variable */
127
120
wchar_t * home ; /* PYTHONHOME environment variable */
@@ -142,7 +135,7 @@ typedef struct {
142
135
143
136
static const wchar_t delimiter [2 ] = {DELIM , '\0' };
144
137
static const wchar_t separator [2 ] = {SEP , '\0' };
145
- static PyPathConfig path_config = {. module_search_path = NULL } ;
138
+ static _PyPathConfig _Py_path_config = _PyPathConfig_INIT ;
146
139
147
140
148
141
/* Get file status. Encode the path to the locale encoding. */
@@ -592,10 +585,10 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
592
585
593
586
594
587
static _PyInitError
595
- calculate_program_name (PyCalculatePath * calculate , PyPathConfig * config )
588
+ calculate_program_full_path (PyCalculatePath * calculate , _PyPathConfig * config )
596
589
{
597
- wchar_t program_name [MAXPATHLEN + 1 ];
598
- memset (program_name , 0 , sizeof (program_name ));
590
+ wchar_t program_full_path [MAXPATHLEN + 1 ];
591
+ memset (program_full_path , 0 , sizeof (program_full_path ));
599
592
600
593
#ifdef __APPLE__
601
594
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
@@ -612,7 +605,7 @@ calculate_program_name(PyCalculatePath *calculate, PyPathConfig *config)
612
605
* $PATH isn't exported, you lose.
613
606
*/
614
607
if (wcschr (calculate -> program_name , SEP )) {
615
- wcsncpy (program_name , calculate -> program_name , MAXPATHLEN );
608
+ wcsncpy (program_full_path , calculate -> program_name , MAXPATHLEN );
616
609
}
617
610
#ifdef __APPLE__
618
611
/* On Mac OS X, if a script uses an interpreter of the form
@@ -628,10 +621,10 @@ calculate_program_name(PyCalculatePath *calculate, PyPathConfig *config)
628
621
else if (0 == _NSGetExecutablePath (execpath , & nsexeclength ) &&
629
622
execpath [0 ] == SEP )
630
623
{
631
- size_t r = mbstowcs (program_name , execpath , MAXPATHLEN + 1 );
624
+ size_t r = mbstowcs (program_full_path , execpath , MAXPATHLEN + 1 );
632
625
if (r == (size_t )-1 || r > MAXPATHLEN ) {
633
626
/* Could not convert execpath, or it's too long. */
634
- program_name [0 ] = '\0' ;
627
+ program_full_path [0 ] = '\0' ;
635
628
}
636
629
}
637
630
#endif /* __APPLE__ */
@@ -645,44 +638,44 @@ calculate_program_name(PyCalculatePath *calculate, PyPathConfig *config)
645
638
if (len > MAXPATHLEN ) {
646
639
len = MAXPATHLEN ;
647
640
}
648
- wcsncpy (program_name , path , len );
649
- program_name [len ] = '\0' ;
641
+ wcsncpy (program_full_path , path , len );
642
+ program_full_path [len ] = '\0' ;
650
643
}
651
644
else {
652
- wcsncpy (program_name , path , MAXPATHLEN );
645
+ wcsncpy (program_full_path , path , MAXPATHLEN );
653
646
}
654
647
655
- joinpath (program_name , calculate -> program_name );
656
- if (isxfile (program_name )) {
648
+ joinpath (program_full_path , calculate -> program_name );
649
+ if (isxfile (program_full_path )) {
657
650
break ;
658
651
}
659
652
660
653
if (!delim ) {
661
- program_name [0 ] = L'\0' ;
654
+ program_full_path [0 ] = L'\0' ;
662
655
break ;
663
656
}
664
657
path = delim + 1 ;
665
658
}
666
659
}
667
660
else {
668
- program_name [0 ] = '\0' ;
661
+ program_full_path [0 ] = '\0' ;
669
662
}
670
- if (program_name [0 ] != SEP && program_name [0 ] != '\0' ) {
671
- absolutize (program_name );
663
+ if (program_full_path [0 ] != SEP && program_full_path [0 ] != '\0' ) {
664
+ absolutize (program_full_path );
672
665
}
673
666
674
- config -> program_name = _PyMem_RawWcsdup (program_name );
675
- if (config -> program_name == NULL ) {
667
+ config -> program_full_path = _PyMem_RawWcsdup (program_full_path );
668
+ if (config -> program_full_path == NULL ) {
676
669
return _Py_INIT_NO_MEMORY ();
677
670
}
678
671
return _Py_INIT_OK ();
679
672
}
680
673
681
674
682
675
static _PyInitError
683
- calculate_argv0_path (PyCalculatePath * calculate , const wchar_t * program_name )
676
+ calculate_argv0_path (PyCalculatePath * calculate , const wchar_t * program_full_path )
684
677
{
685
- wcsncpy (calculate -> argv0_path , program_name , MAXPATHLEN );
678
+ wcsncpy (calculate -> argv0_path , program_full_path , MAXPATHLEN );
686
679
calculate -> argv0_path [MAXPATHLEN ] = '\0' ;
687
680
688
681
#ifdef WITH_NEXT_FRAMEWORK
@@ -718,10 +711,10 @@ calculate_argv0_path(PyCalculatePath *calculate, const wchar_t *program_name)
718
711
if (!ismodule (calculate -> argv0_path )) {
719
712
/* We are in the build directory so use the name of the
720
713
executable - we know that the absolute path is passed */
721
- wcsncpy (calculate -> argv0_path , program_name , MAXPATHLEN );
714
+ wcsncpy (calculate -> argv0_path , program_full_path , MAXPATHLEN );
722
715
}
723
716
else {
724
- /* Use the location of the library as the program_name */
717
+ /* Use the location of the library as the program_full_path */
725
718
wcsncpy (calculate -> argv0_path , wbuf , MAXPATHLEN );
726
719
}
727
720
PyMem_RawFree (wbuf );
@@ -730,15 +723,15 @@ calculate_argv0_path(PyCalculatePath *calculate, const wchar_t *program_name)
730
723
731
724
#if HAVE_READLINK
732
725
wchar_t tmpbuffer [MAXPATHLEN + 1 ];
733
- int linklen = _Py_wreadlink (program_name , tmpbuffer , MAXPATHLEN );
726
+ int linklen = _Py_wreadlink (program_full_path , tmpbuffer , MAXPATHLEN );
734
727
while (linklen != -1 ) {
735
728
if (tmpbuffer [0 ] == SEP ) {
736
729
/* tmpbuffer should never be longer than MAXPATHLEN,
737
730
but extra check does not hurt */
738
731
wcsncpy (calculate -> argv0_path , tmpbuffer , MAXPATHLEN );
739
732
}
740
733
else {
741
- /* Interpret relative to program_name */
734
+ /* Interpret relative to program_full_path */
742
735
reduce (calculate -> argv0_path );
743
736
joinpath (calculate -> argv0_path , tmpbuffer );
744
737
}
@@ -819,7 +812,7 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
819
812
static _PyInitError
820
813
calculate_module_search_path (PyCalculatePath * calculate ,
821
814
const wchar_t * prefix , const wchar_t * exec_prefix ,
822
- PyPathConfig * config )
815
+ _PyPathConfig * config )
823
816
{
824
817
/* Calculate size of return buffer */
825
818
size_t bufsz = 0 ;
@@ -955,14 +948,14 @@ calculate_free(PyCalculatePath *calculate)
955
948
956
949
957
950
static _PyInitError
958
- calculate_path_impl (PyCalculatePath * calculate , PyPathConfig * config )
951
+ calculate_path_impl (PyCalculatePath * calculate , _PyPathConfig * config )
959
952
{
960
- _PyInitError err = calculate_program_name (calculate , config );
953
+ _PyInitError err = calculate_program_full_path (calculate , config );
961
954
if (_Py_INIT_FAILED (err )) {
962
955
return err ;
963
956
}
964
957
965
- err = calculate_argv0_path (calculate , config -> program_name );
958
+ err = calculate_argv0_path (calculate , config -> program_full_path );
966
959
if (_Py_INIT_FAILED (err )) {
967
960
return err ;
968
961
}
@@ -1011,7 +1004,7 @@ calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config)
1011
1004
1012
1005
1013
1006
static void
1014
- pathconfig_clear (PyPathConfig * config )
1007
+ pathconfig_clear (_PyPathConfig * config )
1015
1008
{
1016
1009
#define CLEAR (ATTR ) \
1017
1010
do { \
@@ -1021,7 +1014,7 @@ pathconfig_clear(PyPathConfig *config)
1021
1014
1022
1015
CLEAR (config -> prefix );
1023
1016
CLEAR (config -> exec_prefix );
1024
- CLEAR (config -> program_name );
1017
+ CLEAR (config -> program_full_path );
1025
1018
CLEAR (config -> module_search_path );
1026
1019
#undef CLEAR
1027
1020
}
@@ -1032,7 +1025,7 @@ pathconfig_clear(PyPathConfig *config)
1032
1025
_PyInitError
1033
1026
_PyPathConfig_Init (const _PyMainInterpreterConfig * main_config )
1034
1027
{
1035
- if (path_config .module_search_path ) {
1028
+ if (_Py_path_config .module_search_path ) {
1036
1029
/* Already initialized */
1037
1030
return _Py_INIT_OK ();
1038
1031
}
@@ -1045,7 +1038,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1045
1038
goto done ;
1046
1039
}
1047
1040
1048
- PyPathConfig new_path_config ;
1041
+ _PyPathConfig new_path_config ;
1049
1042
memset (& new_path_config , 0 , sizeof (new_path_config ));
1050
1043
1051
1044
err = calculate_path_impl (& calculate , & new_path_config );
@@ -1054,7 +1047,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1054
1047
goto done ;
1055
1048
}
1056
1049
1057
- path_config = new_path_config ;
1050
+ _Py_path_config = new_path_config ;
1058
1051
err = _Py_INIT_OK ();
1059
1052
1060
1053
done :
@@ -1064,8 +1057,13 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1064
1057
1065
1058
1066
1059
static void
1067
- calculate_path (void )
1060
+ pathconfig_global_init (void )
1068
1061
{
1062
+ if (_Py_path_config .module_search_path ) {
1063
+ /* Already initialized */
1064
+ return ;
1065
+ }
1066
+
1069
1067
_PyInitError err ;
1070
1068
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT ;
1071
1069
@@ -1084,7 +1082,7 @@ calculate_path(void)
1084
1082
void
1085
1083
_PyPathConfig_Fini (void )
1086
1084
{
1087
- pathconfig_clear (& path_config );
1085
+ pathconfig_clear (& _Py_path_config );
1088
1086
}
1089
1087
1090
1088
@@ -1093,58 +1091,50 @@ void
1093
1091
Py_SetPath (const wchar_t * path )
1094
1092
{
1095
1093
if (path == NULL ) {
1096
- pathconfig_clear (& path_config );
1094
+ pathconfig_clear (& _Py_path_config );
1097
1095
return ;
1098
1096
}
1099
1097
1100
- PyPathConfig new_config ;
1101
- new_config .program_name = _PyMem_RawWcsdup (Py_GetProgramName ());
1098
+ _PyPathConfig new_config ;
1099
+ new_config .program_full_path = _PyMem_RawWcsdup (Py_GetProgramName ());
1102
1100
new_config .exec_prefix = _PyMem_RawWcsdup (L"" );
1103
1101
new_config .prefix = _PyMem_RawWcsdup (L"" );
1104
1102
new_config .module_search_path = _PyMem_RawWcsdup (path );
1105
1103
1106
- pathconfig_clear (& path_config );
1107
- path_config = new_config ;
1104
+ pathconfig_clear (& _Py_path_config );
1105
+ _Py_path_config = new_config ;
1108
1106
}
1109
1107
1110
1108
1111
1109
wchar_t *
1112
1110
Py_GetPath (void )
1113
1111
{
1114
- if (!path_config .module_search_path ) {
1115
- calculate_path ();
1116
- }
1117
- return path_config .module_search_path ;
1112
+ pathconfig_global_init ();
1113
+ return _Py_path_config .module_search_path ;
1118
1114
}
1119
1115
1120
1116
1121
1117
wchar_t *
1122
1118
Py_GetPrefix (void )
1123
1119
{
1124
- if (!path_config .module_search_path ) {
1125
- calculate_path ();
1126
- }
1127
- return path_config .prefix ;
1120
+ pathconfig_global_init ();
1121
+ return _Py_path_config .prefix ;
1128
1122
}
1129
1123
1130
1124
1131
1125
wchar_t *
1132
1126
Py_GetExecPrefix (void )
1133
1127
{
1134
- if (!path_config .module_search_path ) {
1135
- calculate_path ();
1136
- }
1137
- return path_config .exec_prefix ;
1128
+ pathconfig_global_init ();
1129
+ return _Py_path_config .exec_prefix ;
1138
1130
}
1139
1131
1140
1132
1141
1133
wchar_t *
1142
1134
Py_GetProgramFullPath (void )
1143
1135
{
1144
- if (!path_config .module_search_path ) {
1145
- calculate_path ();
1146
- }
1147
- return path_config .program_name ;
1136
+ pathconfig_global_init ();
1137
+ return _Py_path_config .program_full_path ;
1148
1138
}
1149
1139
1150
1140
#ifdef __cplusplus
0 commit comments