Tests for various python features. vim: set ft=vim :
STARTTEST
:so small.vim
:set noswapfile
:if !has('python3') | e! test.ok | wq! test.out | endif
:lang C
:fun Test()
:py3 import vim
:let l = []
:py3 l=vim.bindeval('l')
:py3 f=vim.bindeval('function("strlen")')
:" Extending List directly with different types
:py3 l+=[1, "as'd", [1, 2, f, {'a': 1}]]
:$put =string(l)
:$put =string(l[-1])
:try
: $put =string(l[-4])
:catch
: $put =v:exception[:13]
:endtry
:" List assignment
:py3 l[0]=0
:$put =string(l)
:py3 l[-2]=f
:$put =string(l)
:"
:" Extending Dictionary directly with different types
:let d = {}
:fun d.f()
: return 1
:endfun
py3 << EOF
d=vim.bindeval('d')
d['1']='asd'
d.update() # Must not do anything, including throwing errors
d.update(b=[1, 2, f])
d.update((('-1', {'a': 1}),))
d.update({'0': -1})
dk = d.keys()
dv = d.values()
di = d.items()
dk.sort(key=repr)
dv.sort(key=repr)
di.sort(key=repr)
EOF
:$put =py3eval('d[''f''](self={})')
:$put =py3eval('repr(dk)')
:$put =substitute(py3eval('repr(dv)'),'0x\x\+','','g')
:$put =substitute(py3eval('repr(di)'),'0x\x\+','','g')
:for [key, Val] in sort(items(d))
: $put =string(key) . ' : ' . string(Val)
: unlet key Val
:endfor
:py3 del dk
:py3 del di
:py3 del dv
:"
:" removing items with del
:py3 del l[2]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:try
: py3 del l[:3]
: py3 del l[1:]
:catch
: $put =v:exception
:endtry
:$put =string(l)
:"
:py3 del d['-1']
:py3 del d['f']
:$put =string(py3eval('d.get(''b'', 1)'))
:$put =string(py3eval('d.pop(''b'')'))
:$put =string(py3eval('d.get(''b'', 1)'))
:$put =string(py3eval('d.pop(''1'', 2)'))
:$put =string(py3eval('d.pop(''1'', 2)'))
:$put =py3eval('repr(d.has_key(''0''))')
:$put =py3eval('repr(d.has_key(''1''))')
:$put =py3eval('repr(''0'' in d)')
:$put =py3eval('repr(''1'' in d)')
:$put =py3eval('repr(list(iter(d)))')
:$put =string(d)
:$put =py3eval('repr(d.popitem())')
:$put =py3eval('repr(d.get(''0''))')
:$put =py3eval('repr(list(iter(d)))')
:"
:" removing items out of range: silently skip items that don't exist
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py3 del l[2:1]
:$put =string(l)
:py3 del l[2:2]
:$put =string(l)
:py3 del l[2:3]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:4]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:5]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:6]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py3 del l[-1:2]
:$put =string(l)
:py3 del l[-2:2]
:$put =string(l)
:py3 del l[-3:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-4:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-5:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-6:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[::2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[3:0:-2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:4:-2]
:$put =string(l)
:"
:" Slice assignment to a list
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[0:0]=['a']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[1:2]=['b']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[2:4]=['c']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[4:4]=['d']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[-1:2]=['e']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[-10:2]=['f']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[2:-10]=['g']
:$put =string(l)
:let l = []
:py3 l=vim.bindeval('l')
:py3 l[0:0]=['h']
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[2:6:2] = [10, 20]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2:-2] = [10, 20]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2] = ()
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2:1] = ()
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[2:2:1] = ()
:$put =string(l)
:"
:" Locked variables
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:lockvar! l
:py3 l[2]='i'
:$put =string(l)
:unlockvar! l
:"
:" Function calls
py3 << EOF
import sys
import re
py33_type_error_pattern = re.compile('^__call__\(\) takes (\d+) positional argument but (\d+) were given$')
def ee(expr, g=globals(), l=locals()):
cb = vim.current.buffer
try:
try:
exec(expr, g, l)
except Exception as e:
if sys.version_info >= (3, 3) and e.__class__ is AttributeError and str(e).find('has no attribute')>=0 and not str(e).startswith("'vim."):
cb.append(expr + ':' + repr((e.__class__, AttributeError(str(e)[str(e).rfind(" '") + 2:-1]))))
elif sys.version_info >= (3, 3) and e.__class__ is ImportError and str(e).find('No module named \'') >= 0:
cb.append(expr + ':' + repr((e.__class__, ImportError(str(e).replace("'", '')))))
elif sys.version_info >= (3, 3) and e.__class__ is TypeError:
m = py33_type_error_pattern.search(str(e))
if m:
msg = '__call__() takes exactly {0} positional argument ({1} given)'.format(m.group(1), m.group(2))
cb.append(expr + ':' + repr((e.__class__, TypeError(msg))))
else:
cb.append(expr + ':' + repr((e.__class__, e)))
else:
cb.append(expr + ':' + repr((e.__class__, e)))
else:
cb.append(expr + ':NOT FAILED')
except Exception as e:
cb.append(expr + '::' + repr((e.__class__, e)))
EOF
:fun New(...)
: return ['NewStart']+a:000+['NewEnd']
:endfun
:fun DictNew