[1.5.x] 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. Backport of 99a6f0e77 from master.
This commit is contained in:
parent
acd9dc3888
commit
23b234a9d9
@ -324,8 +324,11 @@ def make_messages(locale=None, domain='django', verbosity=1, all=False,
|
|||||||
|
|
||||||
for dirpath, file in find_files(".", ignore_patterns, verbosity,
|
for dirpath, file in find_files(".", ignore_patterns, verbosity,
|
||||||
stdout, symlinks=symlinks):
|
stdout, symlinks=symlinks):
|
||||||
process_file(file, dirpath, potfile, domain, verbosity, extensions,
|
try:
|
||||||
wrap, location, stdout)
|
process_file(file, dirpath, potfile, domain, verbosity, extensions,
|
||||||
|
wrap, location, stdout)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
stdout.write("UnicodeDecodeError: skipped file %s in %s" % (file, dirpath))
|
||||||
|
|
||||||
if os.path.exists(potfile):
|
if os.path.exists(potfile):
|
||||||
write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
|
write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
|
||||||
|
@ -27,6 +27,10 @@ class ExtractorTests(TestCase):
|
|||||||
return
|
return
|
||||||
shutil.rmtree(dname)
|
shutil.rmtree(dname)
|
||||||
|
|
||||||
|
def rmfile(self, filepath):
|
||||||
|
if os.path.exists(filepath):
|
||||||
|
os.remove(filepath)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
os.chdir(self.test_dir)
|
os.chdir(self.test_dir)
|
||||||
try:
|
try:
|
||||||
@ -120,12 +124,21 @@ class BasicExtractorTests(ExtractorTests):
|
|||||||
# Check that the temporary file was cleaned up
|
# Check that the temporary file was cleaned up
|
||||||
self.assertFalse(os.path.exists('./templates/template_with_error.html.py'))
|
self.assertFalse(os.path.exists('./templates/template_with_error.html.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):
|
def test_extraction_warning(self):
|
||||||
os.chdir(self.test_dir)
|
os.chdir(self.test_dir)
|
||||||
shutil.copyfile('./code.sample', './code_sample.py')
|
shutil.copyfile('./code.sample', './code_sample.py')
|
||||||
|
self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'code_sample.py'))
|
||||||
stdout = StringIO()
|
stdout = StringIO()
|
||||||
management.call_command('makemessages', locale=LOCALE, stdout=stdout)
|
management.call_command('makemessages', locale=LOCALE, stdout=stdout)
|
||||||
os.remove('./code_sample.py')
|
|
||||||
self.assertIn("code_sample.py:4", force_text(stdout.getvalue()))
|
self.assertIn("code_sample.py:4", force_text(stdout.getvalue()))
|
||||||
|
|
||||||
def test_template_message_context_extractor(self):
|
def test_template_message_context_extractor(self):
|
||||||
|
1
tests/regressiontests/i18n/commands/not_utf8.sample
Normal file
1
tests/regressiontests/i18n/commands/not_utf8.sample
Normal file
@ -0,0 +1 @@
|
|||||||
|
Copyright (c) 2009 Øyvind Sean Kinsey, oyvind@kinsey.no
|
Loading…
x
Reference in New Issue
Block a user