首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Django UnicodeError makemessages命令时出错

调用Django UnicodeError makemessages命令时出错
EN

Stack Overflow用户
提问于 2016-11-21 16:21:35
回答 2查看 924关注 0票数 6

我正在使用Django的国际化特性为webapp生成翻译字符串。

当我尝试调用makemessages时,出现了一个问题,而现有的语言.po文件包含一个特殊字符(如$£等)。

在其中一个存在的地方,makemessages尝试加载现有的.po文件并对其进行解码。当它这样做时,我会得到一个错误:

代码语言:javascript
复制
Traceback (most recent call last):
 File "manage.py", line 18, in <module>
   execute_from_command_line(sys.argv)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
   utility.execute()
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 346, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
   self.execute(*args, **cmd_options)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
   output = self.handle(*args, **options)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 325, in handle
   self.write_po_file(potfile, locale)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 458, in write_po_file
   msgs, errors, status = gettext_popen_wrapper(args)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 51, in gettext_popen_wrapper
   stdout = stdout.decode(stdout_encoding)
 File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
   return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa2' in position 2105: ordinal not in range(128)

我试着在这里挖回来,但我不知道发生了什么。

似乎Django试图将现有的.po文件解码为UTF8,但是当重新编码它时,它使用的是ASCII编解码器。

任何关于问题所在的见解都会受到极大的赞赏。

编辑:

  • 操作系统: Ubuntu 15.10和OS 10.11.6
  • Python: 2.7.10和2.7.11
  • Django: 1.8.14
  • 六: 1.10.0

我试着按照建议重新安装Django/6,但是错误仍然存在。

Ubuntu's localedef --list-archive

代码语言:javascript
复制
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8

问题翻译文件的内容类型:

代码语言:javascript
复制
 "Content-Type: text/plain; charset=UTF-8\n"
EN

回答 2

Stack Overflow用户

发布于 2016-11-26 14:47:45

注这是与注释中提到的https://stackoverflow.com/questions/22106777/unicode-issue-with-makemessages-all-django-1-6-2-python-3-3不同的异常位置。

在我看来,发生这种情况的唯一方法是,如果已经对django安装进行了修改,或者python2.7版本中有一个bug。

你的堆栈是:

代码语言:javascript
复制
> msgs, errors, status = gettext_popen_wrapper(args)
> stdout = stdout.decode(stdout_encoding)

gettext_popen_wrapper (在django 1.8上,我认为您正在使用它,您能确认吗?)和popen_wrapper,它创建了stdout (在删除了注释/文档字符串并为了清晰起见,请参阅github上的包装器包装器以获得未掺假的代码):

代码语言:javascript
复制
def popen_wrapper(args, os_err_exc_type=CommandError, universal_newlines=True):
    try:
        p = Popen(args, shell=False, stdout=PIPE, stderr=PIPE,
                close_fds=os.name != 'nt', universal_newlines=universal_newlines)
    except OSError as e:
        strerror = force_text(e.strerror, DEFAULT_LOCALE_ENCODING,
                              strings_only=True)
        six.reraise(os_err_exc_type, os_err_exc_type('Error executing %s: %s' %
                    (args[0], strerror)), sys.exc_info()[2])
    # NB: subprocess.Popen.communicate() should return two bytes 
    # (i.e. str in python 2) objects
    output, errors = p.communicate()
    return (
        output,
        force_text(errors, DEFAULT_LOCALE_ENCODING, strings_only=True),
        p.returncode
    )

def gettext_popen_wrapper(args, 
                          os_err_exc_type=CommandError, 
                          stdout_encoding="utf-8"):
    manual_io_wrapper = six.PY3 and stdout_encoding != DEFAULT_LOCALE_ENCODING

    stdout, stderr, status_code = popen_wrapper(
        args, os_err_exc_type=os_err_exc_type,
        universal_newlines=not manual_io_wrapper)

    if manual_io_wrapper:
        stdout = io.TextIOWrapper(io.BytesIO(stdout), encoding=stdout_encoding).read()
    if six.PY2:
        # EXCEPTION HIT ON THE FOLLOWING LINE
        stdout = stdout.decode(stdout_encoding)
    return stdout, stderr, status_code

因此,当我们调用stdout时,str应该是一个普通的stdout.decode()对象(即需要解码的一组字节)。但是,如果是这样的话,为什么en编码中会有例外呢?如果对象已经是unicode对象,即如果它是unicode类型,那么我们只需要对其进行编码。当然,如果我们加上这一行

代码语言:javascript
复制
stdout = stdout.decode('utf-8')

在此之前

代码语言:javascript
复制
stdout = stdout.decode(stdout_encoding)

然后,decode方法首先尝试使用默认的ascii编码来访问unicode stdout,这将导致您看到的异常。通过将manual_io_wrapper设置为True,我也得到了同样的错误,这也导致了stdout = io.TextWrapper(...)行的发生(这也会产生一个unicode ),但这不应该是True,因为您在python 2而不是3。

所以我想要么是:

  • djangosix的安装很糟糕,或者已经被编辑了。试着重新安装它们。
  • 您在subprocess.Popen.communicate()中遇到了一个bug,由于某种原因,它返回的是一个unicode,而不是一个str (我相信unicode3如果被打开的话,这是可能的。您可以通过重新安装python或升级到更高的版本来获得里程。

但我的主要观点是,我不认为这是一个环境问题。很有兴趣知道任何后续行动:

  • 你在哪个站台上
  • 你用的是什么python 2.7
  • 你在用什么姜戈。
票数 3
EN

Stack Overflow用户

发布于 2016-11-30 15:29:33

在下面的行中,不知何故,stdout不是字节str,而是它的unicode,在该unicode的隐式编码过程中,您将得到异常。

代码语言:javascript
复制
stdout = stdout.decode('utf-8')

这是因为decode()应该在字节str上执行,当我们尝试在unicode上调用decode时,在python2.7中,在decode之前会有一个对encode的隐式调用,而对encode的调用将使用默认的charset,在python中是ascii

代码语言:javascript
复制
unicode.encode() --> byte   # results in str
byte.decode() --> unicode   # results in unicode
unicode.decode() --> unicode.encode().decode()  # implicit encode call

所以,开始调查导致stdout成为unicode的原因。

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40724723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档