Skip to content
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
26 changes: 14 additions & 12 deletions src/runtime/Types/ModuleObject.cs
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
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