From 0fec353a08336671ce86116284499065c4fd8bfe Mon Sep 17 00:00:00 2001 From: Joshua Newton Date: Fri, 22 Mar 2024 17:48:06 -0400 Subject: [PATCH] `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 > 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. (https://github.com/microsoft/WSL/issues/4555#issuecomment-700315063) 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. --- nibabel/casting.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/nibabel/casting.py b/nibabel/casting.py index 09015135f..e8fe6877e 100644 --- a/nibabel/casting.py +++ b/nibabel/casting.py @@ -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 @@ -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(