Skip to content

Commit

Permalink
Get warning line same as in python impl
Browse files Browse the repository at this point in the history
  • Loading branch information
pvandyken committed Feb 23, 2024
1 parent 96b13c0 commit 87367f3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions snakebids/_warningformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import textwrap
import warnings
from pathlib import Path

from colorama import Fore, Style

Expand All @@ -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

Check warning on line 29 in snakebids/_warningformat.py

View check run for this annotation

Codecov / codecov/patch

snakebids/_warningformat.py#L29

Added line #L29 was not covered by tests
# When a warning is logged during Python shutdown, linecache
# and the import machinery don't work anymore
line = None
linecache = None

Check warning on line 33 in snakebids/_warningformat.py

View check run for this annotation

Codecov / codecov/patch

snakebids/_warningformat.py#L32-L33

Added lines #L32 - L33 were not covered by tests

return WARN_TEMPLATE.format(
message=textwrap.indent(
Expand Down

0 comments on commit 87367f3

Please sign in to comment.