Skip to content

Py.Import and PyModule.Import return PyObject instead of PyModule #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions src/embed_tests/TestPyModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

using System;

using NUnit.Framework;

using Python.Runtime;
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ internal static Exception ToException(BorrowedReference ob)
/// </remarks>
internal static class Exceptions
{
internal static PyModule warnings_module;
internal static PyModule exceptions_module;
internal static PyObject warnings_module;
internal static PyObject exceptions_module;

/// <summary>
/// Initialization performed on startup of the Python runtime.
Expand Down
17 changes: 10 additions & 7 deletions src/runtime/pymodule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Text;

using Python.Runtime.Native;

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

/// <summary>
/// Given a module or package name, import the
/// module and return the resulting module object as a <see cref="PyModule"/>.
/// Given a module or package name, import the module and return the resulting object.
/// </summary>
/// <param name="name">Fully-qualified module or package name</param>
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();
}

/// <summary>
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/runtime/pyscope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal PyScope(BorrowedReference reference, PyScopeManager manager)
/// <summary>Create a scope based on a Python Module.</summary>
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");
}
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,11 +750,10 @@ public static KeywordArguments kw(params object[] kv)
}

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

public static void SetArgv()
{
Expand Down