Skip to content

Commit

Permalink
readme/pythonapi: attempt PyObject workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Feb 16, 2024
1 parent 55ca59d commit 8590bae
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,39 @@ Alternatively, you may specify a custom pre-processor command using the `--cpp`
#### Binding against the Python API

```bash
printf "import ctypes\nPyTypeObject = ctypes.POINTER(None)\n" > overrides.py
cat >"overrides.py" <<END
import ctypes
class PyObject (ctypes.Structure): pass
PyTypeObject = ctypes.POINTER(None)
def POINTER(obj):
if obj is PyObject: return ctypes.py_object
return ctypes.POINTER(obj)
END

ctypesgen -l python --dllclass pythonapi --system-headers python3.X/Python.h --all-headers -m .overrides --linkage-anchor . -o ctypes_python.py
```
(substituting `3.X` with your system's python version). Minimal test:
substituting `3.X` with your system's python version.

Minimal test (run in python console):
```python
from ctypes import *
from ctypes_python import *

v = Py_GetVersion()
v = cast(v, c_char_p).value.decode("utf-8")
print(v)
v

Py_IncRef(v)
Py_DecRef(v)
```

It should yield something like
```
3.11.6 (main, Oct 3 2023, 00:00:00) [GCC 12.3.1 20230508 (Red Hat 12.3.1-1)]
```

and the same as `sys.version`:
```python
import sys
Expand Down

0 comments on commit 8590bae

Please sign in to comment.