Skip to content

[WIP] Add ability to create module from C# #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions src/embed_tests/TestPyModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

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.SysModules.HasKey("testmod"));

PyModule testmod = new PyModule("testmod");

testmod.SetAttr("testattr1", "True".ToPython());

PyModule.SysModules.SetItem("testmod", testmod);

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());
}
}
}
48 changes: 48 additions & 0 deletions src/runtime/pymodule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.Text;

using Python.Runtime.Native;

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) { }
public PyModule(string name, string filename = null) : this(Create(name, filename)) { }

/// <summary>
/// Given a module or package name, import the
Expand Down Expand Up @@ -37,5 +41,49 @@ public static PyModule FromString(string name, string code)
PythonException.ThrowIfIsNull(m);
return new PyModule(ref m);
}

private static PyModule Create(string name, string filename=null)
{
if(string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}

NewReference op = Runtime.PyModule_New(name);
PythonException.ThrowIfIsNull(op);

if (filename != null)
{
BorrowedReference globals = Runtime.PyModule_GetDict(op);
PythonException.ThrowIfIsNull(globals);
int rc = Runtime.PyDict_SetItemString(globals, "__file__", filename.ToPython().Reference);
PythonException.ThrowIfIsNotZero(rc);
}

return new PyModule(ref op);
}

public void SetBuiltins(PyDict builtins)
{
if(builtins == null || builtins.IsNone())
{
throw new ArgumentNullException(nameof(builtins));
}

BorrowedReference globals = Runtime.PyModule_GetDict(this.Reference);
PythonException.ThrowIfIsNull(globals);
int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", builtins.Reference);
PythonException.ThrowIfIsNotZero(rc);
}

public static PyDict SysModules
{
get
{
BorrowedReference sysModulesRef = Runtime.PyImport_GetModuleDict();
PythonException.ThrowIfIsNull(sysModulesRef);
return new PyDict(sysModulesRef);
}
}
}
}
10 changes: 10 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,16 @@ internal static IntPtr GetBuiltins()
return PyImport_Import(PyIdentifier.builtins);
}

public static PyDict Builtins
{
get
{
BorrowedReference builtins = PyEval_GetBuiltins();
PythonException.ThrowIfIsNull(builtins);
return new PyDict(builtins);
}
}

internal static class Delegates
{
static readonly ILibraryLoader libraryLoader = LibraryLoader.Instance;
Expand Down