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

tempfile.NamedTemporaryFile 缺少 encoding 时 出错 #156

Closed
go2chenhua opened this issue May 12, 2018 · 15 comments
Closed

tempfile.NamedTemporaryFile 缺少 encoding 时 出错 #156

go2chenhua opened this issue May 12, 2018 · 15 comments
Labels

Comments

@go2chenhua
Copy link

当 未保存文档 出现:

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
Copy link
Owner

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

@go2chenhua
Copy link
Author

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

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

@Yggdroot
Copy link
Owner

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

@go2chenhua
Copy link
Author

go2chenhua commented May 13, 2018 via email

@Yggdroot
Copy link
Owner

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

@Yggdroot Yggdroot added the bug label May 13, 2018
@go2chenhua
Copy link
Author

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
Copy link
Author

以上代码保存为 utf-8的

@go2chenhua
Copy link
Author

py2 文档 添加

coding: utf8

可以正常运行.

@go2chenhua
Copy link
Author

# coding: utf8

@Yggdroot
Copy link
Owner

#!/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 added a commit that referenced this issue May 13, 2018
tempfile.NamedTemporaryFile 缺少 encoding 时 出错
@Yggdroot
Copy link
Owner

修复了。

@go2chenhua
Copy link
Author

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

@go2chenhua
Copy link
Author

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
Copy link
Owner

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

@go2chenhua
Copy link
Author

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

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

No branches or pull requests

2 participants