diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fe43b52d01a..a60d64d1a48 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -416,6 +416,38 @@ jobs: with: is-release: ${{ github.ref_type == 'tag' }} + precommit-windows: + name: Pre-commit on Windows + runs-on: windows-latest + if: ${{ github.repository_owner == 'nvidia' && !fromJSON(needs.should-skip.outputs.skip) }} + needs: + - should-skip + permissions: + contents: read + steps: + - name: Checkout repository + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + with: + fetch-depth: 1 + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 + with: + python-version: '3.13' + + - name: Install pre-commit + shell: bash + run: | + set -euxo pipefail + python -m pip install --upgrade pip pre-commit + + - name: Run pre-commit + shell: bash + run: | + set -euxo pipefail + SKIP=lychee pre-commit run --all-files + checks: name: Check job status if: always() @@ -429,6 +461,7 @@ jobs: - test-linux-aarch64 - test-windows - doc + - precommit-windows steps: - name: Exit run: | @@ -461,6 +494,7 @@ jobs: check_result "should-skip" "success" "${{ needs.should-skip.result }}" check_result "detect-changes" "success" "${{ needs.detect-changes.result }}" check_result "doc" "success" "${{ needs.doc.result }}" + check_result "precommit-windows" "success" "${{ needs.precommit-windows.result }}" # [doc-only] flips these from 'success' to 'skipped' if [[ "$doc_only" == "true" ]]; then expected="skipped"; else expected="success"; fi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 029e69da916..3e7b96cf1aa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,7 +61,7 @@ repos: - id: stubgen-pyx-cuda-core name: Generate .pyi stubs for cuda_core - entry: stubgen-pyx cuda_core/cuda --continue-on-error --include-private + entry: python ./toolshed/run_stubgen_pyx.py language: python files: ^cuda_core/cuda/.*\.(pyx|pxd)$ pass_filenames: false @@ -95,7 +95,7 @@ repos: - id: check-yaml - id: debug-statements - id: end-of-file-fixer - exclude: &gen_exclude '^(?:cuda_python/README\.md|cuda_bindings/cuda/bindings/.*\.in?|cuda_bindings/docs/source/module/.*\.rst?|.*\.pyi)$' + exclude: &gen_exclude '^(?:cuda_python/README\.md|(?:.*/)?CLAUDE\.md|(?:.*/)?\.git_archival\.txt|cuda_bindings/cuda/bindings/.*\.in?|cuda_bindings/docs/source/module/.*\.rst?|.*\.pyi)$' - id: mixed-line-ending - id: trailing-whitespace exclude: | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 012126cc842..38a59133313 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,6 +21,7 @@ Thank you for your interest in contributing to CUDA Python! Based on the type of - [Table of Contents](#table-of-contents) - [Type stubs for cuda.core](#type-stubs-for-cudacore) - [Pre-commit](#pre-commit) + - [Pre-commit on Windows](#pre-commit-on-windows) - [Signing Your Work](#signing-your-work) - [Code signing](#code-signing) - [Developer Certificate of Origin (DCO)](#developer-certificate-of-origin-dco) @@ -74,6 +75,17 @@ between commits, leaving stale headers or out-of-date stubs in the history. If the hook isn't installed, `pre-commit run` (and CI) will print a visible warning reminding you to run `pre-commit install`. +### Pre-commit on Windows + +For development on Windows (not WSL), the `lychee` pre-commit task will not work +when running `pre-commit run --all-files`. This problem does not occur if you +install the pre-commit hook and run it automatically as part of your `git +commit` workflow. To resolve this, you can either: + +1. Run `pre-commit` in Git Bash, rather directly in PowerShell or cmd + +2. Skip it by setting the environment variable `SKIP` to `lychee`. This would + be `$env:SKIP = "lychee"` in PowerShell or `set SKIP=lychee` in cmd. ## Signing Your Work diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py index 10a92c20830..24d4d67c9e2 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations @@ -7,6 +7,7 @@ import ctypes import ctypes.util import os +import sys from typing import TYPE_CHECKING, cast from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL @@ -14,7 +15,10 @@ if TYPE_CHECKING: from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor -CDLL_MODE = os.RTLD_NOW | os.RTLD_GLOBAL +if sys.platform == "linux": + CDLL_MODE = os.RTLD_NOW | os.RTLD_GLOBAL +else: + CDLL_MODE = 0 def _load_libdl() -> ctypes.CDLL: @@ -132,27 +136,35 @@ def _candidate_sonames(desc: LibDescriptor) -> list[str]: return candidates -def check_if_already_loaded_from_elsewhere(desc: LibDescriptor, _have_abs_path: bool) -> LoadedDL | None: - for soname in _candidate_sonames(desc): - try: - handle = ctypes.CDLL(soname, mode=os.RTLD_NOLOAD) - except OSError: - continue - else: - return LoadedDL( - abs_path_for_dynamic_library(desc.name, handle), - True, - handle._handle, - "was-already-loaded-from-elsewhere", - ) - return None - - -def _load_lib(desc: LibDescriptor, filename: str) -> ctypes.CDLL: - cdll_mode = CDLL_MODE - if desc.requires_rtld_deepbind: - cdll_mode |= os.RTLD_DEEPBIND - return ctypes.CDLL(filename, cdll_mode) +if sys.platform == "linux": + + def check_if_already_loaded_from_elsewhere(desc: LibDescriptor, _have_abs_path: bool) -> LoadedDL | None: + for soname in _candidate_sonames(desc): + try: + handle = ctypes.CDLL(soname, mode=os.RTLD_NOLOAD) + except OSError: + continue + else: + return LoadedDL( + abs_path_for_dynamic_library(desc.name, handle), + True, + handle._handle, + "was-already-loaded-from-elsewhere", + ) + return None + + def _load_lib(desc: LibDescriptor, filename: str) -> ctypes.CDLL: + cdll_mode = CDLL_MODE + if desc.requires_rtld_deepbind: + cdll_mode |= os.RTLD_DEEPBIND + return ctypes.CDLL(filename, cdll_mode) +else: + + def check_if_already_loaded_from_elsewhere(_desc: LibDescriptor, _have_abs_path: bool) -> LoadedDL | None: + raise RuntimeError(f"check_if_already_loaded_from_elsewhere() is not supported on platform {sys.platform!r}") + + def _load_lib(_desc: LibDescriptor, _filename: str) -> ctypes.CDLL: + raise RuntimeError(f"_load_lib() is not supported on platform {sys.platform!r}") def load_with_system_search(desc: LibDescriptor) -> LoadedDL | None: diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py index b2f61dfc9af..9436d353e38 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations @@ -7,6 +7,7 @@ import ctypes.wintypes import os import struct +import sys import warnings from typing import TYPE_CHECKING @@ -22,7 +23,10 @@ POINTER_ADDRESS_SPACE = 2 ** (struct.calcsize("P") * 8) # Set up kernel32 functions with proper types -kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined] +windll = getattr(ctypes, "windll", None) +if windll is None: + raise RuntimeError("ctypes.windll is required on Windows") +kernel32 = windll.kernel32 # GetModuleHandleW kernel32.GetModuleHandleW.argtypes = [ctypes.wintypes.LPCWSTR] @@ -44,6 +48,14 @@ ] kernel32.GetModuleFileNameW.restype = ctypes.wintypes.DWORD +# AddDllDirectory (Windows 7+) +kernel32.AddDllDirectory.argtypes = [ctypes.wintypes.LPCWSTR] +kernel32.AddDllDirectory.restype = ctypes.c_void_p # DLL_DIRECTORY_COOKIE + +# GetLastError +kernel32.GetLastError.argtypes = [] +kernel32.GetLastError.restype = ctypes.wintypes.DWORD + def ctypes_handle_to_unsigned_int(handle: ctypes.wintypes.HMODULE) -> int: """Convert ctypes HMODULE to unsigned int.""" @@ -73,7 +85,8 @@ def add_dll_directory(dll_abs_path: str) -> None: # the directory must stay on the search path for the process lifetime, and # the handle has no finalizer, so dropping it does not remove the directory. try: - os.add_dll_directory(dirpath) # type: ignore[attr-defined] + if sys.platform == "win32": + os.add_dll_directory(dirpath) except OSError as e: # Warn instead of failing silently; the PATH update below is a weaker # fallback that newer loaders may ignore. @@ -96,7 +109,7 @@ def abs_path_for_dynamic_library(libname: str, handle: ctypes.wintypes.HMODULE) length = kernel32.GetModuleFileNameW(handle, buffer, len(buffer)) if length == 0: - error_code = ctypes.GetLastError() # type: ignore[attr-defined] + error_code = kernel32.GetLastError() raise RuntimeError(f"GetModuleFileNameW failed for {libname!r} (error code: {error_code})") # If buffer was too small, try with larger buffer @@ -104,7 +117,7 @@ def abs_path_for_dynamic_library(libname: str, handle: ctypes.wintypes.HMODULE) buffer = ctypes.create_unicode_buffer(32768) # Extended path length length = kernel32.GetModuleFileNameW(handle, buffer, len(buffer)) if length == 0: - error_code = ctypes.GetLastError() # type: ignore[attr-defined] + error_code = kernel32.GetLastError() raise RuntimeError(f"GetModuleFileNameW failed for {libname!r} (error code: {error_code})") return buffer.value @@ -170,7 +183,7 @@ def load_with_abs_path(desc: LibDescriptor, found_path: str, found_via: str | No handle = kernel32.LoadLibraryExW(found_path, None, flags) if not handle: - error_code = ctypes.GetLastError() # type: ignore[attr-defined] + error_code = kernel32.GetLastError() raise RuntimeError(f"Failed to load DLL at {found_path}: Windows error {error_code}") return LoadedDL(found_path, False, ctypes_handle_to_unsigned_int(handle), found_via) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/platform_loader.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/platform_loader.py index 9b108a57acc..64bf55efe3d 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/platform_loader.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/platform_loader.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 """Platform loader seam for OS-specific dynamic linking. @@ -16,11 +16,11 @@ from __future__ import annotations +import sys from typing import Protocol from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL -from cuda.pathfinder._utils.platform_aware import IS_WINDOWS class PlatformLoader(Protocol): @@ -31,7 +31,7 @@ def load_with_system_search(self, desc: LibDescriptor) -> LoadedDL | None: ... def load_with_abs_path(self, desc: LibDescriptor, found_path: str, found_via: str | None = None) -> LoadedDL: ... -if IS_WINDOWS: +if sys.platform == "win32": from cuda.pathfinder._dynamic_libs import load_dl_windows as _impl else: from cuda.pathfinder._dynamic_libs import load_dl_linux as _impl diff --git a/cuda_pathfinder/cuda/pathfinder/_utils/driver_info.py b/cuda_pathfinder/cuda/pathfinder/_utils/driver_info.py index a5d4d167d33..d07c4b861d3 100644 --- a/cuda_pathfinder/cuda/pathfinder/_utils/driver_info.py +++ b/cuda_pathfinder/cuda/pathfinder/_utils/driver_info.py @@ -5,13 +5,13 @@ import ctypes import functools +import sys from collections.abc import Callable from dataclasses import dataclass from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( load_nvidia_dynamic_lib as _load_nvidia_dynamic_lib, ) -from cuda.pathfinder._utils.platform_aware import IS_WINDOWS class QueryDriverCudaVersionError(RuntimeError): @@ -60,16 +60,16 @@ def query_driver_cuda_version() -> DriverCudaVersion: raise QueryDriverCudaVersionError("Failed to query the CUDA driver version.") from exc +if sys.platform == "win32": + _DRIVER_LIB_LOADER: Callable[[str], ctypes.CDLL] = ctypes.WinDLL +else: + _DRIVER_LIB_LOADER = ctypes.CDLL + + def _query_driver_cuda_version_int() -> int: """Return the encoded CUDA driver version from ``cuDriverGetVersion()``.""" loaded_cuda = _load_nvidia_dynamic_lib("cuda") - if IS_WINDOWS: - # `ctypes.WinDLL` exists on Windows at runtime. The ignore is only for - # Linux mypy runs, where the platform stubs do not define that attribute. - loader_cls: Callable[[str], ctypes.CDLL] = ctypes.WinDLL # type: ignore[attr-defined] - else: - loader_cls = ctypes.CDLL - driver_lib = loader_cls(loaded_cuda.abs_path) + driver_lib = _DRIVER_LIB_LOADER(loaded_cuda.abs_path) cu_driver_get_version = driver_lib.cuDriverGetVersion cu_driver_get_version.argtypes = [ctypes.POINTER(ctypes.c_int)] cu_driver_get_version.restype = ctypes.c_int diff --git a/cuda_pathfinder/tests/test_utils_driver_info.py b/cuda_pathfinder/tests/test_utils_driver_info.py index 21948dadafe..0b3cd61d299 100644 --- a/cuda_pathfinder/tests/test_utils_driver_info.py +++ b/cuda_pathfinder/tests/test_utils_driver_info.py @@ -46,7 +46,6 @@ def test_query_driver_cuda_version_uses_windll_on_windows(monkeypatch): fake_driver_lib = _FakeDriverLib(status=0, version=12080) loaded_paths: list[str] = [] - monkeypatch.setattr(driver_info, "IS_WINDOWS", True) monkeypatch.setattr( driver_info, "_load_nvidia_dynamic_lib", @@ -57,7 +56,7 @@ def fake_windll(abs_path: str): loaded_paths.append(abs_path) return fake_driver_lib - monkeypatch.setattr(driver_info.ctypes, "WinDLL", fake_windll, raising=False) + monkeypatch.setattr(driver_info, "_DRIVER_LIB_LOADER", fake_windll) assert driver_info._query_driver_cuda_version_int() == 12080 assert loaded_paths == [r"C:\Windows\System32\nvcuda.dll"] @@ -93,9 +92,8 @@ def fail_query_driver_cuda_version_int() -> int: def test_query_driver_cuda_version_int_raises_when_cuda_call_fails(monkeypatch): fake_driver_lib = _FakeDriverLib(status=1, version=0) - monkeypatch.setattr(driver_info, "IS_WINDOWS", False) monkeypatch.setattr(driver_info, "_load_nvidia_dynamic_lib", lambda _libname: _loaded_cuda("/usr/lib/libcuda.so.1")) - monkeypatch.setattr(driver_info.ctypes, "CDLL", lambda _abs_path: fake_driver_lib) + monkeypatch.setattr(driver_info, "_DRIVER_LIB_LOADER", lambda _abs_path: fake_driver_lib) with pytest.raises(RuntimeError, match=r"cuDriverGetVersion\(\) \(status=1\)"): driver_info._query_driver_cuda_version_int() diff --git a/toolshed/run_stubgen_pyx.py b/toolshed/run_stubgen_pyx.py new file mode 100644 index 00000000000..1a163ff0778 --- /dev/null +++ b/toolshed/run_stubgen_pyx.py @@ -0,0 +1,54 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Run stubgen-pyx for cuda_core and normalize the generated stub headers. + +stubgen-pyx emits a path using the OS path separator in the first-line comment +(e.g. "# This file was generated by stubgen-pyx from cuda_core\\cuda\\..."). +This wrapper rewrites that separator to "/" so committed stubs are identical +across platforms. Line-ending normalization is handled by .gitattributes. + +This also forces stubgen-pyx to write files with UTF-8 encoding, which is not +the default on Windows. + +This wrapper can be removed once these stubgen-pyx issues are resolved: + https://github.com/jon-edward/stubgen-pyx/issues/41 + https://github.com/jon-edward/stubgen-pyx/issues/42 +""" + +from __future__ import annotations + +import os +import pathlib +import subprocess +import sys + +_HEADER_PREFIX = b"# This file was generated by stubgen-pyx" + + +def _normalize_stub_headers(root: pathlib.Path) -> None: + for stub in root.rglob("*.pyi"): + data = stub.read_bytes() + newline = data.find(b"\n") + first_line = data[:newline] if newline != -1 else data + if not first_line.startswith(_HEADER_PREFIX) or b"\\" not in first_line: + continue + stub.write_bytes(first_line.replace(b"\\", b"/") + data[newline:]) + + +def main() -> int: + env = os.environ.copy() + env.setdefault("PYTHONUTF8", "1") + env.setdefault("PYTHONIOENCODING", "utf-8") + result = subprocess.run( + ["stubgen-pyx", "cuda_core/cuda", "--continue-on-error", "--include-private"], # noqa: S607 + env=env, + ) + if result.returncode != 0: + return result.returncode + _normalize_stub_headers(pathlib.Path("cuda_core/cuda")) + return 0 + + +if __name__ == "__main__": + sys.exit(main())