Skip to content

Commit

Permalink
Remove get_capsule_pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhconradt committed Sep 14, 2024
1 parent 1b70d47 commit f9dd74d
Showing 1 changed file with 0 additions and 31 deletions.
31 changes: 0 additions & 31 deletions codebased/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import ctypes
from functools import lru_cache
from pathlib import Path

Expand Down Expand Up @@ -118,36 +117,6 @@ def get_context(node: tree_sitter.Node) -> tuple[list[int], list[int]]:
return before, after


def get_capsule_pointer(capsule):
# This is a highly unconventional and potentially unsafe method
# It relies on CPython implementation details and may break
# in different Python versions or implementations

# Get the memory address of the capsule object
capsule_address = id(capsule)

# Create a ctypes structure to represent the PyObject
class PyObject(ctypes.Structure):
_fields_ = [("ob_refcnt", ctypes.c_ssize_t),
("ob_type", ctypes.c_void_p)]

# Create a ctypes structure to represent the PyCapsule
class PyCapsule(ctypes.Structure):
_fields_ = [("PyObject_HEAD", PyObject),
("pointer", ctypes.c_void_p),
("name", ctypes.c_char_p),
("context", ctypes.c_void_p),
("destructor", ctypes.c_void_p)]

# Cast the capsule address to a PyCapsule pointer
capsule_struct = ctypes.cast(capsule_address, ctypes.POINTER(PyCapsule)).contents

# Extract the pointer value
pointer_value = capsule_struct.pointer

return pointer_value


def parse_objects(path: Path, text: bytes) -> list[Object]:
file_type = path.suffix[1:]
impl = get_language_for_file_type(file_type)
Expand Down

0 comments on commit f9dd74d

Please sign in to comment.