Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve the formatting of CompileErrors in py3 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import sys
import warnings

from six import string_types, text_type
from six import string_types, text_type, PY2, PY3

from _sass import OUTPUT_STYLES, compile_filename, compile_string

Expand All @@ -48,10 +48,20 @@
MODES = set(['string', 'filename', 'dirname'])


def to_native_s(s):
if isinstance(s, bytes) and PY3: # pragma: no cover (py3)
s = s.decode('UTF-8')
elif isinstance(s, text_type) and PY2: # pragma: no cover (py2)
s = s.encode('UTF-8')
return s


class CompileError(ValueError):
"""The exception type that is raised by :func:`compile()`.
It is a subtype of :exc:`exceptions.ValueError`.
"""
def __init__(self, msg):
super(CompileError, self).__init__(to_native_s(msg))


def mkdirp(path):
Expand Down
19 changes: 17 additions & 2 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import subprocess
import sys
import tempfile
import traceback
import unittest
import warnings

Expand Down Expand Up @@ -914,7 +915,7 @@ def test_error(self):
assert False, 'Expected to raise'
except sass.CompileError as e:
msg, = e.args
assert msg.decode('UTF-8').startswith(
assert msg.startswith(
'Error: Invalid CSS after '
), msg
return
Expand Down Expand Up @@ -1182,7 +1183,7 @@ def assert_raises_compile_error(expected):
with pytest.raises(sass.CompileError) as excinfo:
yield
msg, = excinfo.value.args
assert msg.decode('UTF-8') == expected, (msg, expected)
assert msg == expected, (msg, expected)


class RegexMatcher(object):
Expand Down Expand Up @@ -1417,3 +1418,17 @@ def test_map_with_map_key(self):
),
'a{content:baz}\n',
)


def test_stack_trace_formatting():
try:
sass.compile(string=u'a{☃')
assert False, 'expected to raise CompileError'
except sass.CompileError:
tb = traceback.format_exc()
assert tb.endswith(
'CompileError: Error: Invalid CSS after "a{☃": expected "{", was ""\n'
' on line 1 of stdin\n'
'>> a{☃\n'
' --^\n\n'
)