public void TestImportModule() { using (Py.GIL()) { dynamic sys = ps.Import("sys"); Assert.IsTrue(ps.Contains("sys")); ps.Exec("sys.attr1 = 2"); var value1 = ps.Eval <int>("sys.attr1"); var value2 = sys.attr1.As <int>(); Assert.AreEqual(2, value1); Assert.AreEqual(2, value2); //import as ps.Import("sys", "sys1"); Assert.IsTrue(ps.Contains("sys1")); } }
public void TestPythonErrorTypeName() { try { var module = PyModule.Import("really____unknown___module"); Assert.Fail("Unknown module should not be loaded"); } catch (PythonException ex) { Assert.That(ex.PythonTypeName, Is.EqualTo("ModuleNotFoundError").Or.EqualTo("ImportError")); } }
public void TestPythonExceptionFormatNoTraceback() { try { var module = PyModule.Import("really____unknown___module"); Assert.Fail("Unknown module should not be loaded"); } catch (PythonException ex) { // ImportError/ModuleNotFoundError do not have a traceback when not running in a script Assert.AreEqual(ex.StackTrace, ex.Format()); } }
public void SimpleTestMemory() { bool oldState = Finalizer.Instance.Enable; try { using (PyObject gcModule = PyModule.Import("gc")) using (PyObject pyCollect = gcModule.GetAttr("collect")) { long span1 = CompareWithFinalizerOn(pyCollect, false); long span2 = CompareWithFinalizerOn(pyCollect, true); Assert.Less(span2, span1); } } finally { Finalizer.Instance.Enable = oldState; } }
/// <summary> /// Given a module or package name, import the /// module and return the resulting module object as a <see cref="PyModule"/>. /// </summary> /// <param name="name">Fully-qualified module or package name</param> public static PyModule Import(string name) => PyModule.Import(name);
public void TestSysArgsImportException() { var module = PyModule.Import("PyImportTest.sysargv"); Assert.IsNotNull(module); }
public void TestDottedName() { var module = PyModule.Import("PyImportTest.test.one"); Assert.IsNotNull(module); }