Skip to content

Commit 69820b0

Browse files
committed
Fix string encoding in compiler and compressor errors.
The errors raised when a compiler or compressor failed were including the message as bytes instead of str. This meant that when printing the errors, or seeing them in the django-pipeline-error element that's rendered to the page, the error would be shown as the repr of the bytestring (which did not properly show newlines, and included extra b'' junk). This change fixes things to decode to str when appropriate.
1 parent b5e35f2 commit 69820b0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pipeline/compilers/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.staticfiles import finders
77
from django.contrib.staticfiles.storage import staticfiles_storage
88
from django.core.files.base import ContentFile
9+
from django.utils.encoding import force_str
910

1011
from pipeline.conf import settings
1112
from pipeline.exceptions import CompilerError
@@ -119,7 +120,8 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
119120
# We always catch stdout in a file, but we may not have a use for it.
120121
temp_file_container = cwd or os.path.dirname(stdout_captured or "") or os.getcwd()
121122
with NamedTemporaryFile('wb', delete=False, dir=temp_file_container) as stdout:
122-
compiling = subprocess.Popen(argument_list, cwd=cwd,
123+
compiling = subprocess.Popen(argument_list,
124+
cwd=cwd,
123125
stdout=stdout,
124126
stderr=subprocess.PIPE)
125127
_, stderr = compiling.communicate()
@@ -130,7 +132,7 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
130132
raise CompilerError(
131133
f"{argument_list!r} exit code {compiling.returncode}\n{stderr}",
132134
command=argument_list,
133-
error_output=stderr)
135+
error_output=force_str(stderr))
134136

135137
# User wants to see everything that happened.
136138
if self.verbose:

pipeline/compressors/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,18 @@ def execute_command(self, command, content):
240240
else:
241241
argument_list.extend(flattening_arg)
242242

243-
pipe = subprocess.Popen(argument_list, stdout=subprocess.PIPE,
244-
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
243+
pipe = subprocess.Popen(argument_list,
244+
stdout=subprocess.PIPE,
245+
stdin=subprocess.PIPE,
246+
stderr=subprocess.PIPE)
245247
if content:
246248
content = smart_bytes(content)
247249
stdout, stderr = pipe.communicate(content)
248250
set_std_streams_blocking()
249251
if stderr.strip() and pipe.returncode != 0:
250-
raise CompressorError(stderr)
252+
raise CompressorError(force_str(stderr))
251253
elif self.verbose:
252-
print(stderr)
254+
print(force_str(stderr))
253255
return force_str(stdout)
254256

255257

0 commit comments

Comments
 (0)