Skip to content

Fix positive PyInt converted to negative BigInteger #1993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Fixed

- Fixed objects leaking when Python attached event handlers to them even if they were later removed
- Fixed `PyInt` conversion to `BigInteger` and `System.String` produced incorrect result for values between 128 and 255.


## [3.0.0](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.0) - 2022-09-29
Expand Down
17 changes: 12 additions & 5 deletions src/embed_tests/TestPyInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,23 @@ public void ToBigInteger()
{
0, 1, 2,
0x10,
0x79,
0x80,
0x81,
0xFF,
0x123,
0x8000,
0x1234,
0x8001,
0x4000,
0xFF,
};
simpleValues = simpleValues.Concat(simpleValues.Select(v => -v)).ToArray();

foreach (var val in simpleValues)
{
var pyInt = new PyInt(val);
Assert.AreEqual((BigInteger)val, pyInt.ToBigInteger());
}
var expected = simpleValues.Select(v => new BigInteger(v)).ToArray();
var actual = simpleValues.Select(v => new PyInt(v).ToBigInteger()).ToArray();

CollectionAssert.AreEqual(expected, actual);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/PythonTypes/PyInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public BigInteger ToBigInteger()
offset++;
neg = true;
}
byte[] littleEndianBytes = new byte[(hex.Length - offset + 1) / 2];
byte[] littleEndianBytes = new byte[(hex.Length - offset + 1) / 2 + 1];
for (; offset < hex.Length; offset++)
{
int littleEndianHexIndex = hex.Length - 1 - offset;
Expand Down