Coverage for cuda/bindings/utils/_envvar.py: 100.00%

19 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-03 02:41 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: Apache-2.0 

3 

4import os 

5 

6_TRUE_VALUES = frozenset({"1", "true", "yes", "on"}) 

7_FALSE_VALUES = frozenset({"0", "false", "no", "off"}) 

8 

9 

10def envvar_bool(name: str, default: bool = False) -> bool: 

11 """Read a bool-like environment variable. 

12 

13 Unset, empty, or whitespace-only means ``default``. ``1/true/yes/on`` and 

14 ``0/false/no/off`` are recognised case-insensitively, and any other integer 

15 follows C truthiness, so ``2`` is true and ``-0`` is false. 

16 

17 A value that is none of those keeps the historical set-means-true 

18 behaviour rather than raising, because these variables are read during 

19 import and a raise would turn a typo into an import failure. 

20 """ 

21 raw = os.environ.get(name) 1krstudebfglmnocaphivwxyzjAqB

22 if raw is None: 1krstudebfglmnocaphivwxyzjAqB

23 return default 1kvwxyzAB

24 raw = raw.strip() 1rstudebfglmnocaphijq

25 if not raw: 1rstudebfglmnocaphijq

26 return default 1rstu

27 lowered = raw.lower() 1debfglmnocaphijq

28 if lowered in _TRUE_VALUES: 1debfglmnocaphijq

29 return True 1lmnopq

30 if lowered in _FALSE_VALUES: 1debfgcahij

31 return False 1defghij

32 try: 1bca

33 return int(raw, 0) != 0 1bca

34 except ValueError: 1a

35 return True 1a