Skip to content

Commit f23ca14

Browse files
casting.py: Filter WSL1 + np.longdouble warning
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.
1 parent 0e925ab commit f23ca14

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

nibabel/casting.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import annotations
77

88
import warnings
9-
from platform import machine, processor
9+
from platform import machine, processor, uname
1010

1111
import numpy as np
1212

@@ -274,7 +274,15 @@ def type_info(np_type):
274274
nexp=None,
275275
width=width,
276276
)
277-
info = np.finfo(dt)
277+
# Mitigate warning from WSL1 when checking `np.longdouble` (#1309)
278+
# src for '-Microsoft': https://github.com/microsoft/WSL/issues/4555#issuecomment-536862561
279+
with warnings.catch_warnings():
280+
if uname().release.endswith('-Microsoft'):
281+
warnings.filterwarnings(
282+
action='ignore', category=UserWarning, message='Signature.*numpy.longdouble'
283+
)
284+
info = np.finfo(dt)
285+
278286
# Trust the standard IEEE types
279287
nmant, nexp = info.nmant, info.nexp
280288
ret = dict(

0 commit comments

Comments
 (0)