Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/embed_tests/TestPyModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

using System;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestPyModule
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestCreate()
{
using PyScope scope = Py.CreateScope();

Assert.IsFalse(PyModule.IsInSysModules("testmod"));

PyModule testmod = PyModule.Create("testmod");
testmod.SetAttr("testattr1", "True".ToPython());

testmod.AddToSysModules();

using PyObject code = PythonEngine.Compile(
"import testmod\n" +
"x = testmod.testattr1"
);
scope.Execute(code);

Assert.IsTrue(scope.TryGet("x", out dynamic x));
Assert.AreEqual("True", x.ToString());
}
}
}
44 changes: 44 additions & 0 deletions src/runtime/pymodule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Text;

using Python.Runtime.Native;

namespace Python.Runtime
{
Expand Down Expand Up @@ -29,6 +32,13 @@ public PyModule Reload()
return new PyModule(ref op);
}

public void AddToSysModules()
{
BorrowedReference modules = Runtime.PyImport_GetModuleDict();
int rc = Runtime.PyDict_SetItemString(modules, this.Name, this.Reference);
PythonException.ThrowIfIsNotZero(rc);
}

Comment thread
slide marked this conversation as resolved.
Outdated
public static PyModule FromString(string name, string code)
{
using NewReference c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
Expand All @@ -37,5 +47,39 @@ public static PyModule FromString(string name, string code)
PythonException.ThrowIfIsNull(m);
return new PyModule(ref m);
}

public static bool IsInSysModules(string name)
{
// first check if there is an existing module
BorrowedReference modules = Runtime.PyImport_GetModuleDict();
BorrowedReference m = Runtime.PyDict_GetItemString(modules, name);
return !m.IsNull && Runtime.PyObject_TypeCheck(m, new BorrowedReference(Runtime.PyModuleType));
}

public static PyModule Create(string name, string filename=null)
{
NewReference op = Runtime.PyModule_New(name);
Comment thread
slide marked this conversation as resolved.
PythonException.ThrowIfIsNull(op);

if (filename != null)
{
BorrowedReference globals = Runtime.PyModule_GetDict(new BorrowedReference(op.DangerousGetAddress()));
Comment thread
slide marked this conversation as resolved.
Outdated
PythonException.ThrowIfIsNull(globals);
int rc = Runtime.PyDict_SetItemString(globals, "__file__", new BorrowedReference(filename.ToPython().Handle));
Comment thread
slide marked this conversation as resolved.
Outdated
PythonException.ThrowIfIsNotZero(rc);
}

return new PyModule(ref op);
}
Comment thread
slide marked this conversation as resolved.

public void AddBuiltins()
Comment thread
slide marked this conversation as resolved.
Outdated
{
BorrowedReference globals = Runtime.PyModule_GetDict(this.Reference);
PythonException.ThrowIfIsNull(globals);
BorrowedReference __builtins__ = Runtime.PyEval_GetBuiltins();
PythonException.ThrowIfIsNull(__builtins__);
int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", __builtins__);
PythonException.ThrowIfIsNotZero(rc);
}
}
}