patch 9.1.0844: if_python: no way to pass local vars to python
Problem: if_python: no way to pass local vars to python
Solution: Add locals argument to py3eval(), pyeval() and pyxeval()
(Ben Jackson)
fixes: #8573
closes: #10594
Signed-off-by: Ben Jackson <puremourning@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/evalfunc.c b/src/evalfunc.c
index adae129..2d88668 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -2487,7 +2487,7 @@
ret_dict_number, f_pum_getpos},
{"pumvisible", 0, 0, 0, NULL,
ret_number_bool, f_pumvisible},
- {"py3eval", 1, 1, FEARG_1, arg1_string,
+ {"py3eval", 1, 2, FEARG_1, arg2_string_dict,
ret_any,
#ifdef FEAT_PYTHON3
f_py3eval
@@ -2495,7 +2495,7 @@
NULL
#endif
},
- {"pyeval", 1, 1, FEARG_1, arg1_string,
+ {"pyeval", 1, 2, FEARG_1, arg2_string_dict,
ret_any,
#ifdef FEAT_PYTHON
f_pyeval
@@ -2503,7 +2503,7 @@
NULL
#endif
},
- {"pyxeval", 1, 1, FEARG_1, arg1_string,
+ {"pyxeval", 1, 2, FEARG_1, arg2_string_dict,
ret_any,
#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
f_pyxeval
@@ -9291,18 +9291,35 @@
{
char_u *str;
char_u buf[NUMBUFLEN];
+ dict_T *locals;
if (check_restricted() || check_secure())
return;
- if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_opt_dict_arg(argvars, 1) == FAIL))
return;
if (p_pyx == 0)
p_pyx = 3;
+ if (argvars[1].v_type == VAR_DICT)
+ {
+ locals = argvars[1].vval.v_dict;
+ }
+ else if (argvars[1].v_type != VAR_UNKNOWN)
+ {
+ emsg(_(e_dictionary_required));
+ return;
+ }
+ else
+ {
+ locals = NULL;
+ }
+
str = tv_get_string_buf(&argvars[0], buf);
- do_py3eval(str, rettv);
+ do_py3eval(str, locals, rettv);
}
#endif
@@ -9315,18 +9332,35 @@
{
char_u *str;
char_u buf[NUMBUFLEN];
+ dict_T *locals;
if (check_restricted() || check_secure())
return;
- if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ if (in_vim9script() && (
+ check_for_string_arg(argvars, 0) == FAIL ||
+ check_for_opt_dict_arg(argvars, 1) == FAIL ) )
return;
if (p_pyx == 0)
p_pyx = 2;
+ if (argvars[1].v_type == VAR_DICT)
+ {
+ locals = argvars[1].vval.v_dict;
+ }
+ else if (argvars[1].v_type != VAR_UNKNOWN)
+ {
+ emsg( "Invalid argument: must be dict" );
+ return;
+ }
+ else
+ {
+ locals = NULL;
+ }
+
str = tv_get_string_buf(&argvars[0], buf);
- do_pyeval(str, rettv);
+ do_pyeval(str, locals, rettv);
}
#endif
@@ -9340,7 +9374,9 @@
if (check_restricted() || check_secure())
return;
- if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_opt_dict_arg(argvars, 1) == FAIL))
return;
# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
diff --git a/src/if_py_both.h b/src/if_py_both.h
index 5ba443b..5603ac7 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -328,7 +328,7 @@
#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
typedef void (*rangeinitializer)(void *);
-typedef void (*runner)(const char *, void *
+typedef void (*runner)(const char *, dict_T *, void *
#ifdef PY_CAN_RECURSE
, PyGILState_STATE *
#endif
@@ -6032,7 +6032,7 @@
}
static void
-run_cmd(const char *cmd, void *arg UNUSED
+run_cmd(const char *cmd, dict_T* locals UNUSED, void *arg UNUSED
#ifdef PY_CAN_RECURSE
, PyGILState_STATE *pygilstate UNUSED
#endif
@@ -6057,7 +6057,7 @@
static int code_hdr_len = 30;
static void
-run_do(const char *cmd, void *arg UNUSED
+run_do(const char *cmd, dict_T* locals UNUSED, void *arg UNUSED
#ifdef PY_CAN_RECURSE
, PyGILState_STATE *pygilstate
#endif
@@ -6180,7 +6180,7 @@
}
static void
-run_eval(const char *cmd, void *arg
+run_eval(const char *cmd, dict_T *locals, void *arg
#ifdef PY_CAN_RECURSE
, PyGILState_STATE *pygilstate UNUSED
#endif
@@ -6188,8 +6188,9 @@
{
PyObject *run_ret;
typval_T *rettv = (typval_T*)arg;
+ PyObject *pylocals = locals ? NEW_DICTIONARY(locals) : globals;
- run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
+ run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, pylocals);
if (run_ret == NULL)
{
if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
diff --git a/src/if_python.c b/src/if_python.c
index 461ba52..577807c 100644
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -1009,7 +1009,7 @@
* External interface
*/
static void
-DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
+DoPyCommand(const char *cmd, dict_T* locals, rangeinitializer init_range, runner run, void *arg)
{
#ifndef PY_CAN_RECURSE
static int recursive = 0;
@@ -1058,7 +1058,7 @@
Python_RestoreThread(); // enter python
#endif
- run((char *) cmd, arg
+ run((char *) cmd, locals, arg
#ifdef PY_CAN_RECURSE
, &pygilstate
#endif
@@ -1103,6 +1103,7 @@
p_pyx = 2;
DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
+ NULL,
init_range_cmd,
(runner) run_cmd,
(void *) eap);
@@ -1154,6 +1155,7 @@
// Execute the file
DoPyCommand(buffer,
+ NULL,
init_range_cmd,
(runner) run_cmd,
(void *) eap);
@@ -1166,6 +1168,7 @@
p_pyx = 2;
DoPyCommand((char *)eap->arg,
+ NULL,
init_range_cmd,
(runner)run_do,
(void *)eap);
@@ -1521,9 +1524,10 @@
}
void
-do_pyeval(char_u *str, typval_T *rettv)
+do_pyeval(char_u *str, dict_T *locals, typval_T *rettv)
{
DoPyCommand((char *) str,
+ locals,
init_range_eval,
(runner) run_eval,
(void *) rettv);
diff --git a/src/if_python3.c b/src/if_python3.c
index 5d45ba1..aa934cb 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -1436,7 +1436,11 @@
* External interface
*/
static void
-DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
+DoPyCommand(const char *cmd,
+ dict_T* locals,
+ rangeinitializer init_range,
+ runner run,
+ void *arg)
{
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
char *saved_locale;
@@ -1477,7 +1481,7 @@
cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Py_XDECREF(cmdstr);
- run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
+ run(PyBytes_AsString(cmdbytes), locals, arg, &pygilstate);
Py_XDECREF(cmdbytes);
PyGILState_Release(pygilstate);
@@ -1512,6 +1516,7 @@
p_pyx = 3;
DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
+ NULL,
init_range_cmd,
(runner) run_cmd,
(void *) eap);
@@ -1578,6 +1583,7 @@
// Execute the file
DoPyCommand(buffer,
+ NULL,
init_range_cmd,
(runner) run_cmd,
(void *) eap);
@@ -1590,6 +1596,7 @@
p_pyx = 3;
DoPyCommand((char *)eap->arg,
+ NULL,
init_range_cmd,
(runner)run_do,
(void *)eap);
@@ -2137,9 +2144,10 @@
}
void
-do_py3eval(char_u *str, typval_T *rettv)
+do_py3eval(char_u *str, dict_T *locals, typval_T *rettv)
{
DoPyCommand((char *) str,
+ locals,
init_range_eval,
(runner) run_eval,
(void *) rettv);
diff --git a/src/proto/if_python.pro b/src/proto/if_python.pro
index 51054ca..ee78990 100644
--- a/src/proto/if_python.pro
+++ b/src/proto/if_python.pro
@@ -8,6 +8,6 @@
void python_buffer_free(buf_T *buf);
void python_window_free(win_T *win);
void python_tabpage_free(tabpage_T *tab);
-void do_pyeval(char_u *str, typval_T *rettv);
+void do_pyeval(char_u *str, dict_T* locals, typval_T *rettv);
int set_ref_in_python(int copyID);
/* vim: set ft=c : */
diff --git a/src/proto/if_python3.pro b/src/proto/if_python3.pro
index 0e139b9..63104ab 100644
--- a/src/proto/if_python3.pro
+++ b/src/proto/if_python3.pro
@@ -8,7 +8,7 @@
void python3_buffer_free(buf_T *buf);
void python3_window_free(win_T *win);
void python3_tabpage_free(tabpage_T *tab);
-void do_py3eval(char_u *str, typval_T *rettv);
+void do_py3eval(char_u *str, dict_T* locals, typval_T *rettv);
int set_ref_in_python3(int copyID);
int python3_version(void);
/* vim: set ft=c : */
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index 750c194..6a7f4d3 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -47,6 +47,7 @@
test_vim9_fails \
test_vim9_func \
test_vim9_import \
+ test_vim9_python3 \
test_vim9_script \
test_vim9_typealias
@@ -61,6 +62,7 @@
test_vim9_fails.res \
test_vim9_func.res \
test_vim9_import.res \
+ test_vim9_python3.res \
test_vim9_script.res \
test_vim9_typealias.res
diff --git a/src/testdir/test_python2.vim b/src/testdir/test_python2.vim
index ac43e60..de5f607 100644
--- a/src/testdir/test_python2.vim
+++ b/src/testdir/test_python2.vim
@@ -809,6 +809,49 @@
call AssertException(['let v = pyeval("vim")'], 'E859:')
endfunc
+" Test for py3eval with locals
+func Test_python_pyeval_locals()
+ let str = 'a string'
+ let num = 0xbadb33f
+ let d = {'a': 1, 'b': 2, 'c': str}
+ let l = [ str, num, d ]
+
+ let locals = #{
+ \ s: str,
+ \ n: num,
+ \ d: d,
+ \ l: l,
+ \ }
+
+ " check basics
+ call assert_equal('a string', pyeval('s', locals))
+ call assert_equal(0xbadb33f, pyeval('n', locals))
+ call assert_equal(d, pyeval('d', locals))
+ call assert_equal(l, pyeval('l', locals))
+
+ py << trim EOF
+ def __UpdateDict(d, upd):
+ d.update(upd)
+ return d
+
+ def __ExtendList(l, *args):
+ l.extend(*args)
+ return l
+ EOF
+
+ " check assign to dict member works like bindeval
+ call assert_equal(3, pyeval('__UpdateDict( d, {"c": 3} )["c"]', locals))
+ call assert_equal(3, d['c'])
+
+ " check append lo list
+ call assert_equal(4, pyeval('len(__ExtendList(l, ["new item"]))', locals))
+ call assert_equal("new item", l[-1])
+
+ " check calling a function
+ let StrLen = function('strlen')
+ call assert_equal(3, pyeval('f("abc")', {'f': StrLen}))
+endfunc
+
" Test for vim.bindeval()
func Test_python_vim_bindeval()
" Float
diff --git a/src/testdir/test_python3.vim b/src/testdir/test_python3.vim
index 3178cff..5e2b555 100644
--- a/src/testdir/test_python3.vim
+++ b/src/testdir/test_python3.vim
@@ -1027,6 +1027,52 @@
call AssertException(['let v = py3eval("vim")'], 'E859:')
endfunc
+" Test for py3eval with locals
+func Test_python3_pyeval_locals()
+ let str = 'a string'
+ let num = 0xbadb33f
+ let d = {'a': 1, 'b': 2, 'c': str}
+ let l = [ str, num, d ]
+
+ let locals = #{
+ \ s: str,
+ \ n: num,
+ \ d: d,
+ \ l: l,
+ \ }
+
+ " check basics
+ call assert_equal('a string', py3eval('s', locals))
+ call assert_equal(0xbadb33f, py3eval('n', locals))
+ call assert_equal(d, py3eval('d', locals))
+ call assert_equal(l, py3eval('l', locals))
+ call assert_equal('a,b,c', py3eval('b",".join(l)', {'l': ['a', 'b', 'c']}))
+ call assert_equal('hello', 's'->py3eval({'s': 'hello'}))
+ call assert_equal('a,b,c', 'b",".join(l)'->py3eval({'l': ['a', 'b', 'c']}))
+
+ py3 << trim EOF
+ def __UpdateDict(d, upd):
+ d.update(upd)
+ return d
+
+ def __ExtendList(l, *args):
+ l.extend(*args)
+ return l
+ EOF
+
+ " check assign to dict member works like bindeval
+ call assert_equal(3, py3eval('__UpdateDict( d, {"c": 3} )["c"]', locals))
+ call assert_equal(3, d['c'])
+
+ " check append lo list
+ call assert_equal(4, py3eval('len(__ExtendList(l, ["new item"]))', locals))
+ call assert_equal("new item", l[-1])
+
+ " check calling a function
+ let StrLen = function('strlen')
+ call assert_equal(3, py3eval('f("abc")', {'f': StrLen}))
+endfunc
+
" Test for vim.bindeval()
func Test_python3_vim_bindeval()
" Float
diff --git a/src/testdir/test_pyx2.vim b/src/testdir/test_pyx2.vim
index 7432ceb..781bb41 100644
--- a/src/testdir/test_pyx2.vim
+++ b/src/testdir/test_pyx2.vim
@@ -38,6 +38,49 @@
endfunc
+" Test for pyxeval with locals
+func Test_python_pyeval_locals()
+ let str = 'a string'
+ let num = 0xbadb33f
+ let d = {'a': 1, 'b': 2, 'c': str}
+ let l = [ str, num, d ]
+
+ let locals = #{
+ \ s: str,
+ \ n: num,
+ \ d: d,
+ \ l: l,
+ \ }
+
+ " check basics
+ call assert_equal('a string', pyxeval('s', locals))
+ call assert_equal(0xbadb33f, pyxeval('n', locals))
+ call assert_equal(d, pyxeval('d', locals))
+ call assert_equal(l, pyxeval('l', locals))
+
+ py << trim EOF
+ def __UpdateDict(d, upd):
+ d.update(upd)
+ return d
+
+ def __ExtendList(l, *args):
+ l.extend(*args)
+ return l
+ EOF
+
+ " check assign to dict member works like bindeval
+ call assert_equal(3, pyxeval('__UpdateDict( d, {"c": 3} )["c"]', locals))
+ call assert_equal(3, d['c'])
+
+ " check append lo list
+ call assert_equal(4, pyxeval('len(__ExtendList(l, ["new item"]))', locals))
+ call assert_equal("new item", l[-1])
+
+ " check calling a function
+ let StrLen = function('strlen')
+ call assert_equal(3, pyxeval('f("abc")', {'f': StrLen}))
+endfunc
+
func Test_pyxfile()
" No special comments nor shebangs
redir => var
diff --git a/src/testdir/test_pyx3.vim b/src/testdir/test_pyx3.vim
index 5d38420..b34fdcb 100644
--- a/src/testdir/test_pyx3.vim
+++ b/src/testdir/test_pyx3.vim
@@ -37,6 +37,48 @@
call assert_match(s:py3pattern, split(pyxeval('sys.version'))[0])
endfunc
+" Test for pyxeval with locals
+func Test_python_pyeval_locals()
+ let str = 'a string'
+ let num = 0xbadb33f
+ let d = {'a': 1, 'b': 2, 'c': str}
+ let l = [ str, num, d ]
+
+ let locals = #{
+ \ s: str,
+ \ n: num,
+ \ d: d,
+ \ l: l,
+ \ }
+
+ " check basics
+ call assert_equal('a string', pyxeval('s', locals))
+ call assert_equal(0xbadb33f, pyxeval('n', locals))
+ call assert_equal(d, pyxeval('d', locals))
+ call assert_equal(l, pyxeval('l', locals))
+
+ py3 << trim EOF
+ def __UpdateDict(d, upd):
+ d.update(upd)
+ return d
+
+ def __ExtendList(l, *args):
+ l.extend(*args)
+ return l
+ EOF
+
+ " check assign to dict member works like bindeval
+ call assert_equal(3, pyxeval('__UpdateDict( d, {"c": 3} )["c"]', locals))
+ call assert_equal(3, d['c'])
+
+ " check append lo list
+ call assert_equal(4, pyxeval('len(__ExtendList(l, ["new item"]))', locals))
+ call assert_equal("new item", l[-1])
+
+ " check calling a function
+ let StrLen = function('strlen')
+ call assert_equal(3, pyxeval('f("abc")', {'f': StrLen}))
+endfunc
func Test_pyxfile()
" No special comments nor shebangs
diff --git a/src/testdir/test_vim9_python3.vim b/src/testdir/test_vim9_python3.vim
new file mode 100644
index 0000000..697b368
--- /dev/null
+++ b/src/testdir/test_vim9_python3.vim
@@ -0,0 +1,17 @@
+
+source check.vim
+import './vim9.vim' as v9
+CheckFeature python3
+
+def Test_python3_py3eval_locals()
+ var lines =<< trim EOF
+ var s = 'string'
+ var d = {'s': s}
+ assert_equal('string', py3eval('s', {'s': s}))
+ py3eval('d.update({"s": "new"})', {'d': d})
+ assert_equal('new', d['s'])
+ EOF
+ v9.CheckDefAndScriptSuccess(lines)
+enddef
+
+" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index 7e95a46..3c42059 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 844,
+/**/
843,
/**/
842,