X Tutup
Skip to content

Commit 41684ba

Browse files
committed
Merge branch 'feature_2043_allow_setting_of_file'
2 parents 69f6283 + 4a5764b commit 41684ba

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/embed_tests/Modules.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,44 @@ 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+
//parent module
182+
PyModule.FromString("test", "");
183+
//sub-module
184+
PyModule.FromString("test.scope",
185+
"class Class1():\n" +
186+
" def __init__(self, value):\n" +
187+
" self.value = value\n" +
188+
" def call(self, arg):\n" +
189+
" return self.value + bb + arg\n" + //use scope variables
190+
" def update(self, arg):\n" +
191+
" global bb\n" +
192+
" bb = self.value + arg\n" //update scope variable
193+
, "test"
194+
);
195+
196+
197+
dynamic _ps = Py.Import("test.scope");
198+
_ps.bb = 100;
199+
200+
dynamic obj1 = _ps.Class1(20);
201+
var result = obj1.call(10).As<int>();
202+
Assert.AreEqual(130, result);
203+
204+
obj1.update(10);
205+
result = ps.Get<int>("bb");
206+
Assert.AreEqual(30, result);
207+
}
208+
}
209+
172210
/// <summary>
173211
/// Import a python module into the session.
174212
/// Equivalent to the Python "import" statement.

0 commit comments

Comments
 (0)
X Tutup