Skip to content

Converting a render error to a string fails for a non-UTF-8 template file #450

Closed

Description

@mikeckennedy
from pathlib import Path
from chameleon import PageTemplateFile

Path('latin.pt').write_bytes('<?xml version="1.0" encoding="latin-1"?>\n<p>caf\xe9 ${missing}</p>'.encode('latin-1'))
try:
    PageTemplateFile(str(Path('latin.pt').resolve()))()
except NameError as e:
    str(e)  # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 ...

TemplateError.__str__ and ExceptionFormatter.__call__ in exc.py re-read the template file with a bare open() to show the source lines, which uses the locale encoding. Only OSError is caught, and the decode error happens later, while iterating the file. So logging or a framework's error page loses the original message.

PR to follow: read the lines as UTF-8 with errors='replace'.

Reproduced on master (90b9002) with Python 3.13 and 3.15.

Activity

  1. added a commit that references this issue on Oct 30, 2026
    64c3459
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions