| 1 |
From: Josenilson Ferreira da Silva <[email protected]> |
| 2 |
Date: Mon, 27 Apr 2025 22:00:00 -0300 |
| 3 |
Subject: Workaround for pytest 9.0.x IndexError with Python 3.14 |
| 4 |
Forwarded: not-needed |
| 5 |
|
| 6 |
Monkey-patch get_statement_startend2 to guard against IndexError when |
| 7 |
insert_index is 0, triggered by Python 3.14 AST changes. Fixed in pytest 9.1+. |
| 8 |
--- |
| 9 |
|
| 10 |
Index: python-friendly-traceback/tests/conftest.py |
| 11 |
=================================================================== |
| 12 |
--- /dev/null |
| 13 |
+++ python-friendly-traceback/tests/conftest.py |
| 14 |
@@ -0,0 +1,31 @@ |
| 15 |
+# Workaround for pytest 9.0.x bug with Python 3.14 |
| 16 |
+# Fixed in pytest 9.1+ |
| 17 |
+import ast |
| 18 |
+import bisect |
| 19 |
+import _pytest._code.source as _source |
| 20 |
+ |
| 21 |
+ |
| 22 |
+def _patched_get_statement_startend2(lineno, node): |
| 23 |
+ values = [] |
| 24 |
+ |
| 25 |
+ for x in ast.walk(node): |
| 26 |
+ if isinstance(x, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)): |
| 27 |
+ for d in x.decorator_list: |
| 28 |
+ values.append(d.lineno - 1) |
| 29 |
+ if hasattr(x, "lineno"): |
| 30 |
+ values.append(x.lineno - 1) |
| 31 |
+ for name in ("finalbody", "orelse"): |
| 32 |
+ val = getattr(x, name, None) |
| 33 |
+ if isinstance(val, list) and val and hasattr(val[0], "lineno"): |
| 34 |
+ values.append(val[0].lineno - 1 - 1) |
| 35 |
+ |
| 36 |
+ values.sort() |
| 37 |
+ insert_index = bisect.bisect_right(values, lineno) |
| 38 |
+ if insert_index == 0: |
| 39 |
+ return lineno, None |
| 40 |
+ start = values[insert_index - 1] |
| 41 |
+ end = None if insert_index >= len(values) else values[insert_index] |
| 42 |
+ return start, end |
| 43 |
+ |
| 44 |
+ |
| 45 |
+_source.get_statement_startend2 = _patched_get_statement_startend2 |