Skip to content

Commit d629ced

Browse files
committed
add pycapsulre_failure.py
1 parent 3f3ad78 commit d629ced

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/pycapsule_failure.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from __future__ import annotations
2+
3+
import ctypes
4+
5+
from datafusion import SessionContext, Table
6+
7+
8+
# Keep the backing memory alive for the lifetime of the module so the capsule
9+
# always wraps a valid (non-null) pointer. The capsule content is irrelevant for
10+
# this regression example—we only need a non-null address.
11+
_DUMMY_CAPSULE_BYTES = ctypes.create_string_buffer(b"x")
12+
13+
class CapsuleContainer:
14+
def __init__(self):
15+
self.__datafusion_table_provider__ = make_table_provider_capsule
16+
17+
def make_table_provider_capsule() -> object:
18+
"""Create a dummy PyCapsule with the expected table provider name."""
19+
20+
pycapsule_new = ctypes.pythonapi.PyCapsule_New
21+
pycapsule_new.restype = ctypes.py_object
22+
pycapsule_new.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
23+
dummy_ptr = ctypes.cast(_DUMMY_CAPSULE_BYTES, ctypes.c_void_p)
24+
return pycapsule_new(dummy_ptr, b"datafusion_table_provider", None)
25+
26+
27+
def main() -> None:
28+
"""Attempt to use the capsule the same way existing callers do."""
29+
30+
ctx = SessionContext()
31+
try:
32+
capsule = CapsuleContainer()
33+
except Exception as err:
34+
print("Creating the PyCapsule failed:", err)
35+
return
36+
37+
38+
ctx.read_table(capsule)
39+
40+
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)