Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fdce52

Browse files
committed
Py.Import and PyModule.Import return PyObject instead of PyModule, as not everything that can be imported is PyModule
fixes #1489
1 parent ec65efe commit 0fdce52

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

src/embed_tests/TestFinalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void SimpleTestMemory()
183183
bool oldState = Finalizer.Instance.Enable;
184184
try
185185
{
186-
using (PyModule gcModule = PyModule.Import("gc"))
186+
using (PyObject gcModule = PyModule.Import("gc"))
187187
using (PyObject pyCollect = gcModule.GetAttr("collect"))
188188
{
189189
long span1 = CompareWithFinalizerOn(pyCollect, false);

src/embed_tests/TestPyModule.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
using System;
3-
41
using NUnit.Framework;
52

63
using Python.Runtime;
@@ -43,5 +40,11 @@ public void TestCreate()
4340
Assert.IsTrue(scope.TryGet("x", out dynamic x));
4441
Assert.AreEqual("True", x.ToString());
4542
}
43+
44+
[Test]
45+
public void ImportClrNamespace()
46+
{
47+
Py.Import(typeof(TestPyModule).Namespace);
48+
}
4649
}
4750
}

src/runtime/exceptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ internal static Exception ToException(BorrowedReference ob)
8787
/// </remarks>
8888
internal static class Exceptions
8989
{
90-
internal static PyModule warnings_module;
91-
internal static PyModule exceptions_module;
90+
internal static PyObject warnings_module;
91+
internal static PyObject exceptions_module;
9292

9393
/// <summary>
9494
/// Initialization performed on startup of the Python runtime.

src/runtime/pymodule.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Text;
3-
4-
using Python.Runtime.Native;
52

63
namespace Python.Runtime
74
{
@@ -12,15 +9,14 @@ public PyModule(PyObject o) : base(o.Reference, PyScopeManager.Global) { }
129
public PyModule(string name, string filename = null) : this(Create(name, filename)) { }
1310

1411
/// <summary>
15-
/// Given a module or package name, import the
16-
/// module and return the resulting module object as a <see cref="PyModule"/>.
12+
/// Given a module or package name, import the module and return the resulting object.
1713
/// </summary>
1814
/// <param name="name">Fully-qualified module or package name</param>
19-
public static PyModule Import(string name)
15+
public static PyObject Import(string name)
2016
{
2117
NewReference op = Runtime.PyImport_ImportModule(name);
2218
PythonException.ThrowIfIsNull(op);
23-
return new PyModule(ref op);
19+
return IsModule(op) ? new PyModule(ref op) : op.MoveToPyObject();
2420
}
2521

2622
/// <summary>
@@ -85,5 +81,12 @@ public static PyDict SysModules
8581
return new PyDict(sysModulesRef);
8682
}
8783
}
84+
85+
internal static bool IsModule(BorrowedReference reference)
86+
{
87+
if (reference == null) return false;
88+
BorrowedReference type = Runtime.PyObject_TYPE(reference);
89+
return Runtime.PyType_IsSubtype(type, Runtime.PyModuleType);
90+
}
8891
}
8992
}

src/runtime/pyscope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal PyScope(BorrowedReference reference, PyScopeManager manager)
5656
/// <summary>Create a scope based on a Python Module.</summary>
5757
private PyScope(IntPtr ptr, PyScopeManager manager) : base(ptr)
5858
{
59-
if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(Reference), Runtime.PyModuleType))
59+
if (!PyModule.IsModule(Reference))
6060
{
6161
throw new PyScopeException("object is not a module");
6262
}

src/runtime/pythonengine.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,10 @@ public static KeywordArguments kw(params object[] kv)
750750
}
751751

752752
/// <summary>
753-
/// Given a module or package name, import the
754-
/// module and return the resulting module object as a <see cref="PyModule"/>.
753+
/// Given a module or package name, import the module and return the resulting object.
755754
/// </summary>
756755
/// <param name="name">Fully-qualified module or package name</param>
757-
public static PyModule Import(string name) => PyModule.Import(name);
756+
public static PyObject Import(string name) => PyModule.Import(name);
758757

759758
public static void SetArgv()
760759
{

0 commit comments

Comments
 (0)