Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify CodeType rewriting #1536

Merged
merged 1 commit into from Nov 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES.rst
@@ -1,5 +1,14 @@
.. currentmodule:: jinja2

Version 3.0.3
-------------

Unreleased

- Fix traceback rewriting internals for Python 3.10 and 3.11.
:issue:`1535`


Version 3.0.2
-------------

Expand Down
86 changes: 33 additions & 53 deletions src/jinja2/debug.py
Expand Up @@ -102,62 +102,42 @@ def fake_traceback( # type: ignore
"__jinja_exception__": exc_value,
}
# Raise an exception at the correct line number.
code = compile("\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec")
code: CodeType = compile(
"\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec"
)

# Build a new code object that points to the template file and
# replaces the location with a block name.
try:
location = "template"

if tb is not None:
function = tb.tb_frame.f_code.co_name

if function == "root":
location = "top-level template code"
elif function.startswith("block_"):
location = f"block {function[6:]!r}"

# Collect arguments for the new code object. CodeType only
# accepts positional arguments, and arguments were inserted in
# new Python versions.
code_args = []

for attr in (
"argcount",
"posonlyargcount", # Python 3.8
"kwonlyargcount",
"nlocals",
"stacksize",
"flags",
"code", # codestring
"consts", # constants
"names",
"varnames",
("filename", filename),
("name", location),
"firstlineno",
"lnotab",
"freevars",
"cellvars",
"linetable", # Python 3.10
):
if isinstance(attr, tuple):
# Replace with given value.
code_args.append(attr[1])
continue

try:
# Copy original value if it exists.
code_args.append(getattr(code, "co_" + t.cast(str, attr)))
except AttributeError:
# Some arguments were added later.
continue

code = CodeType(*code_args)
except Exception:
# Some environments such as Google App Engine don't support
# modifying code objects.
pass
location = "template"

if tb is not None:
function = tb.tb_frame.f_code.co_name

if function == "root":
location = "top-level template code"
elif function.startswith("block_"):
location = f"block {function[6:]!r}"

if sys.version_info >= (3, 8):
code = code.replace(co_name=location)
else:
code = CodeType(
code.co_argcount,
code.co_kwonlyargcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
code.co_varnames,
code.co_filename,
location,
code.co_firstlineno,
code.co_lnotab,
code.co_freevars,
code.co_cellvars,
)

# Execute the new code, which is guaranteed to raise, and return
# the new traceback without this frame.
Expand Down