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 |
| 1 | from __future__ import unicode_literals |
2 | 2 | |
3 | 3 | import fnmatch |
4 | 4 | import glob |
… |
… |
def check_programs(*programs):
|
33 | 33 | "gettext tools 0.15 or newer installed." % program) |
34 | 34 | |
35 | 35 | |
36 | | def gettext_popen_wrapper(args, os_err_exc_type=CommandError): |
| 36 | def gettext_popen_wrapper(args, os_err_exc_type=CommandError, stdout_encoding="utf-8"): |
37 | 37 | """ |
38 | 38 | Makes sure text obtained from stdout of gettext utilities is Unicode. |
39 | 39 | """ |
40 | 40 | 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: |
42 | 43 | # This looks weird because it's undoing what subprocess.Popen(universal_newlines=True).communicate() |
43 | 44 | # 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) |
46 | 47 | if six.PY2: |
47 | | stdout = stdout.decode('utf-8') |
| 48 | stdout = stdout.decode(stdout_encoding) |
48 | 49 | return stdout, stderr, status_code |
49 | 50 | |
50 | 51 | |
… |
… |
class Command(BaseCommand):
|
325 | 326 | |
326 | 327 | @cached_property |
327 | 328 | 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) |
329 | 335 | m = re.search(r'(\d+)\.(\d+)\.?(\d+)?', out) |
330 | 336 | if m: |
331 | 337 | return tuple(int(d) for d in m.groups() if d is not None) |
… |
… |
class Command(BaseCommand):
|
341 | 347 | for f in file_list: |
342 | 348 | try: |
343 | 349 | 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)) |
346 | 352 | |
347 | 353 | potfiles = [] |
348 | 354 | for path in self.locale_paths: |