FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Name missing from __all__ on re-import by filmor · Pull Request #2717 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 14 additions & 12 deletions src/runtime/Types/ModuleObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class ModuleObject : ExtensionType
internal PyDict dict;
protected string _namespace;
private readonly PyList __all__ = new ();
private readonly HashSet<string> allNames = new();

// Attributes to be set on the module according to PEP302 and 451
// by the import machinery.
Expand Down Expand Up @@ -178,22 +179,23 @@ public void LoadNames()
{
foreach (string name in AssemblyManager.GetNames(_namespace))
{
cache.TryGetValue(name, out var m);
if (m != null)
bool hasValidAttribute = cache.TryGetValue(name, out var m);
if (!hasValidAttribute)
{
continue;
}
BorrowedReference attr = Runtime.PyDict_GetItemString(dict, name);
// If __dict__ has already set a custom property, skip it.
if (!attr.IsNull)
{
continue;
BorrowedReference attr = Runtime.PyDict_GetItemString(dict, name);
// If __dict__ has already set a custom property, skip it.
if (!attr.IsNull)
{
continue;
}

using var attrVal = GetAttribute(name, true);
hasValidAttribute = !attrVal.IsNull();
}

using var attrVal = GetAttribute(name, true);
if (!attrVal.IsNull())
if (hasValidAttribute && allNames.Add(name))
{
// if it's a valid attribute, add it to __all__
// if it's a valid attribute, add it to __all__ once.
using var pyname = Runtime.PyString_FromString(name);
if (Runtime.PyList_Append(__all__, pyname.Borrow()) != 0)
{
Expand Down
5 changes: 5 additions & 0 deletions tests/test_import.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import pytest
import sys

# Unused import to preload the class
#
# This resulted in the FileStream name missing from the wildcard import later
from System.IO import FileStream # noqa: F401

def test_relative_missing_import():
"""Test that a relative missing import doesn't crash.
Some modules use this to check if a package is installed.
Expand Down
Loading

Back | FazBrowse Home | New Git URL