Mercurial > vim
view src/testdir/test_cscope.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 | 1b9abc263eb3 |
children |
line wrap: on
line source
" Test for cscope commands. source check.vim CheckFeature cscope CheckFeature quickfix CheckExecutable cscope func CscopeSetupOrClean(setup) if a:setup noa sp ../memfile_test.c saveas! Xmemfile_test.c call system('cscope -bk -fXcscope.out Xmemfile_test.c') call system('cscope -bk -fXcscope2.out Xmemfile_test.c') cscope add Xcscope.out set cscopequickfix=s-,g-,d-,c-,t-,e-,f-,i-,a- else cscope kill -1 for file in ['Xcscope.out', 'Xcscope2.out', 'Xmemfile_test.c'] call delete(file) endfo endif endfunc func Test_cscopeWithCscopeConnections() call CscopeSetupOrClean(1) " Test: E568: duplicate cscope database not added try set nocscopeverbose cscope add Xcscope.out set cscopeverbose catch call assert_report('exception thrown') endtry call assert_fails('cscope add', 'E560:') call assert_fails('cscope add Xcscope.out', 'E568:') call assert_fails('cscope add doesnotexist.out', 'E563:') if has('unix') call assert_fails('cscope add /dev/null', 'E564:') endif " Test: Find this C-Symbol for cmd in ['cs find s main', 'cs find 0 main'] let a = execute(cmd) " Test where it moves the cursor call assert_equal('main(void)', getline('.')) " Test the output of the :cs command call assert_match('\n(1 of 1): <<main>> main(void )', a) endfor " Test: Find this definition for cmd in ['cs find g test_mf_hash', \ 'cs find 1 test_mf_hash', \ 'cs find 1 test_mf_hash'] " leading space ignored. exe cmd call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', ' static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1)) endfor " Test: Find functions called by this function for cmd in ['cs find d test_mf_hash', 'cs find 2 test_mf_hash'] let a = execute(cmd) call assert_match('\n(1 of 42): <<mf_hash_init>> mf_hash_init(&ht);', a) call assert_equal(' mf_hash_init(&ht);', getline('.')) endfor " Test: Find functions calling this function for cmd in ['cs find c test_mf_hash', 'cs find 3 test_mf_hash'] let a = execute(cmd) call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a) call assert_equal(' test_mf_hash();', getline('.')) endfor " Test: Find this text string for cmd in ['cs find t Bram', 'cs find 4 Bram'] let a = execute(cmd) call assert_match('(1 of 1): <<<unknown>>> \* VIM - Vi IMproved^Iby Bram Moolenaar', a) call assert_equal(' * VIM - Vi IMproved by Bram Moolenaar', getline('.')) endfor " Test: Find this egrep pattern " test all matches returned by cscope for cmd in ['cs find e ^\#includ.', 'cs find 6 ^\#includ.'] let a = execute(cmd) call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a) call assert_equal('#include <assert.h>', getline('.')) cnext call assert_equal('#include "main.c"', getline('.')) cnext call assert_equal('#include "memfile.c"', getline('.')) call assert_fails('cnext', 'E553:') endfor " Test: Find the same egrep pattern using lcscope this time. let a = execute('lcs find e ^\#includ.') call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a) call assert_equal('#include <assert.h>', getline('.')) lnext call assert_equal('#include "main.c"', getline('.')) lnext call assert_equal('#include "memfile.c"', getline('.')) call assert_fails('lnext', 'E553:') " Test: Find this file for cmd in ['cs find f Xmemfile_test.c', 'cs find 7 Xmemfile_test.c'] enew let a = execute(cmd) call assert_true(a =~ '"Xmemfile_test.c" \d\+L, \d\+B') call assert_equal('Xmemfile_test.c', @%) endfor " Test: Find files #including this file for cmd in ['cs find i assert.h', 'cs find 8 assert.h'] enew let a = execute(cmd) let alines = split(a, '\n', 1) call assert_equal('', alines[0]) call assert_true(alines[1] =~ '"Xmemfile_test.c" \d\+L, \d\+B') call assert_equal('(1 of 1): <<global>> #include <assert.h>', alines[2]) call assert_equal('#include <assert.h>', getline('.')) endfor " Test: Invalid find command call assert_fails('cs find', 'E560:') call assert_fails('cs find x', 'E560:') " Test: Find places where this symbol is assigned a value " this needs a cscope >= 15.8 " unfortunately, Travis has cscope version 15.7 let cscope_version = systemlist('cscope --version')[0] let cs_version = str2float(matchstr(cscope_version, '\d\+\(\.\d\+\)\?')) if cs_version >= 15.8 for cmd in ['cs find a item', 'cs find 9 item'] let a = execute(cmd) call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = LALLOC_CLEAR_ONE(mf_hashitem_T);'], split(a, '\n', 1)) call assert_equal(' item = LALLOC_CLEAR_ONE(mf_hashitem_T);', getline('.')) cnext call assert_equal(' item = mf_hash_find(&ht, key);', getline('.')) cnext call assert_equal(' item = mf_hash_find(&ht, key);', getline('.')) cnext call assert_equal(' item = mf_hash_find(&ht, key);', getline('.')) endfor endif " Test: leading whitespace is not removed for cscope find text let a = execute('cscope find t test_mf_hash') call assert_equal(['', '(1 of 1): <<<unknown>>> test_mf_hash();'], split(a, '\n', 1)) call assert_equal(' test_mf_hash();', getline('.')) " Test: test with scscope let a = execute('scs find t Bram') call assert_match('(1 of 1): <<<unknown>>> \* VIM - Vi IMproved^Iby Bram Moolenaar', a) call assert_equal(' * VIM - Vi IMproved by Bram Moolenaar', getline('.')) " Test: cscope help for cmd in ['cs', 'cs help', 'cs xxx'] let a = execute(cmd) call assert_match('^cscope commands:\n', a) call assert_match('\nadd :', a) call assert_match('\nfind :', a) call assert_match('\nhelp : Show this message', a) call assert_match('\nkill : Kill a connection', a) call assert_match('\nreset: Reinit all connections', a) call assert_match('\nshow : Show connections', a) endfor let a = execute('scscope help') call assert_match('This cscope command does not support splitting the window\.', a) " Test: reset connections let a = execute('cscope reset') call assert_match('\nAdded cscope database.*Xcscope.out (#0)', a) call assert_match('\nAll cscope databases reset', a) " Test: cscope show let a = execute('cscope show') call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a) " Test: cstag and 'csto' option set csto=0 let a = execute('cstag TEST_COUNT') call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a) call assert_equal('#define TEST_COUNT 50000', getline('.')) call assert_fails('cstag DOES_NOT_EXIST', 'E257:') set csto=1 let a = execute('cstag index_to_key') call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a) call assert_equal('#define index_to_key(i) ((i) ^ 15167)', getline('.')) call assert_fails('cstag DOES_NOT_EXIST', 'E257:') call assert_fails('cstag', 'E562:') let save_tags = &tags set tags= call assert_fails('cstag DOES_NOT_EXIST', 'E257:') let a = execute('cstag index_to_key') call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a) let &tags = save_tags " Test: 'cst' option set nocst call assert_fails('tag TEST_COUNT', 'E433:') set cst let a = execute('tag TEST_COUNT') call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a) call assert_equal('#define TEST_COUNT 50000', getline('.')) let a = execute('tags') call assert_match('1 1 TEST_COUNT\s\+\d\+\s\+#define index_to_key', a) " Test: 'cscoperelative' call mkdir('Xcscoperelative') cd Xcscoperelative let a = execute('cs find g test_mf_hash') call assert_notequal('test_mf_hash(void)', getline('.')) set cscoperelative let a = execute('cs find g test_mf_hash') call assert_equal('test_mf_hash(void)', getline('.')) set nocscoperelative cd .. call delete('Xcscoperelative', 'd') " Test: E259: no match found call assert_fails('cscope find g DOES_NOT_EXIST', 'E259:') " Test: this should trigger call to cs_print_tags() " Unclear how to check result though, we just exercise the code. set cst cscopequickfix=s0 call feedkeys(":cs find s main\<CR>", 't') " Test: cscope kill call assert_fails('cscope kill', 'E560:') call assert_fails('cscope kill 2', 'E261:') call assert_fails('cscope kill xxx', 'E261:') let a = execute('cscope kill 0') call assert_match('cscope connection 0 closed', a) cscope add Xcscope.out let a = execute('cscope kill Xcscope.out') call assert_match('cscope connection Xcscope.out closed', a) cscope add Xcscope.out . let a = execute('cscope kill -1') call assert_match('cscope connection .*Xcscope.out closed', a) let a = execute('cscope kill -1') call assert_equal('', a) " Test: 'csprg' option invalid command call assert_equal('cscope', &csprg) set csprg=doesnotexist call assert_fails('cscope add Xcscope2.out', 'E609:') set csprg=cscope " Test: multiple cscope connections cscope add Xcscope.out cscope add Xcscope2.out . -C let a = execute('cscope show') call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a) call assert_match('\n 1 \d\+.*Xcscope2.out\s*\.', a) " Test: test Ex command line completion call feedkeys(":cs \<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"cs add find help kill reset show', @:) call feedkeys(":scs \<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"scs find', @:) call feedkeys(":cs find \<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"cs find a c d e f g i s t', @:) call feedkeys(":cs kill \<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"cs kill -1 0 1', @:) call feedkeys(":cs add Xcscope\<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"cs add Xcscope.out Xcscope2.out', @:) " Test: cscope_connection() call assert_equal(cscope_connection(), 1) call assert_equal(cscope_connection(0, 'out'), 1) call assert_equal(cscope_connection(0, 'xxx'), 1) call assert_equal(cscope_connection(1, 'out'), 1) call assert_equal(cscope_connection(1, 'xxx'), 0) call assert_equal(cscope_connection(2, 'out'), 0) call assert_equal(cscope_connection(2, getcwd() .. '/Xcscope.out', 1), 1) call assert_equal(cscope_connection(3, 'xxx', '..'), 0) call assert_equal(cscope_connection(3, 'out', 'xxx'), 0) call assert_equal(cscope_connection(3, 'out', '.'), 1) call assert_equal(cscope_connection(4, 'out', '.'), 0) call assert_equal(cscope_connection(5, 'out'), 0) call assert_equal(cscope_connection(-1, 'out'), 0) call CscopeSetupOrClean(0) endfunc " Test ":cs add {dir}" (add the {dir}/cscope.out database) func Test_cscope_add_dir() call mkdir('Xcscopedir', 'pD') " Cscope doesn't handle symlinks, so this needs to be resolved in case a " shadow directory is being used. let memfile = resolve('../memfile_test.c') call system('cscope -bk -fXcscopedir/cscope.out ' . memfile) cs add Xcscopedir let a = execute('cscope show') let lines = split(a, "\n", 1) call assert_equal(3, len(lines)) call assert_equal(' # pid database name prepend path', lines[0]) call assert_equal('', lines[1]) call assert_match('^ 0 \d\+.*Xcscopedir/cscope.out\s\+<none>$', lines[2]) cs kill -1 call delete('Xcscopedir/cscope.out') call assert_fails('cs add Xcscopedir', 'E563:') endfunc func Test_cscopequickfix() set cscopequickfix=s-,g-,d+,c-,t+,e-,f0,i-,a- call assert_equal('s-,g-,d+,c-,t+,e-,f0,i-,a-', &cscopequickfix) call assert_fails('set cscopequickfix=x-', 'E474:') call assert_fails('set cscopequickfix=s', 'E474:') call assert_fails('set cscopequickfix=s7', 'E474:') call assert_fails('set cscopequickfix=s-a', 'E474:') endfunc func Test_withoutCscopeConnection() call assert_equal(cscope_connection(), 0) call assert_fails('cscope find s main', 'E567:') let a = execute('cscope show') call assert_match('no cscope connections', a) endfunc " vim: shiftwidth=2 sts=2 expandtab