Only use on dictionary in exec(). This works around wierd scoping for list comprehensions.

This commit is contained in:
Holger Rapp 2014-02-14 19:28:29 +01:00
parent f6a82c13e4
commit 635dc63722
2 changed files with 16 additions and 20 deletions

View File

@ -162,7 +162,6 @@ class PythonCode(NoneditableTextObject):
"""See module docstring.""" """See module docstring."""
def __init__(self, parent, token): def __init__(self, parent, token):
code = token.code.replace("\\`", "`")
# Find our containing snippet for snippet local data # Find our containing snippet for snippet local data
snippet = parent snippet = parent
@ -176,39 +175,31 @@ class PythonCode(NoneditableTextObject):
snippet = snippet._parent # pylint:disable=protected-access snippet = snippet._parent # pylint:disable=protected-access
self._snip = SnippetUtil(token.indent, mode, text) self._snip = SnippetUtil(token.indent, mode, text)
self._globals = {} self._code = "\n".join((
globals = snippet.globals.get("!p", []) "import re, os, vim, string, random",
exec("\n".join(globals).replace("\r\n", "\n"), self._globals) # pylint:disable=exec-used "\n".join(snippet.globals.get("!p", [])).replace("\r\n", "\n"),
token.code.replace("\\`", "`")
# Add Some convenience to the code ))
self._code = "import re, os, vim, string, random\n" + code
NoneditableTextObject.__init__(self, parent, token) NoneditableTextObject.__init__(self, parent, token)
def _update(self, done): def _update(self, done):
path = _vim.eval('expand("%")') path = _vim.eval('expand("%")') or ""
if path is None:
path = ""
fn = os.path.basename(path)
ct = self.current_text ct = self.current_text
self._snip._reset(ct) # pylint:disable=protected-access self._locals.update({
local_d = self._locals
local_d.update({
't': _Tabs(self._parent), 't': _Tabs(self._parent),
'fn': fn, 'fn': os.path.basename(path),
'path': path, 'path': path,
'cur': ct, 'cur': ct,
'res': ct, 'res': ct,
'snip': self._snip, 'snip': self._snip,
}) })
self._snip._reset(ct) # pylint:disable=protected-access
exec(self._code, self._globals, local_d) # pylint:disable=exec-used exec(self._code, self._locals) # pylint:disable=exec-used
rv = as_unicode( rv = as_unicode(
self._snip.rv if self._snip._rv_changed # pylint:disable=protected-access self._snip.rv if self._snip._rv_changed # pylint:disable=protected-access
else as_unicode(local_d['res']) else as_unicode(self._locals['res'])
) )
if ct != rv: if ct != rv:

View File

@ -1227,6 +1227,11 @@ class PythonVisual_LineSelect_Simple(_VimTest):
keys = "hello\nnice\nworld" + ESC + "Vkk" + EX + "test" + EX keys = "hello\nnice\nworld" + ESC + "Vkk" + EX + "test" + EX
wanted = "hVhello\nnice\nworld\nb" wanted = "hVhello\nnice\nworld\nb"
# Tests for https://bugs.launchpad.net/bugs/1259349
class Python_WeirdScoping_Error(_VimTest):
snippets = ("test", "h`!p import re; snip.rv = '%i' % len([re.search for i in 'aiiia'])`b")
keys = "test" + EX
wanted = "h5b"
# End: New Implementation #}}} # End: New Implementation #}}}
# End: PythonCode Interpolation #}}} # End: PythonCode Interpolation #}}}
# Mirrors {{{# # Mirrors {{{#