diff --git a/src/core/IronPython.Modules/ResourceMetaPathImporter.cs b/src/core/IronPython.Modules/ResourceMetaPathImporter.cs
index 5730f201b..73f947fbe 100644
--- a/src/core/IronPython.Modules/ResourceMetaPathImporter.cs
+++ b/src/core/IronPython.Modules/ResourceMetaPathImporter.cs
@@ -5,19 +5,19 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.IO.Compression;
using System.Linq;
using System.Reflection;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
-using IronPython.Zlib;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
namespace IronPython.Modules {
///
- /// Implementes a resource-based meta_path importer as described in PEP 302.
+ /// Implements a resource-based meta_path importer as described in PEP 302.
///
[PythonType]
public class ResourceMetaPathImporter {
@@ -112,7 +112,7 @@ public object load_module(CodeContext /*!*/ context, string fullname) {
ModuleOptions.None, out script);
var 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);
@@ -179,7 +179,7 @@ private byte[] GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, Packe
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;
}
@@ -411,17 +411,13 @@ public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string u
reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
byte[] rawData;
try {
- rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
+ rawData = reader.ReadBytes(dataSize);
} catch {
unpackingError = "Can't read data";
return false;
}
- if (compress != 0) {
- rawData[dataSize] = (byte)'Z';
- }
-
- result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
+ result = compress == 0 ? rawData : Inflate(rawData);
return true;
}
} catch (Exception exception) {
@@ -429,6 +425,13 @@ public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string u
return false;
}
}
+
+ private static byte[] Inflate(byte[] data) {
+ using var input = new DeflateStream(new MemoryStream(data), CompressionMode.Decompress);
+ using var output = new MemoryStream();
+ input.CopyTo(output);
+ return output.ToArray();
+ }
}
}
}
diff --git a/tests/IronPython.Tests/IronPython.Tests.csproj b/tests/IronPython.Tests/IronPython.Tests.csproj
index 17e5d3d40..2e8c614b4 100644
--- a/tests/IronPython.Tests/IronPython.Tests.csproj
+++ b/tests/IronPython.Tests/IronPython.Tests.csproj
@@ -11,6 +11,7 @@
+
@@ -26,6 +27,7 @@
+
diff --git a/tests/IronPython.Tests/ResourceMetaPathImporterTest.cs b/tests/IronPython.Tests/ResourceMetaPathImporterTest.cs
new file mode 100644
index 000000000..0dd0c23e9
--- /dev/null
+++ b/tests/IronPython.Tests/ResourceMetaPathImporterTest.cs
@@ -0,0 +1,59 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information.
+
+using System.Collections.Generic;
+using System.Linq;
+
+using IronPython.Hosting;
+using IronPython.Modules;
+
+using Microsoft.Scripting.Hosting;
+
+using NUnit.Framework;
+
+namespace IronPythonTest {
+ [TestFixture(Category = "IronPython")]
+ public class ResourceMetaPathImporterTest {
+ private ScriptEngine _engine;
+ private ResourceMetaPathImporter _importer;
+
+ [SetUp]
+ public void SetUp() {
+ _engine = Python.CreateEngine();
+ _importer = new ResourceMetaPathImporter(typeof(ResourceMetaPathImporterTest).Assembly, "ResourceMetaPathImporterTest.zip");
+ var metaPath = _engine.GetSysModule().GetVariable>("meta_path");
+ metaPath.Add(_importer);
+ }
+
+ private string ImportValue(string module) {
+ var scope = _engine.CreateScope();
+ _engine.Execute($"import {module} as m\nvalue = m.value", scope);
+ return scope.GetVariable("value");
+ }
+
+ [Test]
+ public void ImportDeflated() {
+ Assert.That(ImportValue("deflated"), Is.EqualTo(string.Concat(Enumerable.Repeat("deflated ", 1000))));
+ }
+
+ [Test]
+ public void ImportStored() {
+ Assert.That(ImportValue("stored"), Is.EqualTo("stored"));
+ }
+
+ [Test]
+ public void ImportPackage() {
+ Assert.That(ImportValue("pkg"), Is.EqualTo("pkg"));
+ Assert.That(ImportValue("pkg.sub"), Is.EqualTo("pkg.sub"));
+ }
+
+ [Test]
+ public void FindMissingModule() {
+ var scope = _engine.CreateScope();
+ scope.SetVariable("importer", _importer);
+ _engine.Execute("value = importer.find_module('missing')", scope);
+ Assert.That(scope.GetVariable("value"), Is.Null);
+ }
+ }
+}
diff --git a/tests/IronPython.Tests/ResourceMetaPathImporterTest.zip b/tests/IronPython.Tests/ResourceMetaPathImporterTest.zip
new file mode 100644
index 000000000..aaee92df1
Binary files /dev/null and b/tests/IronPython.Tests/ResourceMetaPathImporterTest.zip differ