Visitar URL original
Allow setting of the python module file by bmello4688 · Pull Request #2044 · pythonnet/pythonnet · GitHub
Skip to content
Merged
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
add test
  • Loading branch information
bmello4688 authored and filmor committed Oct 16, 2023
commit c03d024a9623a8ed0e1978ed88c74d8fa840a6b9
35 changes: 35 additions & 0 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,41 @@ 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())
{
PyModule.FromString("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 _ps = Py.Import("test.scope");
_ps.bb = 100;

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

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

/// <summary>
/// Import a python module into the session.
/// Equivalent to the Python "import" statement.
Expand Down