-
Notifications
You must be signed in to change notification settings - Fork 171
Apply __new__
approach to disabling __init__
#484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,18 +71,37 @@ def close(self): | |
self.owner = None | ||
self.handle = None | ||
|
||
def __new__(self, *args, **kwargs): | ||
raise RuntimeError( | ||
"Stream objects cannot be instantiated directly. " | ||
"Please use Device APIs (create_stream) or other Stream APIs (from_handle)." | ||
) | ||
|
||
__slots__ = ("__weakref__", "_mnff", "_nonblocking", "_priority", "_device_id", "_ctx_handle") | ||
|
||
def __init__(self): | ||
raise NotImplementedError( | ||
"directly creating a Stream object can be ambiguous. Please either " | ||
"call Device.create_stream() or, if a stream pointer is already " | ||
"available from somewhere else, Stream.from_handle()" | ||
) | ||
@classmethod | ||
def _legacy_default(cls): | ||
self = super().__new__(cls) | ||
self._mnff = Stream._MembersNeededForFinalize(self, driver.CUstream(driver.CU_STREAM_LEGACY), None, True) | ||
self._nonblocking = None # delayed | ||
self._priority = None # delayed | ||
self._device_id = None # delayed | ||
self._ctx_handle = None # delayed | ||
return self | ||
|
||
@staticmethod | ||
def _init(obj=None, *, options: Optional[StreamOptions] = None): | ||
self = Stream.__new__(Stream) | ||
@classmethod | ||
def _per_thread_default(cls): | ||
self = super().__new__(cls) | ||
self._mnff = Stream._MembersNeededForFinalize(self, driver.CUstream(driver.CU_STREAM_PER_THREAD), None, True) | ||
self._nonblocking = None # delayed | ||
self._priority = None # delayed | ||
self._device_id = None # delayed | ||
self._ctx_handle = None # delayed | ||
Comment on lines
+98
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
return self | ||
|
||
@classmethod | ||
def _init(cls, obj=None, *, options: Optional[StreamOptions] = None): | ||
self = super().__new__(cls) | ||
self._mnff = Stream._MembersNeededForFinalize(self, None, None, False) | ||
|
||
if obj is not None and options is not None: | ||
|
@@ -295,22 +314,8 @@ def __cuda_stream__(self): | |
return Stream._init(obj=_stream_holder()) | ||
|
||
|
||
class _LegacyDefaultStream(Stream): | ||
def __init__(self): | ||
self._mnff = Stream._MembersNeededForFinalize(self, driver.CUstream(driver.CU_STREAM_LEGACY), None, True) | ||
self._nonblocking = None # delayed | ||
self._priority = None # delayed | ||
|
||
|
||
class _PerThreadDefaultStream(Stream): | ||
def __init__(self): | ||
self._mnff = Stream._MembersNeededForFinalize(self, driver.CUstream(driver.CU_STREAM_PER_THREAD), None, True) | ||
self._nonblocking = None # delayed | ||
self._priority = None # delayed | ||
|
||
|
||
LEGACY_DEFAULT_STREAM = _LegacyDefaultStream() | ||
PER_THREAD_DEFAULT_STREAM = _PerThreadDefaultStream() | ||
LEGACY_DEFAULT_STREAM = Stream._legacy_default() | ||
PER_THREAD_DEFAULT_STREAM = Stream._per_thread_default() | ||
|
||
|
||
def default_stream(): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2025 NVIDIA Corporation. All rights reserved. | ||
# | ||
# Please refer to the NVIDIA end user license agreement (EULA) associated | ||
# with this source code for terms and conditions that govern your use of | ||
# this software. Any use, reproduction, disclosure, or distribution of | ||
# this software and related documentation outside the terms of the EULA | ||
# is strictly prohibited. | ||
|
||
import pytest | ||
|
||
import cuda.core.experimental | ||
|
||
|
||
def test_context_init_disabled(): | ||
with pytest.raises(RuntimeError, match=r"^Context objects cannot be instantiated directly\."): | ||
cuda.core.experimental._context.Context() # Ensure back door is locked. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this is actually a bug fix too! I wonder why it wasn't caught before...