Skip to content

Commit 16974f8

Browse files
committed
Introduce pythonhome and pythonthreehome options
It is really difficult to set Python home directory properly for Python 2.7 and 3.5 in .vimrc at the same time since both versions use `$PYTHONHOME` environment variable. Introduce `pythonhome` and `pythonthreehome` options in order to set Python home directory via `Py_SetPythonHome` API for both versions.
1 parent f9248a4 commit 16974f8

File tree

7 files changed

+75
-10
lines changed

7 files changed

+75
-10
lines changed

runtime/doc/options.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5921,6 +5921,20 @@ A jump table for the options with a short description can be found at |Q_op|.
59215921
DYNAMIC_PYTHON_DLL, which was specified at compile time.
59225922
Environment variables are expanded |:set_env|.
59235923
This option cannot be set from a |modeline| or in the |sandbox|, for
5924+
security reasons.
5925+
5926+
*'pythonhome'*
5927+
'pythonhome' string (default "")
5928+
global
5929+
{not in Vi}
5930+
{only available when compiled with the |+python/dyn|
5931+
feature}
5932+
Specifies the name of the Python 2.x home directory. When 'pythonhome'
5933+
and the PYTHONHOME environment variable are not set, PYTHON_HOME,
5934+
which was specified at compile time, will be used for the Python 2.x
5935+
home directory.
5936+
Environment variables are expanded |:set_env|.
5937+
This option cannot be set from a |modeline| or in the |sandbox|, for
59245938
security reasons.
59255939

59265940
*'pythonthreedll'*
@@ -5933,6 +5947,20 @@ A jump table for the options with a short description can be found at |Q_op|.
59335947
DYNAMIC_PYTHON3_DLL, which was specified at compile time.
59345948
Environment variables are expanded |:set_env|.
59355949
This option cannot be set from a |modeline| or in the |sandbox|, for
5950+
security reasons.
5951+
5952+
*'pythonthreehome'*
5953+
'pythonthreehome' string (default "")
5954+
global
5955+
{not in Vi}
5956+
{only available when compiled with the |+python3/dyn|
5957+
feature}
5958+
Specifies the name of the Python 3 home directory. When
5959+
'pythonthreehome' and the PYTHONHOME environment variable are not set,
5960+
PYTHON3_HOME, which was specified at compile time, will be used for
5961+
the Python 3 home directory.
5962+
Environment variables are expanded |:set_env|.
5963+
This option cannot be set from a |modeline| or in the |sandbox|, for
59365964
security reasons.
59375965

59385966
*'quoteescape'* *'qe'*

runtime/doc/quickref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,9 @@ Short explanation of each option: *option-list*
840840
'prompt' 'prompt' enable prompt in Ex mode
841841
'pumheight' 'ph' maximum height of the popup menu
842842
'pythondll' name of the Python 2 dynamic library
843+
'pythonhome' name of the Python 2 home directory
843844
'pythonthreedll' name of the Python 3 dynamic library
845+
'pythonthreehome' name of the Python 3 home directory
844846
'quoteescape' 'qe' escape characters used in a string
845847
'readonly' 'ro' disallow writing the buffer
846848
'redrawtime' 'rdt' timeout for 'hlsearch' and |:match| highlighting

runtime/optwin.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,18 @@ if exists("&pythondll")
13451345
call append("$", "pythondll\tname of the Python 2 dynamic library")
13461346
call <SID>OptionG("pythondll", &pythondll)
13471347
endif
1348+
if exists("&pythonhome")
1349+
call append("$", "pythonhome\tname of the Python 2 home directory")
1350+
call <SID>OptionG("pythonhome", &pythonhome)
1351+
endif
13481352
if exists("&pythonthreedll")
13491353
call append("$", "pythonthreedll\tname of the Python 3 dynamic library")
13501354
call <SID>OptionG("pythonthreedll", &pythonthreedll)
13511355
endif
1356+
if exists("&pythonthreehome")
1357+
call append("$", "pythonthreehome\tname of the Python 3 home directory")
1358+
call <SID>OptionG("pythonthreehome", &pythonthreehome)
1359+
endif
13521360
if exists("&rubydll")
13531361
call append("$", "rubydll\tname of the Ruby dynamic library")
13541362
call <SID>OptionG("rubydll", &rubydll)

