-
Notifications
You must be signed in to change notification settings - Fork 4
/
PyObj.cs
130 lines (118 loc) · 4.2 KB
/
PyObj.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
namespace PythonBinding
{
public class PyObj
{
unsafe internal CPython.PyObject* obj;
/// These should only be constructed by APIs in this assembly.
unsafe internal PyObj(CPython.PyObject* obj)
{
CPython.Py_IncRef(obj);
this.obj = obj;
}
/// Tell Python when we're finished referring to this object.
unsafe ~PyObj()
{
CPython.Py_DecRef(obj);
}
/// Call this Python object with the given arguments.
public PyObj CallObject(PyObj args)
{
PyObj result;
unsafe
{
var rawResult = CPython.PyObject_CallObject(obj, args.obj);
if (rawResult == null) throw new NullReferenceException("Calling PyObject returned null.");
result = new PyObj(rawResult);
}
return result;
}
/// Get an attribute from this PyObject by string name.
public PyObj GetAttrString(string attrName)
{
PyObj result;
unsafe
{
var rawResult = CPython.PyObject_GetAttrString(obj, attrName);
if (rawResult == null) throw new NullReferenceException(
string.Format("Failed to get attribute `{0}` from PyObject.", attrName));
result = new PyObj(rawResult);
}
return result;
}
/// If this Python object supports [] indexing, get an item by its key.
public PyObj GetItem(PyObj key)
{
PyObj result;
unsafe
{
var rawResult = CPython.PyObject_GetItem(obj, key.obj);
if (rawResult == null) throw new NullReferenceException("Failed to get item by key.");
result = new PyObj(rawResult);
}
return result;
}
/// If this Python object is iterable, get the iterator.
public PyIter GetIter()
{
PyIter result;
unsafe
{
var rawResult = CPython.PyObject_GetIter(obj);
if (rawResult == null) throw new NullReferenceException("Failed iterate over PyObject.");
result = new PyIter(rawResult);
}
return result;
}
/// Double float representation of this PyObject.
public double ToDouble()
{
double result;
unsafe { result = CPython.PyFloat_AsDouble(obj); }
return result;
}
/// String representation of this PyObject.
public override string ToString()
{
string str;
unsafe
{
var asStr = CPython.PyObject_Str(obj);
if (asStr == null) throw new NullReferenceException("Failed to convert PyObject to string.");
str = CPython.PyUnicode_AsWideCharString(asStr, IntPtr.Zero);
}
return str;
}
/// Long integer representation of this PyObject.
public long ToLong()
{
long result;
unsafe { result = CPython.PyLong_AsLong(obj); }
return result;
}
/// Create a PyObject from a double.
public static PyObj FromDouble(double value)
{
PyObj result;
unsafe
{
var rawResult = CPython.PyFloat_FromDouble(value);
if (rawResult == null) throw new NullReferenceException("Failed to create PyObject from double.");
result = new PyObj(rawResult);
}
return result;
}
/// Create a PyObject from a long integer.
public static PyObj FromLong(long value)
{
PyObj result;
unsafe
{
var rawResult = CPython.PyLong_FromLong(value);
if (rawResult == null) throw new NullReferenceException("Failed to create PyObject from long.");
result = new PyObj(rawResult);
}
return result;
}
}
}