Ticket #24500: 0001-Fix-gettext-tools-output-encoding-troubles.patch

File 0001-Fix-gettext-tools-output-encoding-troubles.patch, 3.3 KB (added by pascal chambon, 10 years ago)
  • django/core/management/commands/makemessages.py

    From c29b5f75f032d7d43e56ee4b5afb87dd019fa58d Mon Sep 17 00:00:00 2001
    From: Pakal <chambon.pascal@gmail.com>
    Date: Thu, 19 Mar 2015 22:47:24 +0100
    Subject: [PATCH] Fix gettext tools output encoding troubles.
    
    ---
     django/core/management/commands/makemessages.py | 24 +++++++++++++++---------
     1 file changed, 15 insertions(+), 9 deletions(-)
    
    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    index cbe4a45..3b24bf3 100644
    a b  
    1 from __future__ import unicode_literals
     1from __future__ import unicode_literals
    22
    33import fnmatch
    44import glob
    def check_programs(*programs):  
    3333                    "gettext tools 0.15 or newer installed." % program)
    3434
    3535
    36 def gettext_popen_wrapper(args, os_err_exc_type=CommandError):
     36def gettext_popen_wrapper(args, os_err_exc_type=CommandError, stdout_encoding="utf-8"):
    3737    """
    3838    Makes sure text obtained from stdout of gettext utilities is Unicode.
    3939    """
    4040    stdout, stderr, status_code = popen_wrapper(args, os_err_exc_type=os_err_exc_type)
    41     if os.name == 'nt' and six.PY3:
     41    preferred_encoding = locale.getpreferredencoding(False)
     42    if os.name == 'nt' and six.PY3 and stdout_encoding != preferred_encoding:
    4243        # This looks weird because it's undoing what subprocess.Popen(universal_newlines=True).communicate()
    4344        # does when capturing PO files contents from stdout of gettext command line programs. See ticket #23271
    44         # for details. No need to do anything on Python 2 because it's already a UTF-8-encoded byte-string there
    45         stdout = stdout.encode(locale.getpreferredencoding(False)).decode('utf-8')
     45        # for details. No need to do anything on Python 2 because it's already a byte-string there
     46        stdout = stdout.encode(preferred_encoding).decode(stdout_encoding)
    4647    if six.PY2:
    47         stdout = stdout.decode('utf-8')
     48        stdout = stdout.decode(stdout_encoding)
    4849    return stdout, stderr, status_code
    4950
    5051
    class Command(BaseCommand):  
    325326
    326327    @cached_property
    327328    def gettext_version(self):
    328         out, err, status = gettext_popen_wrapper(['xgettext', '--version'])
     329        # Gettext tools will output system-encoded bytestrings instead of UTF-8,
     330        # when looking up their version. It's especially a problem on windows.
     331        # See ticket 24500 for details.
     332        stdout_encoding = locale.getpreferredencoding(False)
     333        out, err, status = gettext_popen_wrapper(['xgettext', '--version'],
     334                                                 stdout_encoding=stdout_encoding)
    329335        m = re.search(r'(\d+)\.(\d+)\.?(\d+)?', out)
    330336        if m:
    331337            return tuple(int(d) for d in m.groups() if d is not None)
    class Command(BaseCommand):  
    341347        for f in file_list:
    342348            try:
    343349                f.process(self, self.domain)
    344             except UnicodeDecodeError:
    345                 self.stdout.write("UnicodeDecodeError: skipped file %s in %s" % (f.file, f.dirpath))
     350            except UnicodeDecodeError as e:
     351                self.stdout.write("UnicodeDecodeError: skipped file %s in %s (reason: %s)" % (f.file, f.dirpath, e))
    346352
    347353        potfiles = []
    348354        for path in self.locale_paths:
Back to Top