Skip to content

Commit

Permalink
Only use on dictionary in exec(). This works around wierd scoping for…
Browse files Browse the repository at this point in the history
… list comprehensions.
  • Loading branch information
SirVer committed Feb 14, 2014
1 parent f6a82c1 commit 635dc63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
31 changes: 11 additions & 20 deletions pythonx/UltiSnips/text_objects/_python_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ class PythonCode(NoneditableTextObject):
"""See module docstring."""

def __init__(self, parent, token):
code = token.code.replace("\\`", "`")

# Find our containing snippet for snippet local data
snippet = parent
Expand All @@ -176,39 +175,31 @@ def __init__(self, parent, token):
snippet = snippet._parent # pylint:disable=protected-access
self._snip = SnippetUtil(token.indent, mode, text)

self._globals = {}
globals = snippet.globals.get("!p", [])
exec("\n".join(globals).replace("\r\n", "\n"), self._globals) # pylint:disable=exec-used

# Add Some convenience to the code
self._code = "import re, os, vim, string, random\n" + code

self._code = "\n".join((
"import re, os, vim, string, random",
"\n".join(snippet.globals.get("!p", [])).replace("\r\n", "\n"),
token.code.replace("\\`", "`")
))
NoneditableTextObject.__init__(self, parent, token)

def _update(self, done):
path = _vim.eval('expand("%")')
if path is None:
path = ""
fn = os.path.basename(path)

path = _vim.eval('expand("%")') or ""
ct = self.current_text
self._snip._reset(ct) # pylint:disable=protected-access
local_d = self._locals

local_d.update({
self._locals.update({
't': _Tabs(self._parent),
'fn': fn,
'fn': os.path.basename(path),
'path': path,
'cur': ct,
'res': ct,
'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(
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:
Expand Down
5 changes: 5 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,11 @@ class PythonVisual_LineSelect_Simple(_VimTest):
keys = "hello\nnice\nworld" + ESC + "Vkk" + EX + "test" + EX
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: PythonCode Interpolation #}}}
# Mirrors {{{#
Expand Down

0 comments on commit 635dc63

Please sign in to comment.