Skip to content
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
2 changes: 1 addition & 1 deletion src/chameleon/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def __call__(self) -> str:
for error in self._errors:
expression, line, column, filename, exc = error

if isinstance(exc, UnicodeDecodeError):
if isinstance(exc, UnicodeDecodeError) and exc.object is not None:
string = safe_native(exc.object)
s, marker = compute_source_marker(
string, exc.start, string[exc.start:exc.end], LENGTH
Expand Down
20 changes: 20 additions & 0 deletions src/chameleon/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,26 @@ def __init__(self, class_name, bases, namespace):
exc = create_formatted_exception(Difficult(), Difficult, str)
assert exc.args == ()

def test_create_formatted_exception_unicode_error(self):
from chameleon.utils import create_formatted_exception

try:
"æøå".encode("utf-8").decode("ascii")
except UnicodeDecodeError as exc:
inner = exc

from chameleon.exc import ExceptionFormatter
exc = create_formatted_exception(inner, type(inner), str)

error = "expression", 1, 1, "", exc
formatter = ExceptionFormatter([error], {}, {}, repr)
exc = create_formatted_exception(
NameError('abc'), NameError, formatter
)

formatted = str(exc)
assert formatted.startswith(type(inner).__name__)

def test_error_handler_makes_safe_copy(self):
calls = []

Expand Down
2 changes: 1 addition & 1 deletion src/chameleon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def create_formatted_exception(
inst = cls.__new__(new)

BaseException.__init__(inst, *exc.args)
inst.__dict__ = exc.__dict__ # type: ignore[attr-defined]
inst.__dict__ = exc.__dict__ # type: ignore[assignment]

return inst
except ValueError:
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ deps =
setuptools
commands =
pytest --doctest-modules -W error:"the load_module":DeprecationWarning -W error:"zipimport.zipimporter.load_module":DeprecationWarning -W error:"ast.":DeprecationWarning {posargs}
extras =
test
setenv =
LOGNAME=dummy

Expand Down
Loading