Skip to content

Commit

Permalink
casting.py: Filter WSL1 + np.longdouble warning
Browse files Browse the repository at this point in the history
This commit filters the following warning:

> UserWarning: Signature b'\x00\xd0\xcc\xcc\xcc\xcc\xcc\xcc\xfb\xbf\x00\x00\x00\x00\x00\x00' for
> <class 'numpy.longdouble'> does not match any known type: falling back to type probe function.
> This warnings [sic] indicates broken support for the dtype!
>  machar = _get_machar(dtype)

 To ensure that this warning is only filtered on WSL1, we try to detect WSL
 by checking for a WSL-specific string from the uname, which appears to be
 endorsed by WSL devs.
 (microsoft/WSL#4555 (comment))

 I also tried checking the `WSL_INTEROP` and `WSL_DISTRO_NAME` environment
 variables as suggested in the above linked issues, but I liked that `platform`
 was already imported inside `casting.py`.

 There is perhaps a more thorough approach where we collect all raised warnings,
 test the collected warnings, etc. but I didn't want to overcomplicate things.
  • Loading branch information
joshuacwnewton committed Mar 23, 2024
1 parent 0e925ab commit 0fec353
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions nibabel/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

import warnings
from platform import machine, processor
from platform import machine, processor, uname

import numpy as np

Expand Down Expand Up @@ -274,7 +274,16 @@ def type_info(np_type):
nexp=None,
width=width,
)
info = np.finfo(dt)
# Mitigate warning from WSL1 when checking `np.longdouble` (#1309)
with warnings.catch_warnings():
# src: https://github.com/microsoft/WSL/issues/4555#issuecomment-700213318
# src: https://github.com/scivision/detect-windows-subsystem-for-linux/blob/main/is_wsl.py
is_wsl1 = uname().release.endswith("-Microsoft")
if dt == np.longdouble and is_wsl1:
warnings.filterwarnings(action='ignore', category=UserWarning,
message='Signature.*for numpy.longdouble.*does not match')
info = np.finfo(dt)

# Trust the standard IEEE types
nmant, nexp = info.nmant, info.nexp
ret = dict(
Expand Down

0 comments on commit 0fec353

Please sign in to comment.