pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/pythonnet/pythonnet/commit/8e8c3f3c921720076b6eb7ee217a14d115240d9c

crossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-5efd63e783ac04bb.css" /> Allow setting of the python module file (#2044) · pythonnet/pythonnet@8e8c3f3 · GitHub
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

2 files changed

Lines changed: 75 additions & 8 deletions

File tree

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
//github.com/ <summary>
@@ -169,6 +169,62 @@ public void TestScopeClass()
169169
}
170170
}
171171

172+
//github.com/ <summary>
173+
//github.com/ Create a class in the scope, the class can read variables in the scope.
174+
//github.com/ Its methods can write the variables with the help of 'global' keyword.
175+
//github.com/ </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+
//github.com/ <summary>
209+
//github.com/ Test setting the file attribute via a FromString parameter
210+
//github.com/ </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
//github.com/ <summary>
173229
//github.com/ Import a python module into the session.
174230
//github.com/ Equivalent to the Python "import" statement.
@@ -194,7 +250,7 @@ public void TestImportModule()
194250
}
195251

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

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

243299
//github.com/ <summary>
244-
//github.com/ Create a scope and import variables from a scope,
300+
//github.com/ Create a scope and import variables from a scope,
245301
//github.com/ call the function imported.
246302
//github.com/ </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)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy