Skip to content

Commit

Permalink
Add v:python3_version support
Browse files Browse the repository at this point in the history
This variable indicates the version of Python3 that Vim was built
against (PY_VERSION_HEX), and will be useful to check whether the Python
library you are loading in dynamically actually fits it. When built with
stable ABI, it will be the limited ABI version instead
(`Py_LIMITED_API`), which indicates the minimum version of Python 3 the
user should have, rather than the exact match. When stable ABI is used,
we won't be exposing PY_VERSION_HEX in this var because it just doesn't
seem necessary to do so (the whole point of stable ABI is the promise
that it will work across versions), and I don't want to confuse the user
with too many variables.

Also, cleaned up some documentation, and added help tags.
  • Loading branch information
ychin committed Mar 2, 2023
1 parent 3de9f1c commit fdf9462
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
18 changes: 16 additions & 2 deletions runtime/doc/eval.txt
Expand Up @@ -2397,8 +2397,22 @@ v:progpath Contains the command with which Vim was invoked, in a form

*v:python3_version* *python3-version-variable*
v:python3_version
TODO ychin: This needs to be implemented
Read-only.
Version of Python 3 that Vim was built against. When
Python is loaded dynamically (|python-dynamic|), this version
should exactly match the Python library up to the minor
version (e.g. 3.10.2 and 3.10.3 are compatible as the minor
version is "10", whereas 3.9.4 and 3.10.3 are not compatible).
When |python-stable-abi| is used, this will be the minimum Python
version that you can use instead. (e.g. if v:python3_version
indicates 3.9, you can use 3.9, 3.10, or anything above).

This number is encoded as a hex number following Python ABI
versioning conventions. Do the following to have a
human-readable full version in hex: >
echo printf("%08X", v:python3_version)
< You can obtain only the minor version by doing: >
echo and(v:python3_version>>16,0xff)
< Read-only.

*v:register* *register-variable*
v:register The name of the register in effect for the current normal mode
Expand Down
10 changes: 5 additions & 5 deletions runtime/doc/if_pyth.txt
Expand Up @@ -769,19 +769,19 @@ Unix ~
The 'pythondll' or 'pythonthreedll' option can be used to specify the Python
shared library file instead of DYNAMIC_PYTHON_DLL or DYNAMIC_PYTHON3_DLL file
what were specified at compile time. The version of the shared library must
match the Python 2.x or Python 3 version Vim was compiled with unless using
|python3-stable-abi|.
match the Python 2.x or Python 3 version (|v:python3_version|) Vim was
compiled with unless using |python3-stable-abi|.


Stable ABI and mixing Python versions ~
*python-stable* *python-stable-abi* *python3-stable-abi*
Unless Vim was compiled with Stable ABI (only available for Python 3), the
If Vim was not compiled with Stable ABI (only available for Python 3), the
version of the Python shared library must match the version that Vim was
compiled with. Otherwise, mixing versions could result in unexpected crashes
and failures. With Stable ABI, this restriction is relaxed, and any Python 3
library with version of at least |v:python3_version| will work. See
|has-python| for how to check if Stable ABI is supported, or see
|+python3/dyn-stable|.
|has-python| for how to check if Stable ABI is supported, or see if version
output includes |+python3/dyn-stable|.

==============================================================================
10. Python 3 *python3*
Expand Down
6 changes: 6 additions & 0 deletions runtime/doc/tags
Expand Up @@ -1434,6 +1434,7 @@ $quote eval.txt /*$quote*
+python/dyn various.txt /*+python\/dyn*
+python3 various.txt /*+python3*
+python3/dyn various.txt /*+python3\/dyn*
+python3/dyn-stable various.txt /*+python3\/dyn-stable*
+quickfix various.txt /*+quickfix*
+reltime various.txt /*+reltime*
+rightleft various.txt /*+rightleft*
Expand Down Expand Up @@ -9289,6 +9290,8 @@ python-path_hook if_pyth.txt /*python-path_hook*
python-pyeval if_pyth.txt /*python-pyeval*
python-range if_pyth.txt /*python-range*
python-special-path if_pyth.txt /*python-special-path*
python-stable if_pyth.txt /*python-stable*
python-stable-abi if_pyth.txt /*python-stable-abi*
python-strwidth if_pyth.txt /*python-strwidth*
python-tabpage if_pyth.txt /*python-tabpage*
python-tabpages if_pyth.txt /*python-tabpages*
Expand All @@ -9301,6 +9304,8 @@ python.vim syntax.txt /*python.vim*
python2-directory if_pyth.txt /*python2-directory*
python3 if_pyth.txt /*python3*
python3-directory if_pyth.txt /*python3-directory*
python3-stable-abi if_pyth.txt /*python3-stable-abi*
python3-version-variable eval.txt /*python3-version-variable*
python_x if_pyth.txt /*python_x*
python_x-special-comments if_pyth.txt /*python_x-special-comments*
pythonx if_pyth.txt /*pythonx*
Expand Down Expand Up @@ -10622,6 +10627,7 @@ v:prevcount eval.txt /*v:prevcount*
v:profiling eval.txt /*v:profiling*
v:progname eval.txt /*v:progname*
v:progpath eval.txt /*v:progpath*
v:python3_version eval.txt /*v:python3_version*
v:register eval.txt /*v:register*
v:scrollstart eval.txt /*v:scrollstart*
v:searchforward eval.txt /*v:searchforward*
Expand Down
5 changes: 5 additions & 0 deletions src/evalvars.c
Expand Up @@ -157,6 +157,7 @@ static struct vimvar
{VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
};

// shorthand
Expand Down Expand Up @@ -264,6 +265,10 @@ evalvars_init(void)

set_vim_var_dict(VV_COLORNAMES, dict_alloc());

#ifdef FEAT_PYTHON3
set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
#endif

// Default for v:register is not 0 but '"'. This is adjusted once the
// clipboard has been setup by calling reset_reg_var().
set_reg_var(0);
Expand Down
10 changes: 10 additions & 0 deletions src/if_python3.c
Expand Up @@ -2039,4 +2039,14 @@ do_py3eval(char_u *str, typval_T *rettv)
set_ref_in_python3(int copyID)
{
return set_ref_in_py(copyID);
}

int
python3_version()
{
#ifdef USE_LIMITED_API
return Py_LIMITED_API;
#else
return PY_VERSION_HEX;
#endif
}
1 change: 1 addition & 0 deletions src/proto/if_python3.pro
Expand Up @@ -10,4 +10,5 @@ void python3_window_free(win_T *win);
void python3_tabpage_free(tabpage_T *tab);
void do_py3eval(char_u *str, typval_T *rettv);
int set_ref_in_python3(int copyID);
int python3_version();
/* vim: set ft=c : */
3 changes: 2 additions & 1 deletion src/vim.h
Expand Up @@ -2102,7 +2102,8 @@ typedef int sock_T;
#define VV_SIZEOFLONG 103
#define VV_SIZEOFPOINTER 104
#define VV_MAXCOL 105
#define VV_LEN 106 // number of v: vars
#define VV_PYTHON3_VERSION 106
#define VV_LEN 107 // number of v: vars

// used for v_number in VAR_BOOL and VAR_SPECIAL
#define VVAL_FALSE 0L // VAR_BOOL
Expand Down

0 comments on commit fdf9462

Please sign in to comment.