Skip to content

Commit a58883b

Browse files
committed
patch 8.0.0265: may get ml_get error when :pydo deletes lines
Problem: May get ml_get error when :pydo deletes lines or switches to another buffer. (Nikolai Pavlov, issue #1421) Solution: Check the buffer and line every time.
1 parent d297f35 commit a58883b

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

src/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,8 @@ test_arglist \
21682168
test_popup \
21692169
test_profile \
21702170
test_put \
2171+
test_python2 \
2172+
test_python3 \
21712173
test_pyx2 \
21722174
test_pyx3 \
21732175
test_quickfix \

src/if_py_both.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5619,6 +5619,7 @@ run_do(const char *cmd, void *arg UNUSED
56195619
int status;
56205620
PyObject *pyfunc, *pymain;
56215621
PyObject *run_ret;
5622+
buf_T *was_curbuf = curbuf;
56225623

56235624
if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
56245625
{
@@ -5671,7 +5672,9 @@ run_do(const char *cmd, void *arg UNUSED
56715672
#ifdef PY_CAN_RECURSE
56725673
*pygilstate = PyGILState_Ensure();
56735674
#endif
5674-
if (!(line = GetBufferLine(curbuf, lnum)))
5675+
/* Check the line number, the command my have deleted lines. */
5676+
if (lnum > curbuf->b_ml.ml_line_count
5677+
|| !(line = GetBufferLine(curbuf, lnum)))
56755678
goto err;
56765679
if (!(linenr = PyInt_FromLong((long) lnum)))
56775680
{
@@ -5684,9 +5687,19 @@ run_do(const char *cmd, void *arg UNUSED
56845687
if (!ret)
56855688
goto err;
56865689

5690+
/* Check that the command didn't switch to another buffer. */
5691+
if (curbuf != was_curbuf)
5692+
{
5693+
Py_XDECREF(ret);
5694+
goto err;
5695+
}
5696+
56875697
if (ret != Py_None)
56885698
if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5699+
{
5700+
Py_XDECREF(ret);
56895701
goto err;
5702+
}
56905703

56915704
Py_XDECREF(ret);
56925705
PythonIO_Flush();

src/testdir/Make_all.mak

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ NEW_TESTS = test_arglist.res \
176176
test_packadd.res \
177177
test_perl.res \
178178
test_profile.res \
179+
test_python2.res \
180+
test_python3.res \
179181
test_pyx2.res \
180182
test_pyx3.res \
181183
test_quickfix.res \

src/testdir/test_python2.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
" Test for python 2 commands.
2+
" TODO: move tests from test87.in here.
3+
4+
if !has('python')
5+
finish
6+
endif
7+
8+
func Test_pydo()
9+
" Check deleting lines does not trigger ml_get error.
10+
py import vim
11+
new
12+
call setline(1, ['one', 'two', 'three'])
13+
pydo vim.command("%d_")
14+
bwipe!
15+
16+
" Check switching to another buffer does not trigger ml_get error.
17+
new
18+
let wincount = winnr('$')
19+
call setline(1, ['one', 'two', 'three'])
20+
pydo vim.command("new")
21+
call assert_equal(wincount + 1, winnr('$'))
22+
bwipe!
23+
bwipe!
24+
endfunc

src/testdir/test_python3.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
" Test for python 2 commands.
2+
" TODO: move tests from test88.in here.
3+
4+
if !has('python3')
5+
finish
6+
endif
7+
8+
func Test_py3do()
9+
" Check deleting lines does not trigger an ml_get error.
10+
py3 import vim
11+
new
12+
call setline(1, ['one', 'two', 'three'])
13+
py3do vim.command("%d_")
14+
bwipe!
15+
16+
" Check switching to another buffer does not trigger an ml_get error.
17+
new
18+
let wincount = winnr('$')
19+
call setline(1, ['one', 'two', 'three'])
20+
py3do vim.command("new")
21+
call assert_equal(wincount + 1, winnr('$'))
22+
bwipe!
23+
bwipe!
24+
endfunc

src/version.c

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

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
265,
767769
/**/
768770
264,
769771
/**/

0 commit comments

Comments
 (0)