从Aptana Studio PyDev运行时取消抑制UnicodeEncodeError异常 [英] Unsuppress UnicodeEncodeError exceptions when run from Aptana Studio PyDev

查看:35
本文介绍了从Aptana Studio PyDev运行时取消抑制UnicodeEncodeError异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是应引发 UnicodeEncodeError 异常的语句:

The following is a statement that should raise an UnicodeEncodeError exception:

print 'str+{}'.format(u'unicode:\u2019')

在Python shell中,异常按预期引发:

In a Python shell, the exception is raised as expected:

>>> print 'str+{}'.format(u'unicode:\u2019')

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    print 'str+{}'.format(u'unicode:\u2019')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 8: ordinal not in range(128)

但是,如果我将该行放在 settings.py 的开头并从Aptana Studio启动Django服务器,则不会出现错误,并且会打印以下行:

However, if I place that line at the start of my settings.py and start the Django server from Aptana Studio, no error is raised and this line is printed:

str+unicode:’

但是,如果我从外壳执行 manage.py runserver ,则会引发异常:

But if I execute manage.py runserver from a shell, the exception is raised:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 8: ordinal not in range(128)

是否有某种Python设置可以静默抑制这些unicode错误?

Is there some sort of Python setting that silently suppresses these unicode errors?

当我直接从Aptana Studio启动Django测试服务器时,如何防止unicode错误被忽略?

How can I prevent the unicode error from being ignored when I start the Django test server directly from Aptana Studio?

使用

  • Python 2.7.3
  • Aptana Studio 3.3.2

推荐答案

如果您只是将字节串转换为unicode,例如

If you simply cast a bytestring to unicode, like

print unicode(s)

或在像示例一样的字符串格式操作中混合unicode和字节串,Python将使用系统默认编码(除非已更改,否则为 ascii ),并且将隐式尝试对unicode进行编码/使用 ascii 编解码器解码字节串.

or mix unicode and bytestrings in string formatting operations like your example, Python will fall back on the system default encoding (which is ascii unless it has been changed), and implicitly will try to encode unicode / decode the bytestring using the ascii codec.

当前活动的系统默认编码可以显示为

The currently active system default encoding can be displayed with

import sys
sys.getdefaultencoding()

现在看来,Aptana Studio实际上确实与您的解释器的默认编码混淆了:

Now it seems like Aptana Studio does in fact mess with your interpreters default encoding:

来自Mikko Ohtamaa的博客文章:

From a blog post by Mikko Ohtamaa:

[...]看起来像是PyDev(Eclipse Python插件).这干扰源代码是此处.看起来原因是与Eclipse控制台合作.然而它做错了.无需设置控制台编码,编码设置为整个Python运行时环境,搞乱了完成开发的目标运行时间.

[...] Looks like the culprint was PyDev (Eclipse Python plug-in). The interfering source code is here. Looks like the reason was to co-operate with Eclipse console. However it has been done incorrectly. Instead of setting the console encoding, the encoding is set to whole Python run-time environment, messing up the target run-time where the development is being done.

有可能解决此问题.在 Eclipse Run…对话框设置中,可以在 Common 选项卡上选择 Console Encoding .那里是可能的值US-ASCII.我不确定Python 2的想法"US-ASCII"编码名称,因为默认值为" ascii ".

There is a possible fix for this problem. In Eclipse Run… dialog settings you can choose Console Encoding on Common tab. There is a possible value US-ASCII. I am not sure what Python 2 thinks "US-ASCII" encoding name, since the default is "ascii".

因此,请确保将默认值重置为 ascii ,并且应该不错.

So make sure you reset the default to ascii, and you should be good.

这篇关于从Aptana Studio PyDev运行时取消抑制UnicodeEncodeError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