Skip to content

Commit ba0602a

Browse files
amos402filmor
authored andcommitted
Fix "from import *" after a normal import
1 parent 677281e commit ba0602a

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

src/runtime/importhook.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
184184
}
185185
}
186186

187+
ModuleObject mod;
187188
string mod_name = Runtime.GetManagedString(py_mod_name);
188189
// Check these BEFORE the built-in import runs; may as well
189190
// do the Incref()ed return here, since we've already found
@@ -237,6 +238,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
237238
if (res != IntPtr.Zero)
238239
{
239240
// There was no error.
241+
if (fromlist && IsLoadAll(fromList))
242+
{
243+
mod = ManagedType.GetManagedObject(res) as ModuleObject;
244+
mod?.LoadNames();
245+
}
240246
return res;
241247
}
242248
// There was an error
@@ -290,6 +296,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
290296
{
291297
if (fromlist)
292298
{
299+
if (IsLoadAll(fromList))
300+
{
301+
mod = ManagedType.GetManagedObject(module) as ModuleObject;
302+
mod?.LoadNames();
303+
}
293304
Runtime.XIncref(module);
294305
return module;
295306
}
@@ -345,20 +356,31 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
345356
}
346357
}
347358

348-
ModuleObject mod = fromlist ? tail : head;
359+
mod = fromlist ? tail : head;
349360

350-
if (fromlist && Runtime.PySequence_Size(fromList) == 1)
361+
if (fromlist && IsLoadAll(fromList))
351362
{
352-
IntPtr fp = Runtime.PySequence_GetItem(fromList, 0);
353-
if (!CLRModule.preload && Runtime.GetManagedString(fp) == "*")
354-
{
355-
mod.LoadNames();
356-
}
357-
Runtime.XDecref(fp);
363+
mod.LoadNames();
358364
}
359365

360366
Runtime.XIncref(mod.pyHandle);
361367
return mod.pyHandle;
362368
}
369+
370+
private static bool IsLoadAll(IntPtr fromList)
371+
{
372+
if (CLRModule.preload)
373+
{
374+
return false;
375+
}
376+
if (Runtime.PySequence_Size(fromList) != 1)
377+
{
378+
return false;
379+
}
380+
IntPtr fp = Runtime.PySequence_GetItem(fromList, 0);
381+
bool res = Runtime.GetManagedString(fp) == "*";
382+
Runtime.XDecref(fp);
383+
return res;
384+
}
363385
}
364386
}

src/tests/importtest.py

Lines changed: 13 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
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+
from . import importtest
18+
del sys.modules[importtest.__name__]
19+

0 commit comments

Comments
 (0)