Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit218610e

Browse files
committed
fixed positive PyInt converted to negative BigInteger
BigInteger constructor uses the sign bit in the first byte. Since we explicitly handle the sign, the fix is to prepend a zero byte to the number, which does not change it, but ensures sign bit is zero.fixes#1990
1 parent02b6c2f commit218610e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1414
###Fixed
1515

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

1819

1920
##[3.0.0](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.0) - 2022-09-29

‎src/embed_tests/TestPyInt.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,23 @@ public void ToBigInteger()
191191
{
192192
0,1,2,
193193
0x10,
194+
0x79,
195+
0x80,
196+
0x81,
197+
0xFF,
194198
0x123,
199+
0x8000,
195200
0x1234,
201+
0x8001,
202+
0x4000,
203+
0xFF,
196204
};
197205
simpleValues=simpleValues.Concat(simpleValues.Select(v=>-v)).ToArray();
198206

199-
foreach(varvalinsimpleValues)
200-
{
201-
varpyInt=newPyInt(val);
202-
Assert.AreEqual((BigInteger)val,pyInt.ToBigInteger());
203-
}
207+
varexpected=simpleValues.Select(v=>newBigInteger(v)).ToArray();
208+
varactual=simpleValues.Select(v=>newPyInt(v).ToBigInteger()).ToArray();
209+
210+
CollectionAssert.AreEqual(expected,actual);
204211
}
205212

206213
[Test]

‎src/runtime/PythonTypes/PyInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public BigInteger ToBigInteger()
212212
offset++;
213213
neg=true;
214214
}
215-
byte[]littleEndianBytes=newbyte[(hex.Length-offset+1)/2];
215+
byte[]littleEndianBytes=newbyte[(hex.Length-offset+1)/2+1];
216216
for(;offset<hex.Length;offset++)
217217
{
218218
intlittleEndianHexIndex=hex.Length-1-offset;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp