Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit d5c04a2

Browse files
committed
Fix UnicodeEncodeError Manifest.build_one() rises
1 parent 2c73f30 commit d5c04a2

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Version 0.4.2
77
To be released.
88

99
- Fixed build failing on Mac OS X 10.8 or earlier. [:issue:`19`]
10+
- Fixed :exc:`UnicodeEncodeError` that :meth:`Manifest.build_one()
11+
<sassutils.builder.Manifest.build_one>` method rises when the input source
12+
contains any non-ASCII Unicode characters.
1013

1114

1215
Version 0.4.1

sasstests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ def test_build_one(self):
291291
},
292292
json.load(f)
293293
)
294+
m.build_one(d, 'd.sass', source_map=True)
295+
with open(os.path.join(d, 'css', 'd.sass.css')) as f:
296+
self.assertEqual(
297+
D_EXPECTED_CSS +
298+
'\n/*# sourceMappingURL=d.sass.css.map */',
299+
f.read()
300+
)
301+
with open(os.path.join(d, 'css', 'd.sass.css.map')) as f:
302+
self.assertEqual(
303+
{
304+
'version': 3,
305+
'file': 'd.sass',
306+
'sources': ['../test/d.sass'],
307+
'names': [],
308+
'mappings': 'AAKA;EAHE,kBAAkB;EAGpB,KAEE;IACE,MAAM'
309+
},
310+
json.load(f)
311+
)
294312
finally:
295313
shutil.rmtree(d)
296314

sassutils/builder.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ def build_one(self, package_dir, filename, source_map=False):
196196
if not os.path.exists(css_folder):
197197
os.makedirs(css_folder)
198198
with open(css_path, 'w') as f:
199-
f.write(css)
199+
try:
200+
f.write(css)
201+
except UnicodeEncodeError:
202+
f.write(css.encode('utf-8'))
200203
if source_map:
201-
with open(source_map_path, 'w') as f:
202-
f.write(source_map)
204+
with open(source_map_path, 'wb') as f:
205+
# Source maps are JSON, and JSON has to be UTF-8 encoded
206+
f.write(source_map.encode('utf-8'))
203207
return css_filename

0 commit comments

Comments
 (0)