diff --git a/src/chameleon/exc.py b/src/chameleon/exc.py index 3767473..0598897 100644 --- a/src/chameleon/exc.py +++ b/src/chameleon/exc.py @@ -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 diff --git a/src/chameleon/tests/test_templates.py b/src/chameleon/tests/test_templates.py index 56b5099..3d27633 100644 --- a/src/chameleon/tests/test_templates.py +++ b/src/chameleon/tests/test_templates.py @@ -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 = [] diff --git a/src/chameleon/utils.py b/src/chameleon/utils.py index efda2d8..fa735b7 100644 --- a/src/chameleon/utils.py +++ b/src/chameleon/utils.py @@ -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: diff --git a/tox.ini b/tox.ini index 6518cb2..0e8d9ab 100644 --- a/tox.ini +++ b/tox.ini @@ -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