Skip to content

Commit ce2ca30

Browse files
committed
Move quote_for_shell() to new _utils/platform_aware.py
1 parent bd5e2ae commit ce2ca30

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import sys
5+
6+
IS_WINDOWS = sys.platform == "win32"
7+
8+
9+
def quote_for_shell(s: str) -> str:
10+
if IS_WINDOWS:
11+
# This is a relatively heavy import; keep pathfinder if possible.
12+
from subprocess import list2cmdline # nosec B404
13+
14+
return list2cmdline([s])
15+
else:
16+
import shlex
17+
18+
return shlex.quote(s)

cuda_pathfinder/tests/test_load_nvidia_dynamic_lib.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import functools
55
import json
66
import os
7-
import shlex
8-
from subprocess import list2cmdline # nosec B404
97
from unittest.mock import patch
108

119
import pytest
@@ -16,6 +14,7 @@
1614
from cuda.pathfinder._dynamic_libs import supported_nvidia_libs
1715
from cuda.pathfinder._utils.find_site_packages_dll import find_all_dll_files_via_metadata
1816
from cuda.pathfinder._utils.find_site_packages_so import find_all_so_files_via_metadata
17+
from cuda.pathfinder._utils.platform_aware import quote_for_shell
1918

2019
STRICTNESS = os.environ.get("CUDA_PATHFINDER_TEST_LOAD_NVIDIA_DYNAMIC_LIB_STRICTNESS", "see_what_works")
2120
assert STRICTNESS in ("see_what_works", "all_must_work")
@@ -93,12 +92,6 @@ def _get_libnames_for_test_load_nvidia_dynamic_lib():
9392
return tuple(result)
9493

9594

96-
def _quote_for_shell_copypaste(s):
97-
if os.name == "nt":
98-
return list2cmdline([s])
99-
return shlex.quote(s)
100-
101-
10295
@pytest.mark.parametrize("libname", _get_libnames_for_test_load_nvidia_dynamic_lib())
10396
def test_load_nvidia_dynamic_lib(info_summary_append, libname):
10497
# We intentionally run each dynamic library operation in a child process
@@ -120,5 +113,5 @@ def raise_child_process_failed():
120113
info_summary_append(f"Not found: {libname=!r}")
121114
else:
122115
abs_path = json.loads(result.stdout.rstrip())
123-
info_summary_append(f"abs_path={_quote_for_shell_copypaste(abs_path)}")
116+
info_summary_append(f"abs_path={quote_for_shell(abs_path)}")
124117
assert os.path.isfile(abs_path) # double-check the abs_path

0 commit comments

Comments
 (0)