Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Changes based on feedback
  • Loading branch information
slide committed Jul 27, 2021
commit f872b3d61296e4042666ef0442a44e2c9df5dfea
18 changes: 6 additions & 12 deletions src/embed_tests/TestPyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Python.EmbeddingTest
{
class TestPyModule
public class TestPyModule
{
[OneTimeSetUp]
public void SetUp()
Expand All @@ -25,9 +25,14 @@ public void Dispose()
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"
Expand All @@ -37,16 +42,5 @@ public void TestCreate()
Assert.IsTrue(scope.TryGet("x", out dynamic x));
Assert.AreEqual("True", x.ToString());
}

[Test]
public void TestCreateExisting()
{
using PyScope scope = Py.CreateScope();
PyModule sysmod = PyModule.Create("sys");
sysmod.SetAttr("testattr1", "Hello, Python".ToPython());

dynamic sys = Py.Import("sys");
Assert.AreEqual("Hello, Python", sys.testattr1.ToString());
}
}
}
46 changes: 26 additions & 20 deletions src/runtime/pymodule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,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);
}

public static PyModule FromString(string name, string code)
{
using NewReference c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
Expand All @@ -41,39 +48,38 @@ public static PyModule FromString(string name, string code)
return new PyModule(ref m);
}

public static PyModule Create(string name, string filename = "none")
public static bool IsInSysModules(string name)
{
NewReference op;

// first check if there is an existing module
BorrowedReference modules = Runtime.PyImport_GetModuleDict();
BorrowedReference m = Runtime.PyDict_GetItemString(modules, name);
if(!m.IsNull && Runtime.PyObject_TypeCheck(m, new BorrowedReference(Runtime.PyModuleType)))
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);
PythonException.ThrowIfIsNull(op);

if (filename != null)
{
// there is already a module by this name
op = new NewReference(m);
return new PyModule(ref op);
BorrowedReference globals = Runtime.PyModule_GetDict(new BorrowedReference(op.DangerousGetAddress()));
PythonException.ThrowIfIsNull(globals);
int rc = Runtime.PyDict_SetItemString(globals, "__file__", new BorrowedReference(filename.ToPython().Handle));
PythonException.ThrowIfIsNotZero(rc);
}

// create a new module
op = Runtime.PyModule_New(name);
PythonException.ThrowIfIsNull(op);
return new PyModule(ref op);
}

// setup the module basics (__builtins__ and __file__)
BorrowedReference globals = Runtime.PyModule_GetDict(new BorrowedReference(op.DangerousGetAddress()));
public void AddBuiltins()
{
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);
rc = Runtime.PyDict_SetItemString(globals, "__file__", new BorrowedReference(filename.ToPython().Handle));
PythonException.ThrowIfIsNotZero(rc);

// add to sys.modules
rc = Runtime.PyDict_SetItemString(modules, name, op);
PythonException.ThrowIfIsNotZero(rc);

return new PyModule(ref op);
}
}
}