Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Allow setting of the python module file#2044

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
70 changes: 63 additions & 7 deletionssrc/embed_tests/Modules.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,7 +51,7 @@ public void TestEval()
ps.Set("a", 1);
var result = ps.Eval<int>("a + 2");
Assert.AreEqual(3, result);
}
}
}

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

/// <summary>
/// Create a class in the scope, the class can read variables in the scope.
/// Its methods can write the variables with the help of 'global' keyword.
/// </summary>
[Test]
public void TestCreateVirtualPackageStructure()
{
using (Py.GIL())
{
using var _p1 = PyModule.FromString("test", "");
// Sub-module
using var _p2 = PyModule.FromString("test.scope",
"class Class1():\n" +
" def __init__(self, value):\n" +
" self.value = value\n" +
" def call(self, arg):\n" +
" return self.value + bb + arg\n" + // use scope variables
" def update(self, arg):\n" +
" global bb\n" +
" bb = self.value + arg\n", // update scope variable
"test"
);

dynamic ps2 = Py.Import("test.scope");
ps2.bb = 100;

dynamic obj1 = ps2.Class1(20);
var result = obj1.call(10).As<int>();
Assert.AreEqual(130, result);

obj1.update(10);
result = ps2.Get<int>("bb");
Assert.AreEqual(30, result);
}
}

/// <summary>
/// Test setting the file attribute via a FromString parameter
/// </summary>
[Test]
public void TestCreateModuleWithFilename()
{
using var _gil = Py.GIL();

using var mod = PyModule.FromString("mod", "");
using var modWithoutName = PyModule.FromString("mod_without_name", "", " ");
using var modNullName = PyModule.FromString("mod_null_name", "", null);

using var modWithName = PyModule.FromString("mod_with_name", "", "some_filename");

Assert.AreEqual("none", mod.Get<string>("__file__"));
Assert.AreEqual("none", modWithoutName.Get<string>("__file__"));
Assert.AreEqual("none", modNullName.Get<string>("__file__"));
Assert.AreEqual("some_filename", modWithName.Get<string>("__file__"));
}

/// <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,
/// 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,
/// 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,
/// Create a scope and import variables from a scope,
/// call the function imported.
/// </summary>
[Test]
Expand DownExpand 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 DownExpand 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
//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 deletionsrc/runtime/PythonTypes/PyModule.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
return FromString(name, code, "");
}

public static PyModule FromString(string name, string code, string file)
{
// Force valid value
if (string.IsNullOrWhiteSpace(file))
{
file = "none";
}

using NewReference c = Runtime.Py_CompileString(code, file, (int)RunFlagType.File);
NewReference m = Runtime.PyImport_ExecCodeModule(name, c.BorrowOrThrow());
return new PyModule(m.StealOrThrow());
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp