Skip to content

Commit 072022b

Browse files
authored
Merge branch 'master' into fix-domain-unload
2 parents 7222a2c + 05a1451 commit 072022b

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

src/runtime/importhook.cs

+31-8
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
237237
if (res != IntPtr.Zero)
238238
{
239239
// There was no error.
240+
if (fromlist && IsLoadAll(fromList))
241+
{
242+
var mod = ManagedType.GetManagedObject(res) as ModuleObject;
243+
mod?.LoadNames();
244+
}
240245
return res;
241246
}
242247
// There was an error
@@ -290,6 +295,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
290295
{
291296
if (fromlist)
292297
{
298+
if (IsLoadAll(fromList))
299+
{
300+
var mod = ManagedType.GetManagedObject(module) as ModuleObject;
301+
mod?.LoadNames();
302+
}
293303
Runtime.XIncref(module);
294304
return module;
295305
}
@@ -345,20 +355,33 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
345355
}
346356
}
347357

348-
ModuleObject mod = fromlist ? tail : head;
349-
350-
if (fromlist && Runtime.PySequence_Size(fromList) == 1)
351358
{
352-
IntPtr fp = Runtime.PySequence_GetItem(fromList, 0);
353-
if (!CLRModule.preload && Runtime.GetManagedString(fp) == "*")
359+
var mod = fromlist ? tail : head;
360+
361+
if (fromlist && IsLoadAll(fromList))
354362
{
355363
mod.LoadNames();
356364
}
357-
Runtime.XDecref(fp);
365+
366+
Runtime.XIncref(mod.pyHandle);
367+
return mod.pyHandle;
358368
}
369+
}
359370

360-
Runtime.XIncref(mod.pyHandle);
361-
return mod.pyHandle;
371+
private static bool IsLoadAll(IntPtr fromList)
372+
{
373+
if (CLRModule.preload)
374+
{
375+
return false;
376+
}
377+
if (Runtime.PySequence_Size(fromList) != 1)
378+
{
379+
return false;
380+
}
381+
IntPtr fp = Runtime.PySequence_GetItem(fromList, 0);
382+
bool res = Runtime.GetManagedString(fp) == "*";
383+
Runtime.XDecref(fp);
384+
return res;
362385
}
363386
}
364387
}

src/runtime/moduleobject.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,17 @@ public void LoadNames()
190190
foreach (string name in AssemblyManager.GetNames(_namespace))
191191
{
192192
cache.TryGetValue(name, out m);
193-
if (m == null)
193+
if (m != null)
194194
{
195-
ManagedType attr = GetAttribute(name, true);
195+
continue;
196196
}
197+
IntPtr attr = Runtime.PyDict_GetItemString(dict, name);
198+
// If __dict__ has already set a custom property, skip it.
199+
if (attr != IntPtr.Zero)
200+
{
201+
continue;
202+
}
203+
GetAttribute(name, true);
197204
}
198205
}
199206

src/tests/importtest.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import sys
4+
try:
5+
del sys.modules["System.IO"]
6+
except KeyError:
7+
pass
8+
9+
assert "FileStream" not in globals()
10+
import System.IO
11+
from System.IO import *
12+
13+
assert "FileStream" in globals()

src/tests/test_import.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
"""Test the import statement."""
44

55
import pytest
6-
6+
import sys
77

88
def test_relative_missing_import():
99
"""Test that a relative missing import doesn't crash.
1010
Some modules use this to check if a package is installed.
1111
Relative import in the site-packages folder"""
1212
with pytest.raises(ImportError):
1313
from . import _missing_import
14+
15+
16+
def test_import_all_on_second_time():
17+
"""Test import all attributes after a normal import without '*'.
18+
Due to import * only allowed at module level, the test body splited
19+
to a module file."""
20+
from . import importtest
21+
del sys.modules[importtest.__name__]
22+

0 commit comments

Comments
 (0)