Ultisnips: UltiSnips crashes when python interpolation returns numeric value

Created on 7 Nov 2019  路  3Comments  路  Source: SirVer/ultisnips

Expected behavior:
I can write a python snippet that returns a numeric value, and that value gets printed out. Here is a convenient example.

snippet example "Copyright header" b
// TypeSafe Inc. LLC (c) `!p import datetime
snip.rv = datetime.datetime.now().year`
endsnippet

Actual behavior:
UltiSnips crashes.

An error occured. This is either a bug in UltiSnips or a bug in a
snippet definition. If you think this is a bug, please report it to
https://github.com/SirVer/ultisnips/issues/new
Please read and follow:
https://github.com/SirVer/ultisnips/blob/master/CONTRIBUTING.md#reproducing-bugs

Following is the full stack trace:
Traceback (most recent call last):
  File "/src/UltiSnips/pythonx/UltiSnips/err_to_scratch_buffer.py", line 18, in wrapper
    return func(self, *args, **kwds)
  File "/src/UltiSnips/pythonx/UltiSnips/snippet_manager.py", line 127, in expand
    if not self._try_expand():
  File "/src/UltiSnips/pythonx/UltiSnips/snippet_manager.py", line 759, in _try_expand
    self._do_snippet(snippet, before)
  File "/src/UltiSnips/pythonx/UltiSnips/snippet_manager.py", line 712, in _do_snippet
    text_before, self._visual_content, parent, start, end
  File "/src/UltiSnips/pythonx/UltiSnips/snippet/definition/base.py", line 488, in launch
    snippet_instance.update_textobjects(vim_helper.buf)
  File "/src/UltiSnips/pythonx/UltiSnips/text_objects/snippet_instance.py", line 93, in update_textobjects
    if obj._update(done, buf):
  File "/src/UltiSnips/pythonx/UltiSnips/text_objects/python_code.py", line 279, in _update
    self.overwrite(buf, rv)
  File "/src/UltiSnips/pythonx/UltiSnips/text_objects/base.py", line 146, in overwrite
    self._end = _replace_text(buf, self._start, self._end, gtext)
  File "/src/UltiSnips/pythonx/UltiSnips/text_objects/base.py", line 22, in _replace_text
    lines = text.split("\n")
AttributeError: 'int' object has no attribute 'split'

(Note that the actual snippet in question is one I have been using without issue for several years.)

Steps to reproduce

See branch: https://github.com/chiphogg/ultisnips/tree/chiphogg/1132/python-int-crash-repro

  • Start vim inside docker repro image.
  • Trigger example snippet.

answered or workaround exists low hopefully easy good first issue

Most helpful comment

@chiphogg Funny, I ended up at Lyft after my time at Google :)

On topic: I pushed a PR that should fix the issue.

All 3 comments

馃憢 @chiphogg Nice of you to come by. Still at Google?

I assume that this came with the recent removal of support for py2 - and removing the as_unicode function in some places. Should be very easy to fix and easy to work around (by just wrapping str() around the thing you assign to rv.

Hi @SirVer, good to "see" you again! :slightly_smiling_face:

I actually left Google in January of 2016. I've been at Uber ATG ever since.

@chiphogg Funny, I ended up at Lyft after my time at Google :)

On topic: I pushed a PR that should fix the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisrytting picture chrisrytting  路  7Comments

MikeDacre picture MikeDacre  路  3Comments

blackode picture blackode  路  5Comments

ghost picture ghost  路  8Comments

robenkleene picture robenkleene  路  8Comments