Skip to content

Commit 9407316

Browse files
committed
patch 8.0.1451: difficult to set the python home directories properly
Problem: It is difficult to set the python home directory properly for Python 2.7 and 3.5 since both use $PYTHONHOME. Solution: Add the 'pythonhome' and 'pythonthreehome' options. (Kazuki Sakamoto, closes #1266)
1 parent 1dd45fb commit 9407316

8 files changed

Lines changed: 86 additions & 6 deletions

File tree

runtime/doc/options.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5965,6 +5965,20 @@ A jump table for the options with a short description can be found at |Q_op|.
59655965
DYNAMIC_PYTHON_DLL, which was specified at compile time.
59665966
Environment variables are expanded |:set_env|.
59675967
This option cannot be set from a |modeline| or in the |sandbox|, for
5968+
security reasons.
5969+
5970+
*'pythonhome'*
5971+
'pythonhome' string (default "")
5972+
global
5973+
{not in Vi}
5974+
{only available when compiled with the |+python/dyn|
5975+
feature}
5976+
Specifies the name of the Python 2.x home directory. When 'pythonhome'
5977+
and the PYTHONHOME environment variable are not set, PYTHON_HOME,
5978+
which was specified at compile time, will be used for the Python 2.x
5979+
home directory.
5980+
Environment variables are expanded |:set_env|.
5981+
This option cannot be set from a |modeline| or in the |sandbox|, for
59685982
security reasons.
59695983

59705984
*'pythonthreedll'*
@@ -5977,6 +5991,20 @@ A jump table for the options with a short description can be found at |Q_op|.
59775991
DYNAMIC_PYTHON3_DLL, which was specified at compile time.
59785992
Environment variables are expanded |:set_env|.
59795993
This option cannot be set from a |modeline| or in the |sandbox|, for
5994+
security reasons.
5995+
5996+
*'pythonthreehome'*
5997+
'pythonthreehome' string (default "")
5998+
global
5999+
{not in Vi}
6000+
{only available when compiled with the |+python3/dyn|
6001+
feature}
6002+
Specifies the name of the Python 3 home directory. When
6003+
'pythonthreehome' and the PYTHONHOME environment variable are not set,
6004+
PYTHON3_HOME, which was specified at compile time, will be used for
6005+
the Python 3 home directory.
6006+
Environment variables are expanded |:set_env|.
6007+
This option cannot be set from a |modeline| or in the |sandbox|, for
59806008
security reasons.
59816009

59826010
*'pyxversion'* *'pyx'*

runtime/doc/quickref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ Short explanation of each option: *option-list*
839839
'prompt' 'prompt' enable prompt in Ex mode
840840
'pumheight' 'ph' maximum height of the popup menu
841841
'pythondll' name of the Python 2 dynamic library
842+
'pythonhome' name of the Python 2 home directory
842843
'pythonthreedll' name of the Python 3 dynamic library
844+
'pythonthreehome' name of the Python 3 home directory
843845
'pyxversion' 'pyx' Python version used for pyx* commands
844846
'quoteescape' 'qe' escape characters used in a string
845847
'readonly' 'ro' disallow writing the buffer

runtime/optwin.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,10 +1354,18 @@ if exists("&pythondll")
13541354
call append("$", "pythondll\tname of the Python 2 dynamic library")
13551355
call <SID>OptionG("pythondll", &pythondll)
13561356
endif
1357+
if exists("&pythonhome")
1358+
call append("$", "pythonhome\tname of the Python 2 home directory")
1359+
call <SID>OptionG("pythonhome", &pythonhome)
1360+
endif
13571361
if exists("&pythonthreedll")
13581362
call append("$", "pythonthreedll\tname of the Python 3 dynamic library")
13591363
call <SID>OptionG("pythonthreedll", &pythonthreedll)
13601364
endif
1365+
if exists("&pythonthreehome")
1366+
call append("$", "pythonthreehome\tname of the Python 3 home directory")
1367+
call <SID>OptionG("pythonthreehome", &pythonthreehome)
1368+
endif
13611369
if exists("&rubydll")
13621370
call append("$", "rubydll\tname of the Ruby dynamic library")
13631371
call <SID>OptionG("rubydll", &rubydll)

src/if_python.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@ python_loaded(void)
912912
}
913913
#endif
914914

