From 87367f3170ea8f595c2ecf9ef593bf9e1d2e2c00 Mon Sep 17 00:00:00 2001 From: Peter Van Dyken Date: Fri, 23 Feb 2024 13:10:48 -0500 Subject: [PATCH] Get warning line same as in python impl --- snakebids/_warningformat.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/snakebids/_warningformat.py b/snakebids/_warningformat.py index fa38b7ae..09cb65aa 100644 --- a/snakebids/_warningformat.py +++ b/snakebids/_warningformat.py @@ -2,7 +2,6 @@ import textwrap import warnings -from pathlib import Path from colorama import Fore, Style @@ -23,9 +22,15 @@ def formatwarning( ): """Format warning messages.""" if line is None: - with Path(filename).open() as f: - for _ in range(lineno): - line = f.readline().strip() + try: + import linecache + + line = linecache.getline(filename, lineno) + except Exception: # noqa: BLE001 + # When a warning is logged during Python shutdown, linecache + # and the import machinery don't work anymore + line = None + linecache = None return WARN_TEMPLATE.format( message=textwrap.indent(