Skip to content

Commit 0c43a0f

Browse files
authored
Merge pull request #291 from theatlantic/fix-importer-argspec
Allow partials and other non-function callables as importer callbacks
2 parents e5dc259 + 4866296 commit 0c43a0f

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

sass.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from __future__ import absolute_import
1414

1515
import collections
16-
import functools
1716
import inspect
1817
import io
1918
import os
@@ -195,19 +194,21 @@ def _to_bytes(obj):
195194

196195

197196
def _importer_callback_wrapper(func):
198-
if PY2:
199-
argspec = inspect.getargspec(func)
200-
else:
201-
argspec = inspect.getfullargspec(func)
202-
203-
num_args = len(argspec.args)
204-
205-
@functools.wraps(func)
206197
def inner(path, prev):
207-
if num_args == 2:
208-
ret = func(path.decode('UTF-8'), prev.decode('UTF-8'))
198+
path, prev = path.decode('UTF-8'), prev.decode('UTF-8')
199+
num_args = getattr(inner, '_num_args', None)
200+
if num_args is None:
201+
try:
202+
ret = func(path, prev)
203+
except TypeError:
204+
inner._num_args = 1
205+
ret = func(path)
206+
else:
207+
inner._num_args = 2
208+
elif num_args == 2:
209+
ret = func(path, prev)
209210
else:
210-
ret = func(path.decode('UTF-8'))
211+
ret = func(path)
211212
return _normalize_importer_return_value(ret)
212213
return inner
213214

sasstests.py

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import base64
55
import contextlib
6+
import functools
67
import glob
78
import json
89
import io
@@ -358,6 +359,23 @@ def importer(path, prev):
358359
)
359360
assert ret == 'a{color:red}\n'
360361

362+
def test_importer_prev_path_partial(self):
363+
def importer(a_css, path, prev):
364+
assert path in ('a', 'b')
365+
if path == 'a':
366+
assert prev == 'stdin'
367+
return ((path, '@import "b";'),)
368+
elif path == 'b':
369+
assert prev == 'a'
370+
return ((path, a_css),)
371+
372+
ret = sass.compile(
373+
string='@import "a";',
374+
importers=((0, functools.partial(importer, 'a { color: red; }')),),
375+
output_style='compressed',
376+
)
377+
assert ret == 'a{color:red}\n'
378+
361379
def test_importer_does_not_handle_returns_None(self):
362380
def importer_one(path):
363381
if path == 'one':

0 commit comments

Comments
 (0)