Merge pull request #1536 from pallets/traceback-codetype
Before adoption: this commit came in with the history the project adopted, so no landing records it.
simplify CodeType rewriting
authored by David Lord · committed by GitHub · parents a83aded, 99435a4 · Browse files · 2 files·+39 added−50 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−50 removed
- other:+39 added−50 removed
Provenance
- Commit
a4e2532- Tree
441afe3- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a83aded- Parent 2
99435a4
2 files changed+39 added−50 removed
modified:src/jinja2/debug.py+30 added-50 removed
100 unmodified lines101 "__file__": filename,102 "__jinja_exception__": exc_value,103 }104 # Raise an exception at the correct line number.105- code = compile("\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec")105+ code: CodeType = compile(106+ "\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec"107+ )108 109 # Build a new code object that points to the template file and110 # replaces the location with a block name.109- try:110- location = "template"111+ location = "template"112 112- if tb is not None:113- function = tb.tb_frame.f_code.co_name113+ if tb is not None:114+ function = tb.tb_frame.f_code.co_name115 115- if function == "root":116- location = "top-level template code"117- elif function.startswith("block_"):118- location = f"block {function[6:]!r}"116+ if function == "root":117+ location = "top-level template code"118+ elif function.startswith("block_"):119+ location = f"block {function[6:]!r}"120 120- # Collect arguments for the new code object. CodeType only121- # accepts positional arguments, and arguments were inserted in122- # new Python versions.123- code_args = []121+ if sys.version_info >= (3, 8):122+ code = code.replace(co_name=location)123+ else:124+ code = CodeType(125+ code.co_argcount,126+ code.co_kwonlyargcount,127+ code.co_nlocals,128+ code.co_stacksize,129+ code.co_flags,130+ code.co_code,131+ code.co_consts,132+ code.co_names,133+ code.co_varnames,134+ code.co_filename,135+ location,136+ code.co_firstlineno,137+ code.co_lnotab,138+ code.co_freevars,139+ code.co_cellvars,140+ )141 125- for attr in (126- "argcount",127- "posonlyargcount", # Python 3.8128- "kwonlyargcount",129- "nlocals",130- "stacksize",131- "flags",132- "code", # codestring133- "consts", # constants134- "names",135- "varnames",136- ("filename", filename),137- ("name", location),138- "firstlineno",139- "lnotab",140- "freevars",141- "cellvars",142- "linetable", # Python 3.10143- ):144- if isinstance(attr, tuple):145- # Replace with given value.146- code_args.append(attr[1])147- continue148-149- try:150- # Copy original value if it exists.151- code_args.append(getattr(code, "co_" + t.cast(str, attr)))152- except AttributeError:153- # Some arguments were added later.154- continue155-156- code = CodeType(*code_args)157- except Exception:158- # Some environments such as Google App Engine don't support159- # modifying code objects.160- pass161-142 # Execute the new code, which is guaranteed to raise, and return143 # the new traceback without this frame.144 try:145 exec(code, globals, locals)114 unmodified linesmodified:CHANGES.rst+9 added
1 .. currentmodule:: jinja22 3+Version 3.0.34+-------------5+6+Unreleased7+8+- Fix traceback rewriting internals for Python 3.10 and 3.11.9+ :issue:`1535`10+11+12 Version 3.0.213 -------------14 15 Released 2021-10-04892 unmodified lines