◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
70 changes: 63 additions & 7 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ public void TestEval()
ps.Set("a", 1);
var result = ps.Eval<int>("a + 2");
Assert.AreEqual(3, result);
}
}

/// <summary>
Expand Down Expand Up @@ -169,6 +169,62 @@ public void TestScopeClass()
}
}

/// <summary>
/// Import a python module into the session.
/// Equivalent to the Python "import" statement.
Expand All @@ -194,7 +250,7 @@ public void TestImportModule()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
Expand All @@ -218,7 +274,7 @@ public void TestImportScope()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
Expand All @@ -241,7 +297,7 @@ public void TestImportAllFromScope()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// call the function imported.
/// </summary>
[Test]
Expand Down Expand Up @@ -286,7 +342,7 @@ public void TestImportScopeFunction()
public void TestVariables()
{
using (Py.GIL())
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);
Expand Down Expand Up @@ -326,8 +382,8 @@ public void TestThread()
_ps.res = 0;
_ps.bb = 100;
_ps.th_cnt = 0;
//add function to the scope
//can be call many times, more efficient than ast
ps.Exec(
"import threading\n"+
"lock = threading.Lock()\n"+
Expand Down
13 changes: 12 additions & 1 deletion src/runtime/PythonTypes/PyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ public PyModule Reload()

public static PyModule FromString(string name, string code)
{
using NewReference c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
NewReference m = Runtime.PyImport_ExecCodeModule(name, c.BorrowOrThrow());
return new PyModule(m.StealOrThrow());
}
Expand Down
Toggle all file notes Toggle all file annotations