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
15 changes: 9 additions & 6 deletions src/core/IronPython.Modules/zipimport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public zipimporter(CodeContext/*!*/ context, object pathObj, [ParamDictionary] I
path = buf;
break;
}
if (pal.DirectoryExists(buf)) {
break;
}
buf = Path.GetDirectoryName(buf);
}
} catch {
Expand Down Expand Up @@ -230,7 +233,7 @@ public object load_module(CodeContext/*!*/ context, string fullname) {
ModuleOptions.None, out script);

dict = mod.__dict__;
// we do these here because we don't want CompileModule to initialize the module until we've set
// we do these here because we don't want CompileModule to initialize the module until we've set
// up some additional stuff
dict.Add("__name__", fullname);
dict.Add("__loader__", this);
Expand Down Expand Up @@ -453,7 +456,7 @@ private byte[] GetCodeFromData(CodeContext/*!*/ context, bool ispackage, bool is

if (data != null) {
if (isbytecode) {
// would put in code to unmarshal the bytecode here...
// would put in code to unmarshal the bytecode here...
} else {
code = data;
}
Expand All @@ -464,7 +467,7 @@ private byte[] GetCodeFromData(CodeContext/*!*/ context, bool ispackage, bool is
/// <summary>
/// Given a path to a Zip archive, build a dict, mapping file names
/// (local to the archive, using SEP as a separator) to toc entries.
///
///
/// A toc_entry is a tuple:
/// (__file__, # value to use for __file__, available for all files
/// compress, # compression kind; 0 for uncompressed
Expand All @@ -485,7 +488,7 @@ private PythonDictionary ReadDirectory(CodeContext context, string archive) {
BinaryReader fp = null;
int header_position, header_size, header_offset, count, compress;
int time, date, crc, data_size, name_size, file_size, file_offset;
int arc_offset; // offset from beginning of file to start of zip-archive
int arc_offset; // offset from beginning of file to start of zip-archive
PythonDictionary files = null;
byte[] endof_central_dir = new byte[22];

Expand Down Expand Up @@ -577,13 +580,13 @@ private string GetSubName(string fullname) {

/// <summary>
/// Given a (sub)modulename, write the potential file path in the
/// archive (without extension) to the path buffer.
/// archive (without extension) to the path buffer.
/// </summary>
/// <param name="prefix"></param>
/// <param name="name"></param>
/// <returns></returns>
private string MakeFilename(CodeContext context, string prefix, string name) {
// self.prefix + name [+ SEP + "__init__"] + ".py[co]"
// self.prefix + name [+ SEP + "__init__"] + ".py[co]"
if ((prefix.Length + name.Length + 13) >= MAXPATHLEN) {
throw MakeError(context, "path to long");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Hosting/PythonCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ private int RunFileWorker(string/*!*/ fileName) {
if (Importer.TryImportMainFromZip(DefaultContext.Default, fileName, out importer)) {
return 0;
}
if (importer != null && importer.GetType() != typeof(PythonImport.NullImporter)) {
if (importer != null) {
Console.WriteLine(String.Format("can't find '__main__' module in '{0}'", fileName), Style.Error);
return 0;
}
Expand Down
11 changes: 0 additions & 11 deletions src/core/IronPython/Modules/_imp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,5 @@ private static long GetLockCount(CodeContext/*!*/ context) {
private static void SetLockCount(CodeContext/*!*/ context, long lockCount) {
context.LanguageContext.SetModuleState(_lockCountKey, lockCount);
}

[PythonType]
public sealed class NullImporter {
public NullImporter(string path_string) {
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
public object find_module([NotNone] params object[] args) {
return null;
}
}
}
}
10 changes: 5 additions & 5 deletions src/core/IronPython/Runtime/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ private static object ImportTopAbsolute(CodeContext/*!*/ context, string/*!*/ na
}

if (TryLoadMetaPathModule(context, name, null, out ret)) {
// importlib's BuiltinImporter loads clr before ImportBuiltin gets a chance to
if (ret == context.LanguageContext.ClrModule) {
context.ShowCls = true;
}
return ret;
}

Expand Down Expand Up @@ -825,7 +829,7 @@ internal static bool TryImportMainFromZip(CodeContext/*!*/ context, string/*!*/
return false;
}
importCache[path] = importer = FindImporterForPath(context, path);
if (importer is null || importer is PythonImport.NullImporter) {
if (importer is null) {
return false;
}
// for consistency with cpython, insert zip as a first entry into sys.path
Expand Down Expand Up @@ -871,10 +875,6 @@ private static object FindImporterForPath(CodeContext/*!*/ context, string dirna
}
}

if (!context.LanguageContext.DomainManager.Platform.DirectoryExists(dirname)) {
return new PythonImport.NullImporter(dirname);
}

return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/dlr
15 changes: 15 additions & 0 deletions tests/suite/test_imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,8 @@ def test_load_package(self):

def test_NullImporter(self):
self.assertEqual(imp.NullImporter.__module__, 'imp')
import _imp
self.assertFalse(hasattr(_imp, 'NullImporter'))

sys.path.append('directory_that_does_not_exist')
try:
Expand All @@ -1056,6 +1058,19 @@ def test_NullImporter(self):
finally:
sys.path.remove('directory_that_does_not_exist')

@skipUnlessIronPython()
def test_import_clr_show_cls(self):
# the first import of clr goes through importlib's BuiltinImporter
clr = sys.modules.pop('clr')
try:
g = {}
exec("r1 = hasattr('abc', 'Split')\nimport clr\nr2 = hasattr('abc', 'Split')", g)
self.assertFalse(g['r1'])
self.assertTrue(g['r2'])
self.assertIs(g['clr'], clr)
finally:
sys.modules['clr'] = clr

def test_get_frozen_object(self):
# frozen objects not supported, this always fails
self.assertRaises(ImportError, imp.get_frozen_object, 'foo')
Expand Down
Loading