From 95985e3d376579bb64146079bf9277b1071f4c12 Mon Sep 17 00:00:00 2001 From: James Addison Date: Mon, 22 May 2023 19:48:44 +0100 Subject: [PATCH] Compatibility: pycode parser: adjust dedent-token handling for py3.12+ Refs: https://github.com/sphinx-doc/sphinx/issues/11436 --- sphinx/pycode/parser.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index aecb095b281..34e63e66567 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -6,6 +6,7 @@ import inspect import itertools import re +import sys import tokenize from inspect import Signature from token import DEDENT, INDENT, NAME, NEWLINE, NUMBER, OP, STRING @@ -525,7 +526,16 @@ def finalize_block(self) -> None: definition = self.indents.pop() if definition[0] != 'other': typ, funcname, start_pos = definition - end_pos = self.current.end[0] - 1 + end_pos = self.current.end[0] + + # The dedent end position on py3.12 is capped to the file linecount + # refs: https://github.com/sphinx-doc/sphinx/issues/11436 + if sys.version_info[:2] >= (3, 12) and end_pos == len(self.buffers): + pass + else: + end_pos -= 1 + + # Omit empty dedenting lines from the definition's location while emptyline_re.match(self.get_line(end_pos)): end_pos -= 1