Skip to content

Commit 21c11f3

Browse files
committed
Provide a module spec for string-based template modules
Fixed issue where formatting a traceback for an exception raised inside a template compiled from a string would emit "DeprecationWarning: Module globals is missing a __spec__.loader" on Python 3.15. A module built from types.ModuleType alone has __spec__ and __loader__ present in its namespace but set to None, and Python 3.15's linecache._bless_my_loader() warns when it finds a __spec__ without a loader. Its early return for synthetic modules only applies when __spec__ is absent entirely, so template modules fell through to the warning. As linecache is consulted for every frame while a traceback is built, the warning is raised from inside traceback formatting, which destroys the error report for the common configuration of turning warnings into errors. Template modules are now given a spec whose loader can return the generated module source. Beyond silencing the warning, this makes the generated source lines available to linecache, so tracebacks produced by the standard library now display the generated line for template frames where previously no source could be shown. Fixes: sqlalchemy#437 Change-Id: I838eadefeaf073ad60930ad7a025dd11393e2b6d
1 parent 3c1b6a3 commit 21c11f3

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎doc/build/unreleased/437.rst‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.. change::
2+
:tags: bug, exceptions
3+
:tickets: 437
4+
5+
Fixed issue where formatting a traceback for an exception raised inside a
6+
template compiled from a string would emit ``DeprecationWarning: Module
7+
globals is missing a __spec__.loader`` on Python 3.15. Modules for such
8+
templates were created without a module spec, which Python's
9+
:mod:`linecache` module consults for every frame while a traceback is
10+
being built; the warning was raised from within traceback formatting
11+
itself, disrupting the error report for applications that configure
12+
warnings as errors. These modules are now given a spec with a loader that
13+
provides the generated module source, which additionally allows the
14+
generated source lines to be displayed in tracebacks produced by the
15+
standard library where previously no source was available.

‎mako/template.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""Provides the Template class, a facade for parsing, generating and executing
88
template strings, as well as template runtime operations."""
99

10+
from importlib import abc
11+
from importlib import machinery
1012
import json
1113
import os
1214
import re
@@ -662,6 +664,30 @@ def _compile(template, text, filename, generate_magic_comment):
662664
return source, lexer
663665

664666

667+
class _ModuleSourceLoader(abc.Loader):
668+
"""Provide the generated source of an in-memory template module.
669+
670+
A module created directly from :class:`types.ModuleType` has ``__spec__``
671+
and ``__loader__`` present in its namespace but set to ``None``. The
672+
:mod:`linecache` module consults these while a traceback is being built,
673+
and as of Python 3.15 emits a :class:`DeprecationWarning` when it finds a
674+
``__spec__`` that has no loader; a module lacking a real loader also has
675+
no source lines available to show for its frames. Supplying a loader
676+
that can produce the generated module source addresses both.
677+
678+
"""
679+
680+
def __init__(self, name, source):
681+
self.name = name
682+
self._source = source
683+
684+
def get_source(self, name):
685+
return self._source
686+
687+
def is_package(self, name):
688+
return False
689+
690+
665691
def _compile_text(template, text, filename):
666692
identifier = template.module_id
667693
source, lexer = _compile(
@@ -670,6 +696,9 @@ def _compile_text(template, text, filename):
670696

671697
cid = identifier
672698
module = types.ModuleType(cid)
699+
loader = _ModuleSourceLoader(cid, source)
700+
module.__loader__ = loader
701+
module.__spec__ = machinery.ModuleSpec(cid, loader, origin=cid)
673702
code = compile(source, cid, "exec")
674703

675704
# this exec() works for 2.4->3.3.

‎test/test_exceptions.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import sys
2+
import traceback
3+
import warnings
24

35
from mako import exceptions
46
from mako.lookup import TemplateLookup
57
from mako.template import Template
8+
from mako.testing.assertions import eq_
69
from mako.testing.exclusions import requires_no_pygments_exceptions
710
from mako.testing.exclusions import requires_pygments_14
811
from mako.testing.fixtures import TemplateTest
@@ -371,3 +374,58 @@ def broken():
371374
""" in text_error
372375
else:
373376
assert False
377+
378+
379+
class TemplateModuleSpecTest(TemplateTest):
380+
"""test that in-memory template modules present a usable ``__spec__``.
381+
382+
A module made from :class:`types.ModuleType` alone carries a ``__spec__``
383+
of ``None``, which Python 3.15's :mod:`linecache` reports as a
384+
:class:`DeprecationWarning` while a traceback is being formatted.
385+
386+
"""
387+
388+
def test_module_spec_has_loader(self):
389+
template = Template("hello world")
390+
391+
spec = template.module.__spec__
392+
assert spec is not None
393+
assert spec.loader is not None
394+
eq_(spec.loader, template.module.__loader__)
395+
396+
def test_loader_returns_module_source(self):
397+
template = Template("hello world")
398+
399+
loader = template.module.__spec__.loader
400+
eq_(loader.get_source(template.module.__name__), template.code)
401+
402+
def test_no_warning_formatting_traceback(self):
403+
"""test #437"""
404+
405+
template = Template("${1 / 0}")
406+
407+
try:
408+
template.render()
409+
except ZeroDivisionError:
410+
with warnings.catch_warnings():
411+
warnings.simplefilter("error")
412+
formatted = traceback.format_exc()
413+
else:
414+
assert False
415+
416+
assert "ZeroDivisionError" in formatted
417+
418+
def test_traceback_includes_module_source(self):
419+
"""test that generated source lines are available to linecache"""
420+
421+
template = Template("${1 / 0}")
422+
423+
try:
424+
template.render()
425+
except ZeroDivisionError:
426+
formatted = traceback.format_exc()
427+
else:
428+
assert False
429+
430+
assert template.module.__name__ in formatted
431+
assert "__M_writer" in formatted

0 commit comments

Comments
 (0)