Skip to content

Commit

Permalink
fail-on-template-vars: modernize stack inspection code (#1129)
Browse files Browse the repository at this point in the history
inspect.stack() returns a list of namedtuple (or retrocompatible
objects) since Python 3.5+: let's use the named attribute.

cf https://docs.python.org/3/library/inspect.html#inspect.stack

And once we have access to a FrameInfo object/namedtuple, access to its
frame object and its f_locals member should not need to iterate on all
its members:
https://docs.python.org/3/reference/datamodel.html#frame-objects
  • Loading branch information
xavfernandez authored Sep 2, 2024
1 parent c746a46 commit 19605d7
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,11 @@ def _get_origin() -> str | None:

# Try to use topmost `self.origin` first (Django 1.9+, and with
# TEMPLATE_DEBUG)..
for f in stack[2:]:
func = f[3]
if func == "render":
frame = f[0]
for frame_info in stack[2:]:
if frame_info.function == "render":
origin: str | None
try:
origin = frame.f_locals["self"].origin
origin = frame_info.frame.f_locals["self"].origin
except (AttributeError, KeyError):
origin = None
if origin is not None:
Expand All @@ -686,16 +684,10 @@ def _get_origin() -> str | None:

# finding the ``render`` needle in the stack
frameinfo = reduce(
lambda x, y: y[3] == "render" and "base.py" in y[1] and y or x, stack
lambda x, y: y if y.function == "render" and "base.py" in y.filename else x, stack
)
# assert 0, stack
frame = frameinfo[0]
# finding only the frame locals in all frame members
f_locals = reduce(
lambda x, y: y[0] == "f_locals" and y or x, inspect.getmembers(frame)
)[1]
# ``django.template.base.Template``
template = f_locals["self"]
template = frameinfo.frame.f_locals["self"]
if isinstance(template, Template):
name: str = template.name
return name
Expand Down

0 comments on commit 19605d7

Please sign in to comment.