Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 47 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 48 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);
slide marked this conversation as resolved.
Show resolved Hide resolved
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);
}
slide marked this conversation as resolved.
Show resolved Hide resolved

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 10 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.