Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Py.Import andPyModule.Import returnPyObject instead ofPyModule#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromlosttech:bugs/ImportClrModule
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsrc/embed_tests/TestFinalizer.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/TestPyModule.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@

using System;

using NUnit.Framework;

using Python.Runtime;
Expand DownExpand 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 deletionssrc/runtime/exceptions.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,8 +87,8 @@ internal static Exception ToException(BorrowedReference ob)
/// </remarks>
internal static class Exceptions
{
internal staticPyModule warnings_module;
internal staticPyModule exceptions_module;
internal staticPyObject warnings_module;
internal staticPyObject exceptions_module;

/// <summary>
/// Initialization performed on startup of the Python runtime.
Expand Down
17 changes: 10 additions & 7 deletionssrc/runtime/pymodule.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 staticPyModule Import(string name)
public staticPyObject Import(string name)
{
NewReference op = Runtime.PyImport_ImportModule(name);
PythonException.ThrowIfIsNull(op);
return new PyModule(ref op);
returnIsModule(op) ?new PyModule(ref op) : op.MoveToPyObject();
}

/// <summary>
Expand DownExpand 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 deletionsrc/runtime/pyscope.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/runtime/pythonengine.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 staticPyModule Import(string name) => PyModule.Import(name);
public staticPyObject Import(string name) => PyModule.Import(name);

public static void SetArgv()
{
Expand Down

[8]ページ先頭

©2009-2026 Movatter.jp