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 preferred reusing
 the `platform` module that 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 f23ca14
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 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,15 @@ def type_info(np_type):
nexp=None,
width=width,
)
info = np.finfo(dt)
# Mitigate warning from WSL1 when checking `np.longdouble` (#1309)
# src for '-Microsoft': https://github.com/microsoft/WSL/issues/4555#issuecomment-536862561
with warnings.catch_warnings():
if uname().release.endswith('-Microsoft'):
warnings.filterwarnings(
action='ignore', category=UserWarning, message='Signature.*numpy.longdouble'
)
info = np.finfo(dt)

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

0 comments on commit f23ca14

Please sign in to comment.