Blob Blame History Raw
From 27c3466dfe8e7f74073e6fdfbb71c87ce396e2e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Germ=C3=A1n=20M=2E=20Bravo?= <german.mb@gmail.com>
Date: Sat, 15 Feb 2014 10:07:25 -0600
Subject: [PATCH 04/14] Encoding/decoding of files

When using the pipe, `ExternalTool.subprocess` encodes/decodes unicode properly, but when using files it doesn't. It is not encoding the unicode `data` when it writes to the input file, and also is writing inconsistent types to `out`. This fixes this issue.
(cherry picked from commit cd072974dad06f4000987226485b285d6647f31c)

diff --git a/src/webassets/filter/__init__.py b/src/webassets/filter/__init__.py
index ecc201b..a8743c8 100644
--- a/src/webassets/filter/__init__.py
+++ b/src/webassets/filter/__init__.py
@@ -475,12 +475,16 @@ class ExternalTool(six.with_metaclass(ExternalToolMetaclass, Filter)):
                        item.format(input=input_file, output=output_file), argv))
 
         try:
+            data = (data.read() if hasattr(data, 'read') else data)
+            if data is not None:
+                data = data.encode('utf-8')
+
             if input_file.created:
                 if not data:
                     raise ValueError(
                         '{input} placeholder given, but no data passed')
                 with os.fdopen(input_file.fd, 'wb') as f:
-                    f.write(data.read() if hasattr(data, 'read') else data)
+                    f.write(data)
                     # No longer pass to stdin
                     data = None
 
@@ -491,9 +495,6 @@ class ExternalTool(six.with_metaclass(ExternalToolMetaclass, Filter)):
                 stdout=subprocess.PIPE,
                 stdin=subprocess.PIPE,
                 stderr=subprocess.PIPE)
-            data = (data.read() if hasattr(data, 'read') else data)
-            if data is not None:
-                data = data.encode('utf-8')
             stdout, stderr = proc.communicate(data)
             if proc.returncode:
                 raise FilterError(
@@ -504,7 +505,7 @@ class ExternalTool(six.with_metaclass(ExternalToolMetaclass, Filter)):
             else:
                 if output_file.created:
                     with open(output_file.filename, 'rb') as f:
-                        out.write(f.read())
+                        out.write(f.read().decode('utf-8'))
                 else:
                     out.write(stdout.decode('utf-8'))
         finally:
-- 
1.9.3