diff --git a/src/embed_tests/TestFinalizer.cs b/src/embed_tests/TestFinalizer.cs index c040e6930..1ae5c0390 100644 --- a/src/embed_tests/TestFinalizer.cs +++ b/src/embed_tests/TestFinalizer.cs @@ -183,7 +183,7 @@ public void SimpleTestMemory() bool oldState = Finalizer.Instance.Enable; try { - using (PyModule gcModule = PyModule.Import("gc")) + using (PyObject gcModule = PyModule.Import("gc")) using (PyObject pyCollect = gcModule.GetAttr("collect")) { long span1 = CompareWithFinalizerOn(pyCollect, false); diff --git a/src/embed_tests/TestPyModule.cs b/src/embed_tests/TestPyModule.cs index e575a73a6..623f93d52 100644 --- a/src/embed_tests/TestPyModule.cs +++ b/src/embed_tests/TestPyModule.cs @@ -1,6 +1,3 @@ - -using System; - using NUnit.Framework; using Python.Runtime; @@ -43,5 +40,11 @@ public void TestCreate() Assert.IsTrue(scope.TryGet("x", out dynamic x)); Assert.AreEqual("True", x.ToString()); } + + [Test] + public void ImportClrNamespace() + { + Py.Import(typeof(TestPyModule).Namespace); + } } } diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index a612e34e3..cc8da3899 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -87,8 +87,8 @@ internal static Exception ToException(BorrowedReference ob) /// internal static class Exceptions { - internal static PyModule warnings_module; - internal static PyModule exceptions_module; + internal static PyObject warnings_module; + internal static PyObject exceptions_module; /// /// Initialization performed on startup of the Python runtime. diff --git a/src/runtime/pymodule.cs b/src/runtime/pymodule.cs index e6c50bc66..f36147ce8 100644 --- a/src/runtime/pymodule.cs +++ b/src/runtime/pymodule.cs @@ -1,7 +1,4 @@ using System; -using System.Text; - -using Python.Runtime.Native; namespace Python.Runtime { @@ -12,15 +9,14 @@ public PyModule(PyObject o) : base(o.Reference, PyScopeManager.Global) { } public PyModule(string name, string filename = null) : this(Create(name, filename)) { } /// - /// Given a module or package name, import the - /// module and return the resulting module object as a . + /// Given a module or package name, import the module and return the resulting object. /// /// Fully-qualified module or package name - public static PyModule Import(string name) + public static PyObject Import(string name) { NewReference op = Runtime.PyImport_ImportModule(name); PythonException.ThrowIfIsNull(op); - return new PyModule(ref op); + return IsModule(op) ? new PyModule(ref op) : op.MoveToPyObject(); } /// @@ -85,5 +81,12 @@ public static PyDict SysModules return new PyDict(sysModulesRef); } } + + internal static bool IsModule(BorrowedReference reference) + { + if (reference == null) return false; + BorrowedReference type = Runtime.PyObject_TYPE(reference); + return Runtime.PyType_IsSubtype(type, Runtime.PyModuleType); + } } } diff --git a/src/runtime/pyscope.cs b/src/runtime/pyscope.cs index e1b499c5c..8cb40d781 100644 --- a/src/runtime/pyscope.cs +++ b/src/runtime/pyscope.cs @@ -56,7 +56,7 @@ internal PyScope(BorrowedReference reference, PyScopeManager manager) /// Create a scope based on a Python Module. private PyScope(IntPtr ptr, PyScopeManager manager) : base(ptr) { - if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(Reference), Runtime.PyModuleType)) + if (!PyModule.IsModule(Reference)) { throw new PyScopeException("object is not a module"); } diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 13df54a5d..4b72dabd7 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -750,11 +750,10 @@ public static KeywordArguments kw(params object[] kv) } /// - /// Given a module or package name, import the - /// module and return the resulting module object as a . + /// Given a module or package name, import the module and return the resulting object. /// /// Fully-qualified module or package name - public static PyModule Import(string name) => PyModule.Import(name); + public static PyObject Import(string name) => PyModule.Import(name); public static void SetArgv() { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy