From 99a6f0e77c6e02b310687667d41df56c17b953bf Mon Sep 17 00:00:00 2001 From: Tai Lee Date: Mon, 6 May 2013 13:32:07 +1000 Subject: [PATCH] Fixed #20354 -- `makemessages` no longer crashes with `UnicodeDecodeError` Handle the `UnicodeDecodeError` exception, send a warning to `stdout` with the file name and location, and continue processing other files. --- .../core/management/commands/makemessages.py | 5 ++++- tests/i18n/commands/extraction.py | 22 +++++++++++++------ tests/i18n/commands/not_utf8.sample | 1 + 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 tests/i18n/commands/not_utf8.sample diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index a7e98173ac69..bc171176c218 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -294,7 +294,10 @@ def build_pot_file(self, localedir): os.unlink(potfile) for f in file_list: - f.process(self, potfile, self.domain, self.keep_pot) + try: + f.process(self, potfile, self.domain, self.keep_pot) + except UnicodeDecodeError: + self.stdout.write("UnicodeDecodeError: skipped file %s in %s" % (f.file, f.dirpath)) return potfile def find_files(self, root): diff --git a/tests/i18n/commands/extraction.py b/tests/i18n/commands/extraction.py index 80e4ee011053..7c482e58fb35 100644 --- a/tests/i18n/commands/extraction.py +++ b/tests/i18n/commands/extraction.py @@ -30,6 +30,10 @@ def _rmrf(self, dname): return shutil.rmtree(dname) + def rmfile(self, filepath): + if os.path.exists(filepath): + os.remove(filepath) + def tearDown(self): os.chdir(self.test_dir) try: @@ -126,18 +130,22 @@ def test_extraction_error(self): # Check that the temporary file was cleaned up self.assertFalse(os.path.exists('./templates/template_with_error.tpl.py')) + def test_unicode_decode_error(self): + os.chdir(self.test_dir) + shutil.copyfile('./not_utf8.sample', './not_utf8.txt') + self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'not_utf8.txt')) + stdout = StringIO() + management.call_command('makemessages', locale=LOCALE, stdout=stdout) + self.assertIn("UnicodeDecodeError: skipped file not_utf8.txt in .", + force_text(stdout.getvalue())) + def test_extraction_warning(self): """test xgettext warning about multiple bare interpolation placeholders""" os.chdir(self.test_dir) shutil.copyfile('./code.sample', './code_sample.py') + self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'code_sample.py')) stdout = StringIO() - try: - management.call_command('makemessages', locale=LOCALE, stdout=stdout) - finally: - try: - os.remove('./code_sample.py') - except OSError: - pass + management.call_command('makemessages', locale=LOCALE, stdout=stdout) self.assertIn("code_sample.py:4", force_text(stdout.getvalue())) def test_template_message_context_extractor(self): diff --git a/tests/i18n/commands/not_utf8.sample b/tests/i18n/commands/not_utf8.sample new file mode 100644 index 000000000000..6449f52803d6 --- /dev/null +++ b/tests/i18n/commands/not_utf8.sample @@ -0,0 +1 @@ +Copyright (c) 2009 Øyvind Sean Kinsey, oyvind@kinsey.no \ No newline at end of file