0
0
mirror of https://github.com/vim/vim.git synced 2025-10-30 09:47:20 -04:00

updated for version 7.3.997

Problem:    Vim and Python exceptions are different.
Solution:   Make Vim exceptions be Python exceptions. (ZyX)
This commit is contained in:
Bram Moolenaar
2013-05-21 20:40:40 +02:00
parent cac867ad18
commit a7b64ce74e
6 changed files with 177 additions and 72 deletions

View File

@@ -380,20 +380,24 @@ def e(s, g=globals(), l=locals()):
try:
exec(s, g, l)
except:
vim.command('throw ' + repr(sys.exc_type.__name__))
vim.command('return ' + repr(sys.exc_type.__name__))
def ev(s, g=globals(), l=locals()):
try:
return eval(s, g, l)
except:
vim.command('throw ' + repr(sys.exc_type.__name__))
vim.command('let exc=' + repr(sys.exc_type.__name__))
return 0
EOF
:function E(s)
: python e(vim.eval('a:s'))
:endfunction
:function Ev(s)
: return pyeval('ev(vim.eval("a:s"))')
: let r=pyeval('ev(vim.eval("a:s"))')
: if exists('exc')
: throw exc
: endif
: return r
:endfunction
:py gopts1=vim.options
:py wopts1=vim.windows[2].options
@@ -437,27 +441,24 @@ EOF
: catch
: put =' p/'.v.'! '.v:exception
: endtry
: try
: call E(v.'["'.oname.'"]=invval')
: catch
: put =' inv: '.string(invval).'! '.v:exception
: endtry
: let r=E(v.'['''.oname.''']=invval')
: if r isnot 0
: put =' inv: '.string(invval).'! '.r
: endif
: for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3'])
: let val=substitute(vv, '^.opts', 'oval', '')
: try
: call E(vv.'["'.oname.'"]='.val)
: catch
: put =' '.vv.'! '.v:exception
: endtry
: let r=E(vv.'['''.oname.''']='.val)
: if r isnot 0
: put =' '.vv.'! '.r
: endif
: endfor
: endfor
: call RecVars(oname)
: for v in ['wopts3', 'bopts3']
: try
: call E('del '.v.'["'.oname.'"]')
: catch
: put =' del '.v.'! '.v:exception
: endtry
: let r=E('del '.v.'["'.oname.'"]')
: if r isnot 0
: put =' del '.v.'! '.r
: endif
: endfor
: call RecVars(oname)
:endfor
@@ -651,6 +652,25 @@ for expr, attr in (
):
cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr)))
EOF
:"
:" Test exceptions
:fun Exe(e)
: execute a:e
:endfun
py << EOF
def ee(expr, g=globals(), l=locals()):
try:
exec(expr, g, l)
except:
cb.append(repr(sys.exc_info()[:2]))
Exe = vim.bindeval('function("Exe")')
ee('vim.command("throw \'abc\'")')
ee('Exe("throw \'def\'")')
ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")')
ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")')
ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")')
ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")')
EOF
:endfun
:"
:call Test()

View File

@@ -333,7 +333,7 @@ Number of tabs: 4
Current tab pages:
<tabpage 0>(1): 1 windows, current is <window object (unknown)>
Windows:
<window object (unknown)>(1): displays buffer <buffer test86.in>; cursor is at (970, 0)
<window object (unknown)>(1): displays buffer <buffer test86.in>; cursor is at (990, 0)
<tabpage 1>(2): 1 windows, current is <window object (unknown)>
Windows:
<window object (unknown)>(1): displays buffer <buffer 0>; cursor is at (1, 0)
@@ -368,3 +368,9 @@ vim.current.buffer:Buffer:True
vim.current.range:Range:True
vim.current.window:Window:True
vim.current.tabpage:TabPage:True
(<class 'vim.error'>, error('abc',))
(<class 'vim.error'>, error('def',))
(<class 'vim.error'>, error('ghi',))
(<class 'vim.error'>, error('Vim(echoerr):jkl',))
(<class 'vim.error'>, error('Vim:E492: Not an editor command: xxx_non_existent_command_xxx',))
(<class 'vim.error'>, error('Vim:E492: Not an editor command: xxx_non_existent_command_xxx',))

View File

@@ -367,20 +367,24 @@ def e(s, g=globals(), l=locals()):
try:
exec(s, g, l)
except Exception as e:
vim.command('throw ' + repr(e.__class__.__name__))
vim.command('return ' + repr(e.__class__.__name__))
def ev(s, g=globals(), l=locals()):
try:
return eval(s, g, l)
except Exception as e:
vim.command('throw ' + repr(e.__class__.__name__))
vim.command('let exc=' + repr(e.__class__.__name__))
return 0
EOF
:function E(s)
: python3 e(vim.eval('a:s'))
:endfunction
:function Ev(s)
: return py3eval('ev(vim.eval("a:s"))')
: let r=py3eval('ev(vim.eval("a:s"))')
: if exists('exc')
: throw exc
: endif
: return r
:endfunction
:py3 gopts1=vim.options
:py3 wopts1=vim.windows[2].options
@@ -424,27 +428,24 @@ EOF
: catch
: put =' p/'.v.'! '.v:exception
: endtry
: try
: call E(v.'["'.oname.'"]=invval')
: catch
: put =' inv: '.string(invval).'! '.v:exception
: endtry
: let r=E(v.'['''.oname.''']=invval')
: if r isnot 0
: put =' inv: '.string(invval).'! '.r
: endif
: for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3'])
: let val=substitute(vv, '^.opts', 'oval', '')
: try
: call E(vv.'["'.oname.'"]='.val)
: catch
: put =' '.vv.'! '.v:exception
: endtry
: let r=E(vv.'['''.oname.''']='.val)
: if r isnot 0
: put =' '.vv.'! '.r
: endif
: endfor
: endfor
: call RecVars(oname)
: for v in ['wopts3', 'bopts3']
: try
: call E('del '.v.'["'.oname.'"]')
: catch
: put =' del '.v.'! '.v:exception
: endtry
: let r=E('del '.v.'["'.oname.'"]')
: if r isnot 0
: put =' del '.v.'! '.r
: endif
: endfor
: call RecVars(oname)
:endfor
@@ -638,6 +639,25 @@ for expr, attr in (
):
cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr)))
EOF
:"
:" Test exceptions
:fun Exe(e)
: execute a:e
:endfun
py3 << EOF
def ee(expr, g=globals(), l=locals()):
try:
exec(expr, g, l)
except Exception as e:
cb.append(repr((e.__class__, e)))
Exe = vim.bindeval('function("Exe")')
ee('vim.command("throw \'abc\'")')
ee('Exe("throw \'def\'")')
ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")')
ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")')
ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")')
ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")')
EOF
:endfun
:"
:call Test()

View File

@@ -322,7 +322,7 @@ Number of tabs: 4
Current tab pages:
<tabpage 0>(1): 1 windows, current is <window object (unknown)>
Windows:
<window object (unknown)>(1): displays buffer <buffer test87.in>; cursor is at (946, 0)
<window object (unknown)>(1): displays buffer <buffer test87.in>; cursor is at (966, 0)
<tabpage 1>(2): 1 windows, current is <window object (unknown)>
Windows:
<window object (unknown)>(1): displays buffer <buffer 0>; cursor is at (1, 0)
@@ -357,3 +357,9 @@ vim.current.buffer:Buffer:True
vim.current.range:Range:True
vim.current.window:Window:True
vim.current.tabpage:TabPage:True
(<class 'vim.error'>, error('abc',))
(<class 'vim.error'>, error('def',))
(<class 'vim.error'>, error('ghi',))
(<class 'vim.error'>, error('Vim(echoerr):jkl',))
(<class 'vim.error'>, error('Vim:E492: Not an editor command: xxx_non_existent_command_xxx',))
(<class 'vim.error'>, error('Vim:E492: Not an editor command: xxx_non_existent_command_xxx',))