Yggdroot / LeaderF

An efficient fuzzy finder that helps to locate files, buffers, mrus, gtags, etc. on the fly for both vim and neovim.
Apache License 2.0
2.14k stars 178 forks source link

tempfile.NamedTemporaryFile 缺少 encoding 时 出错 #156

Closed go2chenhua closed 6 years ago

go2chenhua commented 6 years ago

当 未保存文档 出现:

set listchars=tab:→\ ,nbsp:␣,trail:•,extends:⟩,precedes:⟨

image

使用 utf-8 解决

        with tempfile.NamedTemporaryFile(mode='w+',
                                         encoding='utf-8',
                                         suffix='_'+os.path.basename(buffer.name),
                                         delete=False) as f:
Yggdroot commented 6 years ago

为什么关了? 这个问题是怎么复现的?你的vimrc里面设置了set encoding=utf-8了吗?

go2chenhua commented 6 years ago

'encoding' 'enc' Removed. |vim-differences| {Nvim} Nvim always uses UTF-8 internally. RPC communication (remote plugins/GUIs) must use UTF-8 strings.

好像添加 一行 代码就解决了, 所以 关了.

Yggdroot commented 6 years ago

你使用的是Python3,Python2的NamedTemporaryFile()没有encoding这个参数。 所以对于插件的修改,我不能简单的加上这么一句。而且还要考虑vim(not neovim)上,没有设置set encoding=utf-8的情况。

go2chenhua commented 6 years ago

这样啊. 我看了下代码, 好像为了用户的需求牺牲了性能. 还是不要修改好. 我自己本地改一下就行. 希望不要像 denite 那样, 越改越复杂. leaderf 明显要快. 用了一下你的 fuzzyMatchC, 希望将来可以用在 deoplete 上. thx.

On Sun, May 13, 2018 at 9:46 AM, Yggdroot notifications@github.com wrote:

你使用的是Python3,Python2的NamedTemporaryFile()没有encoding这个参数。 所以对于插件的修改,我不能简单的加上这么一句。而且还要考虑vim(not neovim)上,没有设置set encoding=utf-8的情况。

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/Yggdroot/LeaderF/issues/156#issuecomment-388595162, or mute the thread https://github.com/notifications/unsubscribe-auth/AD8RYPXxxP7-celXwWuRldDgCGuhtqwuks5tx5CMgaJpZM4T8epP .

Yggdroot commented 6 years ago

我在gvim上复现了,只有在是Python3的时候才会有错,你能帮忙确认一下你那边使用Python2是否报错吗?

go2chenhua commented 6 years ago
from tempfile import NamedTemporaryFile

with NamedTemporaryFile(mode='w+', delete=False, suffix='xx_') as f:
    f.write("""set listchars=tab:→\ ,nbsp:␣,trail:•,extends:⟩,precedes:⟨""")
    # f.write("""xxxx""")

其实直接使用 py3 运行就会 报错. py2:

λ py2 D:/xx/Python/py3/py1.py
  File "D:/xx/Python/py3/py1.py", line 5
SyntaxError: Non-ASCII character '\xe2' in file D:/xx/Python/py3/py1.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

别说运行, comment 掉 都不能运行.

go2chenhua commented 6 years ago

以上代码保存为 utf-8的

go2chenhua commented 6 years ago

py2 文档 添加

coding: utf8

可以正常运行.

go2chenhua commented 6 years ago
# coding: utf8
Yggdroot commented 6 years ago
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tempfile import NamedTemporaryFile

with NamedTemporaryFile(mode='w+', delete=False, suffix='xx_') as f:
    f.write("""set listchars=tab:→\ ,nbsp:␣,trail:•,extends:⟩,precedes:⟨""")
    # f.write("""xxxx""")

有非ASCII字符,需要在代码开头加上# -*- coding: utf-8 -*-指定编码。你上面说的Python2的问题是一个common issue,不是NamedTemporaryFile的问题。 如果按照你这样测试的话,py2上是没有问题。 我在py3上修复一下。

Yggdroot commented 6 years ago

修复了。

go2chenhua commented 6 years ago

tempfile 频繁 读写, 不是很消耗资源吗? 再加上 动态运行求值... 反正我在本地删了这些兼容性代码. :smile:

go2chenhua commented 6 years ago
from tempfile import NamedTemporaryFile
from functools import partial
if 3 > 2:
    fn = partial(NamedTemporaryFile, encoding='utf-8')
else:
    fn = NamedTemporaryFile

with fn(mode='w+', delete=False, suffix='_xx_') as f:
    f.write("ddddddddddddset listchars=tab:→\ ,nbsp:␣,trail:•,extends:⟩,precedes:⟨")

if 一次, 不用每次 if 顺便减少 .call

Yggdroot commented 6 years ago

首先,一个代码文件一般不是很大,这些操作在现代计算机上都会很快,一般不会有什么感觉。 其次,临时文件不是每次都使用的,当你代码有改动,但是还没有保存的时候才会用临时文件,这样保证你新加的一个函数也能看到。如果代码保存了,直接用磁盘上的文件,不会用临时文件。

go2chenhua commented 6 years ago

我知道未保存才用tempfile, 其实我之前就搜 能否 让 ctags daemon 运行, 别人早就提了. 是我想太多了吧, 也可能是我更关心我的电脑吧. 其实我是担心电费而已. 就这样吧.