Skip to content

Commit

Permalink
Support Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- authored and jackrosenthal committed Nov 24, 2022
1 parent 7edf1fa commit 65fbfae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
39 changes: 29 additions & 10 deletions kajiki/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import types
from sys import version_info

import linetable

import kajiki
from kajiki import i18n

Expand Down Expand Up @@ -386,14 +388,33 @@ def annotate_lnotab(self, filename, py_to_tpl, py_to_tpl_dct):
if not py_to_tpl:
return
code = self._func.__code__
new_lnotab_numbers = []
for bc_off, py_lno in dis.findlinestarts(code):
tpl_lno = py_to_tpl_dct[py_lno]
new_lnotab_numbers.append((bc_off, tpl_lno))
if not new_lnotab_numbers:
return

new_firstlineno = py_to_tpl_dct.get(code.co_firstlineno, 0)
new_lnotab = lnotab.lnotab_string(new_lnotab_numbers, new_firstlineno)
if version_info >= (3, 11):
ltable = linetable.parse_linetable(code.co_linetable, code.co_firstlineno)
new_lnotab = linetable.generate_linetable(
(
(
length,
py_to_tpl_dct[start_line],
py_to_tpl_dct[end_line],
None,
None,
)
if start_line
else (length, None, None, None, None)
for length, start_line, end_line, *_ in ltable
),
firstlineno=new_firstlineno,
)
else:
new_lnotab_numbers = []
for bc_off, py_lno in dis.findlinestarts(code):
tpl_lno = py_to_tpl_dct[py_lno]
new_lnotab_numbers.append((bc_off, tpl_lno))
if not new_lnotab_numbers:
return
new_lnotab = lnotab.lnotab_string(new_lnotab_numbers, new_firstlineno)
new_code = patch_code_file_lines(code, filename, new_firstlineno, new_lnotab)
self._func.__code__ = new_code
return
Expand All @@ -415,9 +436,7 @@ def patch_code_file_lines(code, filename, firstlineno, lnotab):
code.co_name,
code.co_qualname if version_info >= (3, 11) else "REMOVE",
firstlineno,
lnotab,
code.co_endlinetable if version_info >= (3, 11) else "REMOVE",
code.co_columntable if version_info >= (3, 11) else "REMOVE",
lnotab, # linetable for >=3.11 and lnotab for <3.11
code.co_exceptiontable if version_info >= (3, 11) else "REMOVE",
code.co_freevars,
code.co_cellvars,
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def content_of(*files):
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
Expand All @@ -60,6 +61,7 @@ def content_of(*files):
include_package_data=True,
zip_safe=False,
python_requires=">=3.4",
install_requires=["linetable"],
extras_require={
"testing": ["babel", "pytest"],
"docs": ["sphinx"],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ def test_code_error(self):
except ZeroDivisionError:
exn_info = traceback.format_exception(*sys.exc_info())
last_line = exn_info[-2]
assert "${3/0}" in last_line, last_line
assert "${3/0}" in last_line
else:
assert False

Expand Down

0 comments on commit 65fbfae

Please sign in to comment.