Skip to content

ENH: Add support instantiating ObjectCode with path-like instances#2123

Open
acosmicflamingo wants to merge 9 commits into
NVIDIA:mainfrom
acosmicflamingo:objectcode-pathlike-support
Open

ENH: Add support instantiating ObjectCode with path-like instances#2123
acosmicflamingo wants to merge 9 commits into
NVIDIA:mainfrom
acosmicflamingo:objectcode-pathlike-support

Conversation

@acosmicflamingo
Copy link
Copy Markdown

Resolves #755

Before looking into addressing #663, I noticed @kkraus14 had pointed out missing pathlib-like support and figured I could give it a try it a go.

One improvement I could make to _lazy_load_module is the ask-for-forgiveness approach to avoid checking module type twice. It would only require declaring path_bytes before the try block:

diff --git a/cuda_core/cuda/core/_module.pyx b/cuda_core/cuda/core/_module.pyx
index 96ac65effc..189971aed7 100644
--- a/cuda_core/cuda/core/_module.pyx
+++ b/cuda_core/cuda/core/_module.pyx
@@ -733,8 +733,16 @@ cdef class ObjectCode:
         if self._h_library:
             return 0
         module = self._module
-        assert_type_str_or_bytes_like(module)
         cdef bytes path_bytes
+        try:
+            assert_type(module, os.PathLike)
+            path_bytes = os.fsencode(module)
+            self._h_library = create_library_handle_from_file(<const char*>path_bytes)
+            if not self._h_library:
+                HANDLE_RETURN(get_last_error())
+            return 0
+        except TypeError:
+            assert_type_str_or_bytes_like(module)
         if isinstance(module, str):
             path_bytes = module.encode()
             self._h_library = create_library_handle_from_file(<const char*>path_bytes)

@copy-pr-bot
Copy link
Copy Markdown
Contributor

copy-pr-bot Bot commented May 21, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label May 21, 2026
@acosmicflamingo
Copy link
Copy Markdown
Author

This could also be the updated documentation for the change:

diff --git a/cuda_core/cuda/core/_module.pyx b/cuda_core/cuda/core/_module.pyx
index ea334aae07..b5b0b441aa 100644
--- a/cuda_core/cuda/core/_module.pyx
+++ b/cuda_core/cuda/core/_module.pyx
@@ -628,9 +628,10 @@ cdef class ObjectCode:
 
         Parameters
         ----------
-        module : Union[bytes, str]
-            Either a bytes object containing the in-memory cubin to load, or
-            a file path string pointing to the on-disk cubin to load.
+        module : Union[bytes, str, os.PathLike]
+            Either a bytes object containing the in-memory cubin to load,
+            the file path pointing to the on-disk cubin to load,
+            or the string representation of the file path
         name : Optional[str]
             A human-readable identifier representing this code object.
         symbol_mapping : Optional[dict]

Comment thread cuda_core/cuda/core/_module.pyx Outdated
Comment thread cuda_core/cuda/core/_module.pyx Outdated
Comment thread cuda_core/cuda/core/_module.pyx Outdated
Comment thread cuda_core/cuda/core/_module.pyx Outdated
@kkraus14
Copy link
Copy Markdown
Collaborator

/ok to test 837e70c

@kkraus14 kkraus14 added the enhancement Any code-related improvements label May 22, 2026
@kkraus14 kkraus14 added this to the cuda.core v1.1.0 milestone May 22, 2026
@acosmicflamingo
Copy link
Copy Markdown
Author

Whoops, I expect tests to fail because os.fsencode and os.fspath is still there after I removed import os. I'll push fix after verifying that cython injects clean C-API in that isinstance(modules, CodeTypeT) call.

@acosmicflamingo
Copy link
Copy Markdown
Author

/ok to test 6ed4a7f

@kkraus14
Copy link
Copy Markdown
Collaborator

/ok to test 6ed4a7f

@kkraus14
Copy link
Copy Markdown
Collaborator

We require maintainers to trigger CI 😄

@github-actions
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module enhancement Any code-related improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pathlib wherever we accept a string for the file path

2 participants