Skip to content

Commit 84cbd24

Browse files
pythonAPI.cc for Windows builds.
1 parent d6ed79d commit 84cbd24

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

pythonCAPI.cc

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
@copyright Russell Standish 2025
3+
@author Russell Standish
4+
This file is part of Classdesc
5+
6+
Open source licensed under the MIT license. See LICENSE for details.
7+
*/
8+
9+
// implements stubs to link to the Windows python dynamic library
10+
11+
#include "pythonCAPI.h"
12+
#include <windows.h>
13+
#include <iostream>
14+
15+
static HINSTANCE pythonExe=GetModuleHandle(nullptr);
16+
17+
extern "C"
18+
{
19+
20+
#define APIFN(return,name,arg_decls,args) \
21+
return name arg_decls \
22+
{ \
23+
static auto symbol=(decltype(name)*)GetProcAddress(pythonExe, #name); \
24+
return symbol? symbol args: 0; \
25+
} \
26+
27+
#define VOID_APIFN(name,arg_decls,args) \
28+
void name arg_decls \
29+
{ \
30+
static auto symbol=(decltype(name)*)GetProcAddress(pythonExe, #name); \
31+
if (symbol) symbol args; \
32+
} \
33+
34+
VOID_APIFN(_Py_Dealloc,(PyObject*o),(o));
35+
APIFN(PyObject*, PyErr_Occurred, (), ());
36+
VOID_APIFN(PyErr_Print,()());
37+
VOID_APIFN(PyErr_SetString,(PyObject* o,const char* s),(o,s));
38+
39+
APIFN(int, PyType_IsSubtype,(PyTypeObject* o1, PyTypeObject* o2),(o1,o2));
40+
APIFN(unsigned long, PyType_GetFlags,(PyTypeObject* o),(o));
41+
APIFN(PyObject*, PyLong_FromLong,(long x),(x));
42+
APIFN(PyObject*, PyFloat_FromDouble,(double x),(x));
43+
APIFN(long long, PyLong_AsLongLong, (PyObject* x), (x));
44+
APIFN(double, PyFloat_AsDouble, (PyObject* x), (x));
45+
APIFN(int, PyType_Ready, (PyTypeObject* t), (t));
46+
47+
APIFN(PyObject*, PyObject_Str, (PyObject* o), (o));
48+
APIFN(PyObject*, PyObject_Dir, (PyObject* o), (o));
49+
APIFN(PyObject*, PyObject_GetAttr, (PyObject* o, PyObject* a), (o,a));
50+
APIFN(PyObject*, PyObject_GenericGetAttr, (PyObject* o, PyObject* a), (o,a));
51+
APIFN(int, PyObject_SetAttrString, (PyObject* o, const char* a, PyObject* v), (o,a,v));
52+
53+
APIFN(int, PyModule_AddObject, (PyObject* o, const char* n, PyObject* v), (o,n,v));
54+
APIFN(const char*, PyModule_GetName, (PyObject* o), (o));
55+
APIFN(PyObject*, PyModule_Create2, (PyModuleDef* m,int i), (m,i));
56+
APIFN(int, PySequence_Check, (PyObject* o), (o));
57+
APIFN(ssize_t, PySequence_Size, (PyObject* o), (o));
58+
APIFN(PyObject*, PySequence_GetItem, (PyObject* o, ssize_t i), (o,i));
59+
60+
APIFN(PyObject*, PyUnicode_FromString, (const char* s), (s));
61+
APIFN(char*, PyUnicode_AsUTF8, (PyObject* s), (s));
62+
63+
APIFN(PyObject*, PyDict_New, (), ());
64+
APIFN(int, PyDict_SetItemString, (PyObject* d, const char* k, PyObject* v), (d,k,v));
65+
66+
APIFN(PyObject*, PyList_New, (ssize_t size), (size));
67+
APIFN(int, PyList_SetItem, (PyObject* o, ssize_t i, PyObject* v), (o,i,v));
68+
APIFN(int, PyList_Append, (PyObject* o, PyObject* v), (o,v));
69+
70+
APIFN(int, PyMapping_Check, (PyObject* o), (o));
71+
APIFN(PyObject*, PyMapping_Items, (PyObject* o), (o));
72+
}

0 commit comments

Comments
 (0)