Skip to content

Commit

Permalink
fix empty bytes object construction
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed May 15, 2023
1 parent d677540 commit badc8fe
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 70 deletions.
4 changes: 2 additions & 2 deletions dotnet/Razorvine.Pickle/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.11.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.5" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions dotnet/Razorvine.Pickle/Pickle/Pickle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<Authors>Irmen de Jong</Authors>
<Product>Python pickle serialization protocol library for .NET</Product>
<PackageId>Razorvine.Pickle</PackageId>
<Version>1.3.0</Version>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<Version>1.4.0</Version>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>Pickle.snk</AssemblyOriginatorKeyFile>
Expand All @@ -25,6 +25,6 @@
<PackageReleaseNotes>Stand alone pickle library.</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public class ByteArrayConstructor : IObjectConstructor {
public object construct(object[] args) {
// args for bytearray constructor: [ String string, String encoding ]
// args for bytearray constructor (from python3 bytes): [ ArrayList ] or just [byte[]] (when it uses BINBYTES opcode)
if (args.Length != 1 && args.Length != 2)
// or, zero arguments: empty bytearray.
if (args.Length>2)
throw new PickleException("invalid pickle data for bytearray; expected 1 or 2 args, got "+args.Length);

if (args.Length == 0)
return new byte[]{};

if(args.Length==1) {
if(args[0] is byte[]) {
return args[0];
Expand Down
9 changes: 9 additions & 0 deletions dotnet/Razorvine.Pickle/UnitTests/Pickle/UnpicklerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ public void TestBytesAndByteArray()
Assert.Equal(Encoding.ASCII.GetBytes("ABCDEFGH"), (byte[])U(p3));
}

[Fact]
public void TestUnpickleEmptyBytes() {
byte[] pickled = {
128, 2, 99, 95, 95, 98, 117, 105, 108, 116, 105, 110, 95, 95, 10, 98, 121, 116, 101, 115, 10, 113, 0, 41, 82, 113, 1, 46
};
byte[] obj = (byte[]) (new Unpickler().loads(pickled));
Assert.Empty(obj);
}

[Fact]
public void TestArray()
{
Expand Down
6 changes: 3 additions & 3 deletions dotnet/Razorvine.Pickle/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions java/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions java/.idea/libraries/Maven__junit_junit_4_13_1.xml

This file was deleted.

13 changes: 0 additions & 13 deletions java/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

This file was deleted.

3 changes: 2 additions & 1 deletion java/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions java/.idea/modules.xml

This file was deleted.

17 changes: 0 additions & 17 deletions java/pickle.iml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public class ByteArrayConstructor implements IObjectConstructor {
public Object construct(Object[] args) throws PickleException {
// args for bytearray constructor: [ String string, String encoding ]
// args for bytearray constructor (from python3 bytes): [ ArrayList<Number> ] or just [byte[]] (when it uses BINBYTES opcode)
if (args.length != 1 && args.length != 2)
throw new PickleException("invalid pickle data for bytearray; expected 1 or 2 args, got "+args.length);
// or, zero arguments: empty bytearray.
if (args.length>2)
throw new PickleException("invalid pickle data for bytearray; expected 0, 1 or 2 args, got "+args.length);

if(args.length==0)
return new byte[0];

if(args.length==1) {
if(args[0] instanceof byte[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Object constructors and other utility classes for the pickle package.
*
* @author Irmen de Jong (irmen@razorvine.net)
* @version 1.2
* @version 1.4
* @see net.razorvine.pickle
*/
package net.razorvine.pickle.objects;
2 changes: 1 addition & 1 deletion java/src/main/java/net/razorvine/pickle/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* functionality.
*
* @author Irmen de Jong (irmen@razorvine.net)
* @version 1.2
* @version 1.4
*/
package net.razorvine.pickle;

Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ public void testBytesAndByteArray() throws IOException
assertArrayEquals(new byte[]{'A','B','C','D','E','F','G','H'}, (byte[])U(p3));
}

@Test
public void testUnpickleEmptyBytes() throws IOException {
byte[] pickled = new byte[] {
(byte)128, 2, 99, 95, 95, 98, 117, 105, 108, 116, 105, 110, 95, 95, 10, 98, 121, 116, 101, 115, 10, 113, 0, 41, 82, 113, 1, 46
};
byte[] obj = (byte[]) (new Unpickler().loads(pickled));
assertEquals(0, obj.length);
}

@Test
public void testArray() throws PickleException, IOException
{
Expand Down

0 comments on commit badc8fe

Please sign in to comment.