Skip to content

Commit fd4eeb8

Browse files
committed
readme/pythonapi: attempt PyObject workaround
1 parent 55ca59d commit fd4eeb8

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,39 @@ Alternatively, you may specify a custom pre-processor command using the `--cpp`
3939
#### Binding against the Python API
4040

4141
```bash
42-
printf "import ctypes\nPyTypeObject = ctypes.POINTER(None)\n" > overrides.py
42+
cat >"overrides.py" <<END
43+
import ctypes
44+
45+
class PyTypeObject (ctypes.Structure): pass
46+
class PyObject (ctypes.Structure): pass
47+
48+
def POINTER(obj):
49+
if obj is PyObject: return ctypes.py_object
50+
return ctypes.POINTER(obj)
51+
END
52+
4353
ctypesgen -l python --dllclass pythonapi --system-headers python3.X/Python.h --all-headers -m .overrides --linkage-anchor . -o ctypes_python.py
4454
```
45-
(substituting `3.X` with your system's python version). Minimal test:
55+
substituting `3.X` with your system's python version.
56+
57+
Minimal test (run in python console):
4658
```python
4759
from ctypes import *
4860
from ctypes_python import *
4961

5062
v = Py_GetVersion()
5163
v = cast(v, c_char_p).value.decode("utf-8")
52-
print(v)
64+
v
65+
66+
Py_IncRef(v)
67+
Py_DecRef(v)
5368
```
69+
5470
It should yield something like
5571
```
5672
3.11.6 (main, Oct 3 2023, 00:00:00) [GCC 12.3.1 20230508 (Red Hat 12.3.1-1)]
5773
```
74+
5875
and the same as `sys.version`:
5976
```python
6077
import sys

0 commit comments

Comments
 (0)