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

Commit 797a571

Browse files
committed
Merge pull request #111 from asottile/disallow_arbitrary_kwargs
Disallow arbitrary kwargs in compile()
2 parents 86e9fc2 + 0aa67c6 commit 797a571

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

sass.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ def compile_dirname(
176176
return True, None
177177

178178

179+
def _check_no_remaining_kwargs(func, kwargs):
180+
if kwargs:
181+
raise TypeError(
182+
'{0}() got unexpected keyword argument(s) {1}'.format(
183+
func.__name__,
184+
', '.join("'{0}'".format(arg) for arg in sorted(kwargs)),
185+
)
186+
)
187+
188+
179189
def compile(**kwargs):
180190
"""There are three modes of parameters :func:`compile()` can take:
181191
``string``, ``filename``, and ``dirname``.
@@ -456,9 +466,10 @@ def func_name(a, b):
456466
if not isinstance(indented, bool):
457467
raise TypeError('indented must be bool, not ' +
458468
repr(source_comments))
469+
_check_no_remaining_kwargs(compile, kwargs)
459470
s, v = compile_string(
460471
string, output_style, source_comments, include_paths, precision,
461-
custom_functions, indented
472+
custom_functions, indented,
462473
)
463474
if s:
464475
return v.decode('utf-8')
@@ -470,6 +481,7 @@ def func_name(a, b):
470481
raise IOError('{0!r} seems not a file'.format(filename))
471482
elif isinstance(filename, text_type):
472483
filename = filename.encode(fs_encoding)
484+
_check_no_remaining_kwargs(compile, kwargs)
473485
s, v, source_map = compile_filename(
474486
filename, output_style, source_comments, include_paths, precision,
475487
source_map_filename, custom_functions,
@@ -515,6 +527,7 @@ def func_name(a, b):
515527
except ValueError:
516528
raise ValueError('dirname must be a pair of (source_dir, '
517529
'output_dir)')
530+
_check_no_remaining_kwargs(compile, kwargs)
518531
s, v = compile_dirname(
519532
search_path, output_path, output_style, source_comments,
520533
include_paths, precision, custom_functions,

sasstests.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ def test_compile_invalid_source_comments(self):
224224
string='a { color: blue; }',
225225
source_comments='invalid')
226226

227+
def test_compile_disallows_arbitrary_arguments(self):
228+
for args in (
229+
{'string': 'a{b:c}'},
230+
{'filename': 'test/a.scss'},
231+
{'dirname': ('test', '/dev/null')},
232+
):
233+
with assert_raises(TypeError) as excinfo:
234+
sass.compile(herp='derp', harp='darp', **args)
235+
msg, = excinfo.value.args
236+
assert msg == (
237+
"compile() got unexpected keyword argument(s) 'harp', 'herp'"
238+
)
239+
227240
def test_compile_string(self):
228241
actual = sass.compile(string='a { b { color: blue; } }')
229242
assert actual == 'a b {\n color: blue; }\n'
@@ -1023,13 +1036,24 @@ def compile_with_func(s):
10231036

10241037

10251038
@contextlib.contextmanager
1026-
def assert_raises_compile_error(expected):
1039+
def assert_raises(exctype):
1040+
# I want pytest.raises, this'll have to do for now
1041+
class C:
1042+
pass
1043+
10271044
try:
1028-
yield
1045+
yield C
10291046
assert False, 'Expected to raise!'
1030-
except sass.CompileError as e:
1031-
msg, = e.args
1032-
assert msg.decode('UTF-8') == expected, (msg, expected)
1047+
except exctype as e:
1048+
C.value = e
1049+
1050+
1051+
@contextlib.contextmanager
1052+
def assert_raises_compile_error(expected):
1053+
with assert_raises(sass.CompileError) as excinfo:
1054+
yield
1055+
msg, = excinfo.value.args
1056+
assert msg.decode('UTF-8') == expected, (msg, expected)
10331057

10341058

10351059
class RegexMatcher(object):

0 commit comments

Comments
 (0)