Thursday, July 15, 2010

Limitations of vim-python interface with respect to character encodings

I've recently discovered that there are some limitations with respect
to using the python interface to bring non-ascii characters back into
vim.

For example, this command works as expected:

:py vim.command(u'echo "\u0061"')
a

However this one gives the following error message:

:py vim.command(u'echo "\u2026"')
Traceback (most recent call last):
File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 10-11: ordinal not in range(128)

Other functions seem to be even more restricted:

:py vim.current.range.append(u'\u0061')
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: bad argument type for built-in operation

Even output from python is restricted:

:py print u'echo "\u0061"'
echo "a"
:py print u'echo "\u2026"'
Traceback (most recent call last):
File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
position 6: ordinal not in range(128)

I've discovered that this last issue can be remedied via

import sys, codecs
sys.stdout, _held = (codecs.getwriter('utf-8')(sys.stdout),
sys.stdout)

Setting python's default encoding from within /etc/python2.6/
sitecustomize.py has a similar effect.

These last two workarounds only fix issues with printing; issues with
appending to buffers are not affected by either of these changes.

Is it possible to fix this by reconfiguring something within Python,
or is this a limitation of the vim-python interface?

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: