diff options
| author | Claude Paroz <claude@2xlibre.net> | 2023-03-12 12:28:00 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-03-12 16:54:20 +0100 |
| commit | 03bc92af978f2148c1141673c730f17772cd7037 (patch) | |
| tree | b07350231d8d9193c7f9790dc6f55ecc56cfb768 | |
| parent | 4db33e96d1b5b4ba1a739e15b28835495f78eee4 (diff) | |
Fixed #34407 -- Reported filename when decoding fails in collectstatic's post_process.
| -rw-r--r-- | django/contrib/staticfiles/storage.py | 5 | ||||
| -rw-r--r-- | tests/staticfiles_tests/project/nonutf8/nonutf8.css | 2 | ||||
| -rw-r--r-- | tests/staticfiles_tests/test_storage.py | 12 |
3 files changed, 18 insertions, 1 deletions
diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index b3ee32e665..df02906ebc 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -353,7 +353,10 @@ class HashedFilesMixin: # ..to apply each replacement pattern to the content if name in adjustable_paths: old_hashed_name = hashed_name - content = original_file.read().decode("utf-8") + try: + content = original_file.read().decode("utf-8") + except UnicodeDecodeError as exc: + yield name, None, exc, False for extension, patterns in self._patterns.items(): if matches_patterns(path, (extension,)): for pattern, template in patterns: diff --git a/tests/staticfiles_tests/project/nonutf8/nonutf8.css b/tests/staticfiles_tests/project/nonutf8/nonutf8.css new file mode 100644 index 0000000000..4d5e729088 --- /dev/null +++ b/tests/staticfiles_tests/project/nonutf8/nonutf8.css @@ -0,0 +1,2 @@ +/* éviter écrasement */ +.test { margin: 1 rem; } diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py index 06df05e196..759582122d 100644 --- a/tests/staticfiles_tests/test_storage.py +++ b/tests/staticfiles_tests/test_storage.py @@ -368,6 +368,18 @@ class TestHashedFiles: self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue()) self.assertPostCondition() + @override_settings( + STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "nonutf8")], + STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"], + ) + def test_post_processing_nonutf8(self): + finders.get_finder.cache_clear() + err = StringIO() + with self.assertRaises(UnicodeDecodeError): + call_command("collectstatic", interactive=False, verbosity=0, stderr=err) + self.assertEqual("Post-processing 'nonutf8.css' failed!\n\n", err.getvalue()) + self.assertPostCondition() + @override_settings( STORAGES={ |
