-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpythonCAPI.cc
97 lines (79 loc) · 4.32 KB
/
pythonCAPI.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
@copyright Russell Standish 2025
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
// implements stubs to link to the Windows python dynamic library
#include "pythonCAPI.h"
#include <windows.h>
#include <iostream>
static HINSTANCE pythonExe=GetModuleHandle("python3");
// On Windows, python3 refers to the stable ABI. checkSymbol provides
// an important check that all symbols referenced here are available
// in the stable ABI. This check should be performed prior to release.
static void checkSymbol(const char* name)
{
auto symbol=GetProcAddress(pythonExe, name);
if (!symbol)
std::cerr<<name<<" not found in module"<<std::endl;
}
#define CHECK_SYMBOL(name) static int checkSymbol_##name=(checkSymbol(#name),1);
extern "C"
{
#define APIFN(returnType,name,arg_decls,args) \
CHECK_SYMBOL(name) \
returnType name arg_decls \
{ \
if (!pythonExe) puts("python exe not found"); \
static auto symbol=(decltype(name)*)GetProcAddress(pythonExe, #name); \
if (!symbol) puts("failed to load: "#name); \
return symbol? symbol args: 0; \
}
#define VOID_APIFN(name,arg_decls,args) \
CHECK_SYMBOL(name) \
void name arg_decls \
{ \
static auto symbol=(decltype(name)*)GetProcAddress(pythonExe, #name); \
if (!symbol) puts("failed to load: "#name); \
if (symbol) symbol args; \
}
#define APIVARPTR(type, name) \
type* name=(type*)GetProcAddress(pythonExe, #name); \
CHECK_SYMBOL(name)
APIVARPTR(PyTypeObject, PyBool_Type);
APIVARPTR(PyTypeObject, PyFloat_Type);
APIVARPTR(PyObject, PyExc_RuntimeError);
VOID_APIFN(_Py_Dealloc,(PyObject*o),(o));
APIFN(PyObject*, PyErr_Occurred, (), ());
VOID_APIFN(PyErr_Print,(),());
VOID_APIFN(PyErr_SetString,(PyObject* o,const char* s),(o,s));
APIFN(PyObject*, Py_GetConstantBorrowed,(unsigned x),(x));
APIFN(int, PyType_IsSubtype,(PyTypeObject* o1, PyTypeObject* o2),(o1,o2));
APIFN(unsigned long, PyType_GetFlags,(PyTypeObject* o),(o));
APIFN(PyObject*, PyLong_FromLong,(long x),(x));
APIFN(PyObject*, PyFloat_FromDouble,(double x),(x));
APIFN(long long, PyLong_AsLongLong, (PyObject* x), (x));
APIFN(double, PyFloat_AsDouble, (PyObject* x), (x));
APIFN(int, PyType_Ready, (PyTypeObject* t), (t));
APIFN(PyObject*, PyObject_Str, (PyObject* o), (o));
APIFN(PyObject*, PyObject_Dir, (PyObject* o), (o));
APIFN(PyObject*, PyObject_GetAttr, (PyObject* o, PyObject* a), (o,a));
APIFN(PyObject*, PyObject_GenericGetAttr, (PyObject* o, PyObject* a), (o,a));
APIFN(int, PyObject_SetAttrString, (PyObject* o, const char* a, PyObject* v), (o,a,v));
APIFN(int, PyModule_AddObject, (PyObject* o, const char* n, PyObject* v), (o,n,v));
APIFN(const char*, PyModule_GetName, (PyObject* o), (o));
APIFN(PyObject*, PyModule_Create2, (PyModuleDef* m,int i), (m,i));
APIFN(int, PySequence_Check, (PyObject* o), (o));
APIFN(ssize_t, PySequence_Size, (PyObject* o), (o));
APIFN(PyObject*, PySequence_GetItem, (PyObject* o, ssize_t i), (o,i));
APIFN(PyObject*, PyUnicode_FromString, (const char* s), (s));
APIFN(char*, PyUnicode_AsUTF8AndSize, (PyObject* s,Py_ssize_t* sz), (s,sz));
APIFN(PyObject*, PyDict_New, (), ());
APIFN(int, PyDict_SetItemString, (PyObject* d, const char* k, PyObject* v), (d,k,v));
APIFN(PyObject*, PyList_New, (ssize_t size), (size));
APIFN(int, PyList_SetItem, (PyObject* o, ssize_t i, PyObject* v), (o,i,v));
APIFN(int, PyList_Append, (PyObject* o, PyObject* v), (o,v));
APIFN(int, PyMapping_Check, (PyObject* o), (o));
APIFN(PyObject*, PyMapping_Items, (PyObject* o), (o));
}