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
23 changes: 13 additions & 10 deletions src/core/IronPython.Modules/ResourceMetaPathImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/// <summary>
/// Implementes a resource-based meta_path importer as described in PEP 302.
/// Implements a resource-based meta_path importer as described in PEP 302.
/// </summary>
[PythonType]
public class ResourceMetaPathImporter {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -411,24 +411,27 @@ 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) {
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
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();
}
}
}
}
2 changes: 2 additions & 0 deletions tests/IronPython.Tests/IronPython.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\core\IronPython\IronPython.csproj" />
<ProjectReference Include="..\..\src\core\IronPython.Modules\IronPython.Modules.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(IsFullFramework)' == 'true' ">
Expand All @@ -26,6 +27,7 @@

<ItemGroup>
<EmbeddedResource Include="Cases\*.ini" />
<EmbeddedResource Include="ResourceMetaPathImporterTest.zip" LogicalName="ResourceMetaPathImporterTest.zip" />
</ItemGroup>

<ItemGroup>
Expand Down
59 changes: 59 additions & 0 deletions tests/IronPython.Tests/ResourceMetaPathImporterTest.cs
Original file line number Diff line number Diff line change
@@ -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<IList<object>>("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<string>("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);
}
}
}
Binary file not shown.
Loading