-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPyModule.cs
26 lines (24 loc) · 988 Bytes
/
PyModule.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
using System;
namespace PythonBinding
{
public class PyModule : PyObj
{
unsafe internal PyModule(CPython.PyObject* obj) : base(obj) { }
/// Get a member of this module by name.
public PyObj GetMember(string attrName)
{
PyObj result;
unsafe
{
var rawDict = CPython.PyModule_GetDict(obj);
if (rawDict == null) throw new NullReferenceException("Failed to get dict from module.");
var rawString = CPython.PyUnicode_FromString(attrName);
if (rawString == null) throw new NullReferenceException("Failed to create PyObject from string.");
var rawResult = CPython.PyDict_GetItem(rawDict, rawString);
if (rawResult == null) throw new NullReferenceException("Failed to get item by key.");
result = new PyObj(rawResult);
}
return result;
}
}
}