using System;using NUnit.Framework;using Python.Runtime;namespace Python.EmbeddingTest{public class TestPyWith{[OneTimeSetUp]public void SetUp(){PythonEngine.Initialize();}[OneTimeTearDown]public void Dispose(){PythonEngine.Shutdown();}/// <summary>/// Test that exception is raised in context manager that ignores it./// </summary>[Test]public void TestWithPositive(){var locals = new PyDict();PythonEngine.Exec(@"class CmTest:def __enter__(self):return selfdef __exit__(self, t, v, tb):# Exception not handled, return will be Falsepassdef fail(self):return 5 / 0a = CmTest()", null, locals);var a = locals.GetItem("a");try{Py.With(a, cmTest =>{cmTest.fail();});}catch (PythonException e){TestContext.Out.WriteLine(e.Message);Assert.IsTrue(e.Type.Name == "ZeroDivisionError");}}/// <summary>/// Test that exception is not raised in context manager that handles it/// </summary>[Test]public void TestWithNegative(){var locals = new PyDict();PythonEngine.Exec(@"class CmTest:def __enter__(self):print('Enter')return selfdef __exit__(self, t, v, tb):# Signal exception is handled by returning truereturn Truedef fail(self):return 5 / 0a = CmTest()", null, locals);var a = locals.GetItem("a");Py.With(a, cmTest =>{cmTest.fail();});}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。