Allow setting of the python module file by bmello4688 · Pull Request #2044 · pythonnet/pythonnet
/// <summary>
/// <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.
/// <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]
/// <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]
/// <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]