-
Notifications
You must be signed in to change notification settings - Fork 4
/
CPython.cs
90 lines (65 loc) · 4.6 KB
/
CPython.cs
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
using System;
using System.Runtime.InteropServices;
namespace PythonBinding
{
internal static class CPython
{
private const string pythonDll = @"python35\python35.dll";
internal const string StartupScript =
@"import sys
sys.stdout = open('CONOUT$', 'wt')
print('hello world')";
internal unsafe struct PyObject { }
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Py_DecRef(PyObject* obj);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Py_IncRef(PyObject* obj);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void Py_Initialize();
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void Py_SetProgramName([MarshalAs(UnmanagedType.LPWStr)]string name);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyDict_GetItem(PyObject* dict, PyObject* key);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe double PyFloat_AsDouble(PyObject* value);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyFloat_FromDouble(double value);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyImport_GetModuleDict();
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyIter_Next(PyObject* iterator);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe long PyLong_AsLong(PyObject* value);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyLong_FromLong(long value);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyMapping_GetItemString(PyObject* obj, [MarshalAs(UnmanagedType.LPStr)]string key);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyModule_GetDict(PyObject* module);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyObject_CallObject(PyObject* callable, PyObject* args);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyObject_GetAttrString(PyObject* obj, [MarshalAs(UnmanagedType.LPStr)]string attrName);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyObject_GetItem(PyObject* obj, PyObject* key);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyObject_GetIter(PyObject* obj);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyObject_Str(PyObject* obj);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int PyObject_Print(PyObject* toPrint, IntPtr filestreamHandle, int mode);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyRun_SimpleString([MarshalAs(UnmanagedType.LPStr)]string toRun);
// Don't forget to wrap variadic things in `__arglist(...)`
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyTuple_Pack(IntPtr size, __arglist);
// TODO - fix leak and null
// Returns a buffer allocated by PyMem_Alloc() (use PyMem_Free() to free it) on success.
// Note that the resulting wchar_t string might contain null characters, which would cause the string to be truncated when used with most C functions.
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)]
internal static extern unsafe string PyUnicode_AsWideCharString(PyObject* str, IntPtr size);
[DllImport(pythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe PyObject* PyUnicode_FromString([MarshalAs(UnmanagedType.LPStr)]string str);
}
}