src/if_python.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,13 +933,17 @@ Python_Init(void)
933933
EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
934934
goto fail;
935935
}
936-
#endif
937936

938-
#ifdef PYTHON_HOME
939-
# ifdef DYNAMIC_PYTHON
940-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
941-
# endif
937+
if (p_pyhome && *p_pyhome != '\0')
938+
Py_SetPythonHome((char *)p_pyhome);
939+
# ifdef PYTHON_HOME
940+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
942941
Py_SetPythonHome(PYTHON_HOME);
942+
# endif
943+
#else
944+
# ifdef PYTHON_HOME
945+
Py_SetPythonHome(PYTHON_HOME);
946+
# endif
943947
#endif
944948

945949
init_structs();

src/if_python3.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,25 @@ Python3_Init(void)
860860

861861
init_structs();
862862

863-
864-
#ifdef PYTHON3_HOME
865-
# ifdef DYNAMIC_PYTHON3
866-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
867-
# endif
863+
#ifdef DYNAMIC_PYTHON3
864+
if (p_py3home && *p_py3home != '\0')
865+
{
866+
int len;
867+
wchar_t *buf;
868+
len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
869+
buf = (wchar_t *)alloc(len * sizeof(wchar_t));
870+
if (buf && mbstowcs(buf, (char *)p_py3home, len) != (size_t)-1)
871+
Py_SetPythonHome(buf);
872+
vim_free(buf);
873+
}
874+
# ifdef PYTHON3_HOME
875+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
868876
Py_SetPythonHome(PYTHON3_HOME);
877+
# endif
878+
#else
879+
# ifdef PYTHON3_HOME
880+
Py_SetPythonHome(PYTHON3_HOME);
881+
# endif
869882
#endif
870883

871884
PyImport_AppendInittab("vim", Py3Init_vim);

src/option.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,12 +2187,20 @@ static struct vimoption options[] =
21872187
(char_u *)&p_py3dll, PV_NONE,
21882188
{(char_u *)DYNAMIC_PYTHON3_DLL, (char_u *)0L}
21892189
SCRIPTID_INIT},
2190+
{"pythonthreehome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2191+
(char_u *)&p_py3home, PV_NONE,
2192+
{(char_u *)NULL, (char_u *)0L}
2193+
SCRIPTID_INIT},
21902194
#endif
21912195
#if defined(DYNAMIC_PYTHON)
21922196
{"pythondll", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
21932197
(char_u *)&p_pydll, PV_NONE,
21942198
{(char_u *)DYNAMIC_PYTHON_DLL, (char_u *)0L}
21952199
SCRIPTID_INIT},
2200+
{"pythonhome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2201+
(char_u *)&p_pyhome, PV_NONE,
2202+
{(char_u *)NULL, (char_u *)0L}
2203+
SCRIPTID_INIT},
21962204
#endif
21972205
{"quoteescape", "qe", P_STRING|P_ALLOCED|P_VI_DEF,
21982206
#ifdef FEAT_TEXTOBJ

src/option.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,11 @@ EXTERN char_u *p_perldll; /* 'perldll' */
706706
#endif
707707
#if defined(DYNAMIC_PYTHON3)
708708
EXTERN char_u *p_py3dll; /* 'pythonthreedll' */
709+
EXTERN char_u *p_py3home; /* 'pythonthreehome' */
709710
#endif
710711
#if defined(DYNAMIC_PYTHON)
711712
EXTERN char_u *p_pydll; /* 'pythondll' */
713+
EXTERN char_u *p_pyhome; /* 'pythonhome' */
712714
#endif
713715
#ifdef FEAT_RELTIME
714716
EXTERN long p_rdt; /* 'redrawtime' */

0 commit comments

Comments
 (0)