forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpymodule.cs
More file actions
41 lines (37 loc) · 1.45 KB
/
pymodule.cs
File metadata and controls
41 lines (37 loc) · 1.45 KB
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
using System;
namespace Python.Runtime
{
public class PyModule : PyScope
{
internal PyModule(ref NewReference reference) : base(ref reference, PyScopeManager.Global) { }
public PyModule(PyObject o) : base(o.Reference, PyScopeManager.Global) { }
/// <summary>
/// Given a module or package name, import the
/// module and return the resulting module object as a <see cref="PyModule"/>.
/// </summary>
/// <param name="name">Fully-qualified module or package name</param>
public static PyModule Import(string name)
{
NewReference op = Runtime.PyImport_ImportModule(name);
PythonException.ThrowIfIsNull(op);
return new PyModule(ref op);
}
/// <summary>
/// Reloads the module, and returns the updated object
/// </summary>
public PyModule Reload()
{
NewReference op = Runtime.PyImport_ReloadModule(this.Reference);
PythonException.ThrowIfIsNull(op);
return new PyModule(ref op);
}
public static PyModule FromString(string name, string code)
{
using NewReference c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
PythonException.ThrowIfIsNull(c);
NewReference m = Runtime.PyImport_ExecCodeModule(name, c);
PythonException.ThrowIfIsNull(m);
return new PyModule(ref m);
}
}
}