Mercurial > vim
view src/testdir/test_listener.vim @ 32936:c517845bd10e v9.0.1776
patch 9.0.1776: No support for stable Python 3 ABI
Commit: https://github.com/vim/vim/commit/c13b3d1350b60b94fe87f0761ea31c0e7fb6ebf3
Author: Yee Cheng Chin <ychin.git@gmail.com>
Date: Sun Aug 20 21:18:38 2023 +0200
patch 9.0.1776: No support for stable Python 3 ABI
Problem: No support for stable Python 3 ABI
Solution: Support Python 3 stable ABI
Commits:
1) Support Python 3 stable ABI to allow mixed version interoperatbility
Vim currently supports embedding Python for use with plugins, and the
"dynamic" linking option allows the user to specify a locally installed
version of Python by setting `pythonthreedll`. However, one caveat is
that the Python 3 libs are not binary compatible across minor versions,
and mixing versions can potentially be dangerous (e.g. let's say Vim was
linked against the Python 3.10 SDK, but the user sets `pythonthreedll`
to a 3.11 lib). Usually, nothing bad happens, but in theory this could
lead to crashes, memory corruption, and other unpredictable behaviors.
It's also difficult for the user to tell something is wrong because Vim
has no way of reporting what Python 3 version Vim was linked with.
For Vim installed via a package manager, this usually isn't an issue
because all the dependencies would already be figured out. For prebuilt
Vim binaries like MacVim (my motivation for working on this), AppImage,
and Win32 installer this could potentially be an issue as usually a
single binary is distributed. This is more tricky when a new Python
version is released, as there's a chicken-and-egg issue with deciding
what Python version to build against and hard to keep in sync when a new
Python version just drops and we have a mix of users of different Python
versions, and a user just blindly upgrading to a new Python could lead to
bad interactions with Vim.
Python 3 does have a solution for this problem: stable ABI / limited API
(see https://docs.python.org/3/c-api/stable.html). The C SDK limits the
API to a set of functions that are promised to be stable across
versions. This pull request adds an ifdef config that allows us to turn
it on when building Vim. Vim binaries built with this option should be
safe to freely link with any Python 3 libraies without having the
constraint of having to use the same minor version.
Note: Python 2 has no such concept and this doesn't change how Python 2
integration works (not that there is going to be a new version of Python
2 that would cause compatibility issues in the future anyway).
---
Technical details:
======
The stable ABI can be accessed when we compile with the Python 3 limited
API (by defining `Py_LIMITED_API`). The Python 3 code (in `if_python3.c`
and `if_py_both.h`) would now handle this and switch to limited API
mode. Without it set, Vim will still use the full API as before so this
is an opt-in change.
The main difference is that `PyType_Object` is now an opaque struct that
we can't directly create "static types" out of, and we have to create
type objects as "heap types" instead. This is because the struct is not
stable and changes from version to version (e.g. 3.8 added a
`tp_vectorcall` field to it). I had to change all the types to be
allocated on the heap instead with just a pointer to them.
Other functions are also simply missing in limited API, or they are
introduced too late (e.g. `PyUnicode_AsUTF8AndSize` in 3.10) to it that
we need some other ways to do the same thing, so I had to abstract a few
things into macros, and sometimes re-implement functions like
`PyObject_NEW`.
One caveat is that in limited API, `OutputType` (used for replacing
`sys.stdout`) no longer inherits from `PyStdPrinter_Type` which I don't
think has any real issue other than minor differences in how they
convert to a string and missing a couple functions like `mode()` and
`fileno()`.
Also fixed an existing bug where `tp_basicsize` was set incorrectly for
`BufferObject`, `TabListObject, `WinListObject`.
Technically, there could be a small performance drop, there is a little
more indirection with accessing type objects, and some APIs like
`PyUnicode_AsUTF8AndSize` are missing, but in practice I didn't see any
difference, and any well-written Python plugin should try to avoid
excessing callbacks to the `vim` module in Python anyway.
I only tested limited API mode down to Python 3.7, which seemes to
compile and work fine. I haven't tried earlier Python versions.
2) Fix PyIter_Check on older Python vers / type##Ptr unused warning
For PyIter_Check, older versions exposed them as either macros (used in
full API), or a function (for use in limited API). A previous change
exposed PyIter_Check to the dynamic build because Python just moved it
to function-only in 3.10 anyway. Because of that, just make sure we
always grab the function in dynamic builds in earlier versions since
that's what Python eventually did anyway.
3) Move Py_LIMITED_API define to configure script
Can now use --with-python-stable-abi flag to customize what stable ABI
version to target. Can also use an env var to do so as well.
4) Show +python/dyn-stable in :version, and allow has() feature query
Not sure if the "/dyn-stable" suffix would break things, or whether we
should do it another way. Or just don't show it in version and rely on
has() feature checking.
5) Documentation first draft. Still need to implement v:python3_version
6) Fix PyIter_Check build breaks when compiling against Python 3.8
7) Add CI coverage stable ABI on Linux/Windows / make configurable on Windows
This adds configurable options for Windows make files (both MinGW and
MSVC). CI will also now exercise both traditional full API and stable
ABI for Linux and Windows in the matrix for coverage.
Also added a "dynamic" option to Linux matrix as a drive-by change to
make other scripting languages like Ruby / Perl testable under both
static and dynamic builds.
8) Fix inaccuracy in Windows docs
Python's own docs are confusing but you don't actually want to use
`python3.dll` for the dynamic linkage.
9) Add generated autoconf file
10) Add v:python3_version support
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.
11) Fix Python 3.7 compat issues
Fix a couple issues when using limited API < 3.8
- Crash on exit: In Python 3.7, if a heap-allocated type is destroyed
before all instances are, it would cause a crash later. This happens
when we destroyed `OptionsType` before calling `Py_Finalize` when
using the limited API. To make it worse, later versions changed the
semantics and now each instance has a strong reference to its own type
and the recommendation has changed to have each instance de-ref its
own type and have its type in GC traversal. To avoid dealing with
these cross-version variations, we just don't free the heap type. They
are static types in non-limited-API anyway and are designed to last
through the entirety of the app, and we also don't restart the Python
runtime and therefore do not need it to have absolutely 0 leaks.
See:
- https://docs.python.org/3/whatsnew/3.8.html#changes-in-the-c-api
- https://docs.python.org/3/whatsnew/3.9.html#changes-in-the-c-api
- PyIter_Check: This function is not provided in limited APIs older than
3.8. Previously I was trying to mock it out using manual
PyType_GetSlot() but it was brittle and also does not actually work
properly for static types (it will generate a Python error). Just
return false. It does mean using limited API < 3.8 is not recommended
as you lose the functionality to handle iterators, but from playing
with plugins I couldn't find it to be an issue.
- Fix loading of PyIter_Check so it will be done when limited API < 3.8.
Otherwise loading a 3.7 Python lib will fail even if limited API was
specified to use it.
12) Make sure to only load `PyUnicode_AsUTF8AndSize` in needed in limited API
We don't use this function unless limited API >= 3.10, but we were
loading it regardless. Usually it's ok in Unix-like systems where Python
just has a single lib that we load from, but in Windows where there is a
separate python3.dll this would not work as the symbol would not have
been exposed in this more limited DLL file. This makes it much clearer
under what condition is this function needed.
closes: #12032
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 20 Aug 2023 21:30:04 +0200 |
parents | 695b50472e85 |
children |
line wrap: on
line source
" tests for listener_add() and listener_remove() func s:StoreList(s, e, a, l) let s:start = a:s let s:end = a:e let s:added = a:a let s:text = getline(a:s) let s:list = a:l endfunc func s:AnotherStoreList(l) let s:list2 = a:l endfunc func s:EvilStoreList(l) let s:list3 = a:l call assert_fails("call add(a:l, 'myitem')", "E742:") endfunc func Test_listening() new call setline(1, ['one', 'two']) let s:list = [] let id = listener_add({b, s, e, a, l -> s:StoreList(s, e, a, l)}) call setline(1, 'one one') call listener_flush() call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list) " Undo is also a change set undolevels& " start new undo block call append(2, 'two two') undo call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}], s:list) redraw " the two changes are not merged call assert_equal([{'lnum': 3, 'end': 4, 'col': 1, 'added': -1}], s:list) 1 " Two listeners, both get called. Also check column. call setline(1, ['one one', 'two']) call listener_flush() let id2 = listener_add({b, s, e, a, l -> s:AnotherStoreList(l)}) let s:list = [] let s:list2 = [] exe "normal $asome\<Esc>" redraw call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], s:list) call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], s:list2) " removing listener works call listener_remove(id2) call setline(1, ['one one', 'two']) call listener_flush() let s:list = [] let s:list2 = [] call setline(3, 'three') redraw call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}], s:list) call assert_equal([], s:list2) " a change above a previous change without a line number change is reported " together call setline(1, ['one one', 'two']) call listener_flush(bufnr()) call append(2, 'two two') call setline(1, 'something') call bufnr()->listener_flush() call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}, \ {'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list) call assert_equal(1, s:start) call assert_equal(3, s:end) call assert_equal(1, s:added) " an insert just above a previous change that was the last one does not get " merged call setline(1, ['one one', 'two']) call listener_flush() let s:list = [] call setline(2, 'something') call append(1, 'two two') call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list) call listener_flush() call assert_equal([{'lnum': 2, 'end': 2, 'col': 1, 'added': 1}], s:list) " an insert above a previous change causes a flush call setline(1, ['one one', 'two']) call listener_flush() call setline(2, 'something') call append(0, 'two two') call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list) call assert_equal('something', s:text) call listener_flush() call assert_equal([{'lnum': 1, 'end': 1, 'col': 1, 'added': 1}], s:list) call assert_equal('two two', s:text) " a delete at a previous change that was the last one does not get merged call setline(1, ['one one', 'two']) call listener_flush() let s:list = [] call setline(2, 'something') 2del call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list) call listener_flush() call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': -1}], s:list) " a delete above a previous change causes a flush call setline(1, ['one one', 'two']) call listener_flush() call setline(2, 'another') 1del call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list) call assert_equal(2, s:start) call assert_equal('another', s:text) call listener_flush() call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': -1}], s:list) call assert_equal('another', s:text) " the "o" command first adds an empty line and then changes it %del call setline(1, ['one one', 'two']) call listener_flush() let s:list = [] exe "normal Gofour\<Esc>" redraw call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}, \ {'lnum': 3, 'end': 4, 'col': 1, 'added': 0}], s:list) " Remove last listener let s:list = [] call listener_remove(id) call setline(1, 'asdfasdf') redraw call assert_equal([], s:list) " Trying to change the list fails let id = listener_add({b, s, e, a, l -> s:EvilStoreList(l)}) let s:list3 = [] call setline(1, 'asdfasdf') redraw call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list3) eval id->listener_remove() bwipe! endfunc func s:StoreListArgs(buf, start, end, added, list) let s:buf = a:buf let s:start = a:start let s:end = a:end let s:added = a:added let s:list = a:list endfunc func Test_listener_args() new call setline(1, ['one', 'two']) let s:list = [] let id = listener_add('s:StoreListArgs') " just one change call setline(1, 'one one') call listener_flush() call assert_equal(bufnr(''), s:buf) call assert_equal(1, s:start) call assert_equal(2, s:end) call assert_equal(0, s:added) call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list) " two disconnected changes call setline(1, ['one', 'two', 'three', 'four']) call listener_flush() call setline(1, 'one one') call setline(3, 'three three') call listener_flush() call assert_equal(bufnr(''), s:buf) call assert_equal(1, s:start) call assert_equal(4, s:end) call assert_equal(0, s:added) call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}, \ {'lnum': 3, 'end': 4, 'col': 1, 'added': 0}], s:list) " add and remove lines call setline(1, ['one', 'two', 'three', 'four', 'five', 'six']) call listener_flush() call append(2, 'two two') 4del call append(5, 'five five') call listener_flush() call assert_equal(bufnr(''), s:buf) call assert_equal(3, s:start) call assert_equal(6, s:end) call assert_equal(1, s:added) call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}, \ {'lnum': 4, 'end': 5, 'col': 1, 'added': -1}, \ {'lnum': 6, 'end': 6, 'col': 1, 'added': 1}], s:list) " split a line then insert one, should get two disconnected change lists call setline(1, 'split here') call listener_flush() let s:list = [] exe "normal 1ggwi\<CR>\<Esc>" 1 normal o call assert_equal([{'lnum': 1, 'end': 2, 'col': 7, 'added': 1}], s:list) call listener_flush() call assert_equal([{'lnum': 2, 'end': 2, 'col': 1, 'added': 1}], s:list) call listener_remove(id) bwipe! " Invalid arguments call assert_fails('call listener_add([])', 'E921:') call assert_fails('call listener_add("s:StoreListArgs", [])', 'E730:') call assert_fails('call listener_flush([])', 'E730:') call assert_fails('eval ""->listener_add()', 'E119:') endfunc func s:StoreBufList(buf, start, end, added, list) let s:bufnr = a:buf let s:list = a:list endfunc func Test_listening_other_buf() new call setline(1, ['one', 'two']) let bufnr = bufnr('') normal ww let id = bufnr->listener_add(function('s:StoreBufList')) let s:list = [] call setbufline(bufnr, 1, 'hello') redraw call assert_equal(bufnr, s:bufnr) call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list) call listener_remove(id) exe "buf " .. bufnr bwipe! endfunc func Test_listener_garbage_collect() func MyListener(x, bufnr, start, end, added, changes) " NOP endfunc new let id = listener_add(function('MyListener', [{}]), bufnr('')) call test_garbagecollect_now() " must not crash caused by invalid memory access normal ia call assert_true(v:true) call listener_remove(id) delfunc MyListener bwipe! endfunc " This verifies the fix for issue #4455 func Test_listener_caches_buffer_line() new inoremap <silent> <CR> <CR><Esc>O function EchoChanges(bufnr, start, end, added, changes) for l:change in a:changes let text = getbufline(a:bufnr, l:change.lnum, l:change.end-1+l:change.added) endfor endfunction let lid = listener_add("EchoChanges") set autoindent set cindent call setline(1, ["{", "\tif true {}", "}"]) exe "normal /{}\nl" call feedkeys("i\r\e", 'xt') call assert_equal(["{", "\tif true {", "", "\t}", "}"], getline(1, 5)) bwipe! delfunc EchoChanges call listener_remove(lid) iunmap <CR> set nocindent endfunc " Verify the fix for issue #4908 func Test_listener_undo_line_number() function DoIt() " NOP endfunction function EchoChanges(bufnr, start, end, added, changes) call DoIt() endfunction new let lid = listener_add("EchoChanges") call setline(1, ['a', 'b', 'c']) set undolevels& " start new undo block call feedkeys("ggcG\<Esc>", 'xt') undo bwipe! delfunc DoIt delfunc EchoChanges call listener_remove(lid) endfunc func Test_listener_undo_delete_all() new call setline(1, [1, 2, 3, 4]) let s:changes = [] func s:ExtendList(bufnr, start, end, added, changes) call extend(s:changes, a:changes) endfunc let id = listener_add('s:ExtendList') set undolevels& " start new undo block normal! ggdG undo call listener_flush() call assert_equal(2, s:changes->len()) " delete removes four lines, empty line remains call assert_equal({'lnum': 1, 'end': 5, 'col': 1, 'added': -4}, s:changes[0]) " undo replaces empty line and adds 3 lines call assert_equal({'lnum': 1, 'end': 2, 'col': 1, 'added': 3}, s:changes[1]) call listener_remove(id) delfunc s:ExtendList unlet s:changes bwipe! endfunc func Test_listener_cleared_newbuf() func Listener(bufnr, start, end, added, changes) let g:gotCalled += 1 endfunc new " check that listening works let g:gotCalled = 0 let lid = listener_add("Listener") call feedkeys("axxx\<Esc>", 'xt') call listener_flush(bufnr()) call assert_equal(1, g:gotCalled) %bwipe! let bufnr = bufnr() let b:testing = 123 let lid = listener_add("Listener") enew! " check buffer is reused call assert_equal(bufnr, bufnr()) call assert_false(exists('b:testing')) " check that listening stops when reusing the buffer let g:gotCalled = 0 call feedkeys("axxx\<Esc>", 'xt') call listener_flush(bufnr()) call assert_equal(0, g:gotCalled) unlet g:gotCalled bwipe! delfunc Listener endfunc func Test_col_after_deletion_moved_cur() func Listener(bufnr, start, end, added, changes) call assert_equal([#{lnum: 1, end: 2, added: 0, col: 2}], a:changes) endfunc new call setline(1, ['foo']) let lid = listener_add('Listener') call feedkeys("lD", 'xt') call listener_flush() bwipe! delfunc Listener endfunc func Test_remove_listener_in_callback() new let s:ID = listener_add('Listener') func Listener(...) call listener_remove(s:ID) let g:listener_called = 'yes' endfunc call setline(1, ['foo']) call feedkeys("lD", 'xt') call listener_flush() call assert_equal('yes', g:listener_called) bwipe! delfunc Listener unlet g:listener_called endfunc " When multiple listeners are registered, remove one listener and verify the " other listener is still called func Test_remove_one_listener_in_callback() new let g:listener1_called = 0 let g:listener2_called = 0 let s:ID1 = listener_add('Listener1') let s:ID2 = listener_add('Listener2') func Listener1(...) call listener_remove(s:ID1) let g:listener1_called += 1 endfunc func Listener2(...) let g:listener2_called += 1 endfunc call setline(1, ['foo']) call feedkeys("~", 'xt') call listener_flush() call feedkeys("~", 'xt') call listener_flush() call assert_equal(1, g:listener1_called) call assert_equal(2, g:listener2_called) call listener_remove(s:ID2) bwipe! delfunc Listener1 delfunc Listener2 unlet g:listener1_called unlet g:listener2_called endfunc func Test_no_change_for_empty_undo() new let text = ['some word here', 'second line'] call setline(1, text) let g:entries = [] func Listener(bufnr, start, end, added, changes) for change in a:changes call add(g:entries, [change.lnum, change.end, change.added]) endfor endfunc let s:ID = listener_add('Listener') let @a = "one line\ntwo line\nthree line" set undolevels& " start new undo block call feedkeys('fwviw"ap', 'xt') call listener_flush(bufnr()) " first change deletes "word", second change inserts the register call assert_equal([[1, 2, 0], [1, 2, 2]], g:entries) let g:entries = [] set undolevels& " start new undo block undo call listener_flush(bufnr()) call assert_equal([[1, 4, -2]], g:entries) call assert_equal(text, getline(1, 2)) call listener_remove(s:ID) bwipe! unlet g:entries delfunc Listener endfunc " vim: shiftwidth=2 sts=2 expandtab