Visitar URL original
Allow setting of the python module file by bmello4688 · Pull Request #2044 · pythonnet/pythonnet · GitHub
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
Handle nitpicks and keep 'ABI' stable
  • Loading branch information
filmor committed Oct 16, 2023
commit 51fed4dc59957d92af72b6b917ebe1796de68863
20 changes: 10 additions & 10 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff 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 Down Expand Up @@ -186,13 +186,13 @@ public void TestCreateVirtualPackageStructure()
" def __init__(self, value):\n" +
" self.value = value\n" +
" def call(self, arg):\n" +
" return self.value + bb + arg\n" + //use scope variables
" 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
" bb = self.value + arg\n" // update scope variable
, "test"
);


dynamic _ps = Py.Import("test.scope");
_ps.bb = 100;
Expand Down Expand Up @@ -232,7 +232,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 @@ -256,7 +256,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 @@ -279,7 +279,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 Down Expand Up @@ -324,7 +324,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 @@ -364,8 +364,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
11 changes: 8 additions & 3 deletions src/runtime/PythonTypes/PyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ public PyModule Reload()
return new PyModule(op.StealOrThrow());
}

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

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