915+
static char *py_home_buf = NULL;
916+
915917
static int
916918
Python_Init(void)
917919
{
@@ -929,10 +931,15 @@ Python_Init(void)
929931
}
930932
#endif
931933

934+
if (*p_pyhome != NUL)
935+
{
936+
/* The string must not change later, make a copy in static memory. */
937+
py_home_buf = (char *)vim_strsave(p_pyhome);
938+
if (py_home_buf != NULL)
939+
Py_SetPythonHome(py_home_buf);
940+
}
932941
#ifdef PYTHON_HOME
933-
# ifdef DYNAMIC_PYTHON
934-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
935-
# endif
942+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
936943
Py_SetPythonHome(PYTHON_HOME);
937944
#endif
938945

src/if_python3.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ python3_loaded(void)
842842
}
843843
#endif
844844

845+
static wchar_t *py_home_buf = NULL;
846+
845847
static int
846848
Python3_Init(void)
847849
{
@@ -857,11 +859,18 @@ Python3_Init(void)
857859

858860
init_structs();
859861

862+
if (*p_py3home != NUL)
863+
{
864+
size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
860865

866+
/* The string must not change later, make a copy in static memory. */
867+
py_home_buf = (wchar_t *)alloc(len * sizeof(wchar_t));
868+
if (py_home_buf != NULL && mbstowcs(
869+
py_home_buf, (char *)p_py3home, len) != (size_t)-1)
870+
Py_SetPythonHome(py_home_buf);
871+
}
861872
#ifdef PYTHON3_HOME
862-
# ifdef DYNAMIC_PYTHON3
863-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
864-
# endif
873+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
865874
Py_SetPythonHome(PYTHON3_HOME);
866875
#endif
867876

src/option.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,15 @@ static struct vimoption options[] =
22462246
#else
22472247
(char_u *)NULL, PV_NONE,
22482248
{(char_u *)NULL, (char_u *)0L}
2249+
#endif
2250+
SCRIPTID_INIT},
2251+
{"pythonthreehome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2252+
#if defined(FEAT_PYTHON3)
2253+
(char_u *)&p_py3home, PV_NONE,
2254+
{(char_u *)"", (char_u *)0L}
2255+
#else
2256+
(char_u *)NULL, PV_NONE,
2257+
{(char_u *)NULL, (char_u *)0L}
22492258
#endif
22502259
SCRIPTID_INIT},
22512260
{"pythondll", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
@@ -2255,6 +2264,15 @@ static struct vimoption options[] =
22552264
#else
22562265
(char_u *)NULL, PV_NONE,
22572266
{(char_u *)NULL, (char_u *)0L}
2267+
#endif
2268+
SCRIPTID_INIT},
2269+
{"pythonhome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2270+
#if defined(FEAT_PYTHON)
2271+
(char_u *)&p_pyhome, PV_NONE,
2272+
{(char_u *)"", (char_u *)0L}
2273+
#else
2274+
(char_u *)NULL, PV_NONE,
2275+
{(char_u *)NULL, (char_u *)0L}
22582276
#endif
22592277
SCRIPTID_INIT},
22602278
{"pyxversion", "pyx", P_NUM|P_VI_DEF|P_SECURE,

src/option.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,15 @@ EXTERN char_u *p_perldll; /* 'perldll' */
696696
#if defined(DYNAMIC_PYTHON3)
697697
EXTERN char_u *p_py3dll; /* 'pythonthreedll' */
698698
#endif
699+
#ifdef FEAT_PYTHON3
700+
EXTERN char_u *p_py3home; /* 'pythonthreehome' */
701+
#endif
699702
#if defined(DYNAMIC_PYTHON)
700703
EXTERN char_u *p_pydll; /* 'pythondll' */
701704
#endif
705+
#ifdef FEAT_PYTHON
706+
EXTERN char_u *p_pyhome; /* 'pythonhome' */
707+
#endif
702708
#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
703709
EXTERN long p_pyx; /* 'pyxversion' */
704710
#endif

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1451,
774776
/**/
775777
1450,
776778
/**/

0 commit comments

Comments
 (0)