Skip to content

Commit 1e94acf

Browse files
committed
Fixing seg fault
1 parent 70cf7e7 commit 1e94acf

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

cuda_core/cuda/core/experimental/_context.pyx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,15 @@ cdef class Context:
4545
True if other is a Context wrapping the same handle, False if not equal,
4646
NotImplemented if other is not a Context.
4747
"""
48-
# Use cast with exception handling instead of isinstance(other, Context)
48+
# Use type identity check + cast with exception handling instead of isinstance(other, Context)
4949
# for performance: isinstance with cdef classes must traverse inheritance
5050
# hierarchies via Python's type checking mechanism, even when other is
51-
# already a Context. In contrast, a direct cast succeeds immediately in
52-
# the common case (other is a Context), and exception handling has very
53-
# low overhead when no exception occurs.
54-
cdef Context _other
55-
try:
56-
_other = <Context>other
57-
except TypeError:
51+
# already a Context. A type identity check (type(other) is Context) is very fast
52+
# and catches non-Context types before attempting the cast, preventing potential
53+
# segfaults from unsafe casts.
54+
if type(other) is not Context:
5855
return NotImplemented
56+
cdef Context _other = <Context>other
5957
return int(self._handle) == int(_other._handle)
6058

6159
def __hash__(self) -> int:

0 commit comments

Comments
 (0)