Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when inserting python snippets #6

Open
riquems opened this issue Jul 29, 2024 · 1 comment
Open

Error when inserting python snippets #6

riquems opened this issue Jul 29, 2024 · 1 comment

Comments

@riquems
Copy link

riquems commented Jul 29, 2024

Hello, I'm trying to use the python snippets but they are not working on my end, I've tried using html and csharp snippet already. One csharp snippet for example is the "class" one, that does this when I hit Tab:

function provider#python3#Call, line 1: Vim(return):E5108: Error executing lua Vim:Error invoking 'python_eval' on channel 3 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'luasnip_snippets_python_helper' is not defined
stack traceback:
^I[C]: at 0x555cbad29e70
^I[C]: in function 'py3eval'
^I...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:296: in function 'c_py'
^I...e/nvim/lazy/luasnip-snippets/lua/luasnip_snippets/cs.lua:143: in function 'fn'
^I...hare/nvim/lazy/LuaSnip/lua/luasnip/nodes/dynamicNode.lua:152: in function 'update'
^I...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:1026: in function 'update'
^I...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:769: in function 'trigger_expand'
^I...h140/.local/share/nvim/lazy/LuaSnip/lua/luasnip/init.lua:272: in function 'snip_expand'
^I...cal/share/nvim/lazy/cmp_luasnip/lua/cmp_luasnip/init.lua:169: in function 'execute'
^I../<user>/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:396: in function 'execute'
^I.../<user>/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:492: in function 'execute'
^I...e/<user>/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:503: in function <...e/<user>/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:502>
^I[C]: in function 'pcall'
^I...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function 'run'
^I[string ":lua"]:1: in main chunk

Since it's a "name is not defined" error I looked into the file snip_utils.lua:296 the name that it looks for and tried to import it using vim command bar with python3 import luasnip_snippets_python_helper and this happens:

E5108: Error executing lua Vim:Error invoking 'python_execute' on channel 3 (python3-script-host):                                                                 Traceback (most recent call last):                                                                                                                                   
File "<string>", line 1, in <module>                                                                                                                               
File "/home/<user>/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 300, in <module>                                          @functools.cache                                                                                                                                               
AttributeError: module 'functools' has no attribute 'cache'                                                                                                        
stack traceback:                                                                                                                                                          
 [C]: at 0x555cbad29e70

From my search, it looks like functools 'cache' came only after Python 3.9, however updating it didn't work for me (I don't know if I did it right, but I downloaded Python 3.9, Python 3.12 and updated vim.g.python3_host_prog accordingly), it gives me this error when it's not set to "/usr/bin/python3.8":

nvim_exec2()..function provider#python3#Call[1]..remote#host#Require[10]..provider#python3#Require, line 1: Vim(return):E5108: Error executing lua nvim_exec2()..fu
nction provider#python3#Call[1]..remote#host#Require[10]..provider#python3#Require[1]..provider#Poll, line 17: Failed to load python3 host. You can try to see what
 happened by starting nvim with $NVIM_PYTHON_LOG_FILE set and opening the generated log file. Also, the host stderr is available in messages.
stack traceback:
        [C]: at 0x562df51efe70
        [C]: at 0x562df51efe70
        [C]: in function 'pcall'
        ...m-linux64/share/nvim/runtime/lua/vim/provider/python.lua:132: in function <...m-linux64/share/nvim/runtime/lua/vim/provider/python.lua:125>
        [C]: in function 'nvim_exec2'
        vim/_editor.lua: in function 'cmd'
        ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:289: in function 'load_python_helper'
        ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:294: in function 'c_py'
        ...e/nvim/lazy/luasnip-snippets/lua/luasnip_snippets/cs.lua:143: in function <...e/nvim/lazy/luasnip-snippets/lua/luasnip_snippets/cs.lua:143>
Error while evaluating dynamicNode@3 for snippet 'class':
...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:62: invalid value (userdata) at index 1 in table for 'concat'

One solution for me, according to the answer of this stackoverflow thread, was to use lru_cache instead of cache in luasnip_snippets_python_helper.py:297

-@functools.cache
+@functools.lru_cache
 def cached_compile(*args):
        return compile(*args)

I don't know if that's a fix so I'm not doing a PR or anything yet, is there anything I can do to fix my end and probably help others as well? (maybe be explicit Python 3.9 >= is required?) or isn't it?

Thanks in advance.

Btw I am on WSL2 Ubuntu 20.04

mireq added a commit that referenced this issue Sep 7, 2024
This allows to run code with older python versions (before 3.9)

refs #6
@mireq
Copy link
Owner

mireq commented Sep 7, 2024

@riquems thanks for reporting.

cache is equivalent of lru_cache with unlimited size (maxsize=None). I have replaced cache function with lru_cache. Try it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants