8000 Allow setting of the python module file (#2044) · pythonnet/pythonnet@8e8c3f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e8c3f3

Browse files
bmello4688filmor
andauthored
Allow setting of the python module file (#2044)
Allow the setting of the python module file in order to create a virtual package structure --------- Co-authored-by: Benedikt Reinartz <filmor@gmail.com>
1 parent c05f578 commit 8e8c3f3

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

src/embed_tests/Modules.cs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void TestEval()
5151
ps.Set("a", 1);
5252
var result = ps.Eval<int>("a + 2");
5353
Assert.AreEqual(3, result);
54-
}
54+
}
5555
}
5656

5757
/// <summary>
@@ -169,6 +169,62 @@ public void TestScopeClass()
169169
}
170170
}
171171

172+
/// <summary>
173+
/// Create a class in the scope, the class can read variables in the scope.
174+
/// Its methods can write the variables with the help of 'global' keyword.
175+
/// </summary>
176+
[Test]
177+
public void TestCreateVirtualPackageStructure()
178+
{
179+
using (Py.GIL())
180+
{
181+
using var _p1 = PyModule.FromString("test", "");
182+
// Sub-module
183+
using var _p2 = PyModule.FromString("test.scope",
184+
"class Class1():\n" +
185+
" def __init__(self, value):\n" +
186+
" self.value = value\n" +
187+
" def call(self, arg):\n" +
188+
" return self.value + bb + arg\n" + // use scope variables
189+
" def update(self, arg):\n" +
190+
" global bb\n" +
191+
" bb = self.value + arg\n", // update scope variable
192+
"test"
193+
);
194+
195+
dynamic ps2 = Py.Import("test.scope");
196+
ps2.bb = 100;
197+
198+
dynamic obj1 = ps2.Class1(20);
199+
var result = obj1.call(10).As<int>();
200+
Assert.AreEqual(130, result);
201+
202+
obj1.update(10);
203+
result = ps2.Get<int>("bb");
204+
Assert.AreEqual(30, result);
205+
}
206+
}
207+
208+
/// <summary>
209+
/// Test setting the file attribute via a FromString parameter
210+
/// </summary>
211+
[Test]
212+
public void TestCreateModuleWithFilename()
213+
{
214+
using var _gil = Py.GIL();
215+
216+
using var mod = PyModule.FromString("mod", "");
217+
using var modWithoutName = PyModule.FromString("mod_without_name", "", " ");
218+
using var modNullName = PyModule.FromString("mod_null_name", "", null);
219+
220+
using var modWithName = PyModule.FromString("mod_with_name", "", "some_filename");
221+
222+
Assert.AreEqual("none", mod.Get<string>("__file__"));
223+
Assert.AreEqual("none", modWithoutName.Get<string>("__file__"));
224+
Assert.AreEqual("none", modNullName.Get<string>("__file__"));
225+
Assert.AreEqual("some_filename", modWithName.Get<string>("__file__"));
226+
}
227+
172228
/// <summary>
173229
/// Import a python module into the session.
174230
/// Equivalent to the Python "import" statement.
@@ -194,7 +250,7 @@ public void TestImportModule()
194250
}
195251

196252
/// <summary>
197-
/// Create a scope and import variables from a scope,
253+
/// Create a scope and import variables from a scope,
198254
/// exec Python statements in the scope then discard it.
199255
/// </summary>
200256
[Test]
@@ -218,7 +274,7 @@ public void TestImportScope()
218274
}
219275

220276
/// <summary>
221-
/// Create a scope and import variables from a scope,
277+
/// Create a scope and import variables from a scope,
222278
/// exec Python statements in the scope then discard it.
223279
/// </summary>
224280
[Test]
@@ -241,7 +297,7 @@ public void TestImportAllFromScope()
241297
}
242298

243299
/// <summary>
244-
/// Create a scope and import variables from a scope,
300+
/// Create a scope and import variables from a scope,
245301
/// call the function imported.
246302
/// </summary>
247303
[Test]
@@ -286,7 +342,7 @@ public void TestImportScopeFunction()
286342
public void TestVariables()
287343
{
288344
using (Py.GIL())
289-
{
345+
{
290346
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
291347
var a0 = ps.Get<int>("ee");
292348
Assert.AreEqual(200, a0);
@@ -326,8 +382,8 @@ public void TestThread()
326382
_ps.res = 0;
327383
_ps.bb = 100;
328384
_ps.th_cnt = 0;
329-
//add function to the scope
330-
//can be call many times, more efficient than ast
385+
//add function to the scope
386+
//can be call many times, more efficient than ast
331387
ps.Exec(
332388
"import threading\n"+
333389
"lock = threading.Lock()\n"+

src/runtime/PythonTypes/PyModule.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,18 @@ public PyModule Reload()
8282

8383
public static PyModule FromString(string name, string code)
8484
{
85-
using NewReference c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
85+
return FromString(name, code, "");
86+
}
87+
88+
public static PyModule FromString(string name, string code, string file)
89+
{
90+
// Force valid value
91+
if (string.IsNullOrWhiteSpace(file))
92+
{
93+
file = "none";
94+
}
95+
96+
using NewReference c = Runtime.Py_CompileString(code, file, (int)RunFlagType.File);
8697
NewReference m = Runtime.PyImport_ExecCodeModule(name, c.BorrowOrThrow());
8798
return new PyModule(m.StealOrThrow());
8899
}

0 commit comments

Comments
 (0)
0