Skip to content

Commit 059efae

Browse files
committed
Python 3 fixes: convert source to text
In Python 3 the SCSS source code must be decoded from binary to text before processing. The latest compressor passes a charset argument to our filter, which will be used. If not passed, it will default to utf-8. Note that Python 3 support currently depends on the latest (master) compressor and a pyScss with this fix incorporated: wojas/pyScss@e4cf419 NOTE: Tests related to the sprites fail for me, both on python 2.7 and 3.4. The hash is different from what's expected.
1 parent 5b4c1b1 commit 059efae

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
tmp/
44
.coverage
55
htmlcov/
6+
__pycache__
7+
*.pyc

django_pyscss/compressor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010

1111
class DjangoScssFilter(FilterBase):
12-
compiler = DjangoScss()
12+
compiler = None
13+
14+
def __init__(self, content, attrs=None, filter_type=None, filename=None, charset='utf-8'):
15+
self.compiler = DjangoScss(charset=charset)
1316

14-
def __init__(self, content, attrs=None, filter_type=None, filename=None):
1517
# It looks like there is a bug in django-compressor because it expects
1618
# us to accept attrs.
1719
super(DjangoScssFilter, self).__init__(content, filter_type, filename)

django_pyscss/scss.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class DjangoScss(Scss):
3030
files.
3131
"""
3232
supported_extensions = ['.scss', '.sass', '.css']
33+
charset = 'utf-8'
34+
35+
def __init__(self, **kwargs):
36+
if 'charset' in kwargs:
37+
self.charset = kwargs.pop('charset')
38+
super(DjangoScss, self).__init__(**kwargs)
3339

3440
def get_file_from_storage(self, filename):
3541
try:
@@ -87,7 +93,7 @@ def _find_source_file(self, filename, relative_to=None):
8793
if full_filename:
8894
if full_filename not in self.source_file_index:
8995
with storage.open(full_filename) as f:
90-
source = f.read()
96+
source = f.read().decode(self.charset)
9197

9298
source_file = SourceFile(
9399
full_filename,

0 commit comments

Comments
 (0)