Skip to content

Commit e60c691

Browse files
committed
Merge commit from fork
1 parent af65c3b commit e60c691

5 files changed

Lines changed: 129 additions & 14 deletions

File tree

‎pyasn1/type/univ.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ class Pi(Real):
13621362
def __normalizeBase10(value):
13631363
m, b, e = value
13641364
while m and m % 10 == 0:
1365-
m /= 10
1365+
m //= 10
13661366
e += 1
13671367
return m, b, e
13681368

@@ -1490,10 +1490,21 @@ def __int__(self):
14901490
def __float__(self):
14911491
if self._value in self._inf:
14921492
return self._value
1493-
else:
1494-
return float(
1495-
self._value[0] * pow(self._value[1], self._value[2])
1496-
)
1493+
1494+
mantissa, base, exponent = self._value
1495+
1496+
if not mantissa:
1497+
return 0.0
1498+
1499+
if base == 2:
1500+
return math.ldexp(float(mantissa), exponent)
1501+
1502+
# base is 10 (prettyIn() rejects everything else); refuse to
1503+
# materialize astronomically large integers via pow()
1504+
if exponent > sys.float_info.max_10_exp:
1505+
raise OverflowError('Real value too large to convert to float')
1506+
1507+
return float(mantissa * pow(base, exponent))
14971508

14981509
def __abs__(self):
14991510
return self.clone(abs(float(self)))

‎tests/codec/ber/test_decoder.py‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pyasn1.type import char
2222
from pyasn1.codec import streaming
2323
from pyasn1.codec.ber import decoder
24+
from pyasn1.codec.ber import encoder
2425
from pyasn1.codec.ber import eoo
2526
from pyasn1 import error
2627

@@ -680,17 +681,51 @@ def testBin5(self): # case of 2 octs for exponent and negative exponent
680681
bytes((9, 4, 161, 255, 1, 3))
681682
) == (univ.Real((3, 2, -1020)), b'')
682683

683-
# TODO: this requires Real type comparison fix
684+
def testBin6(self): # large exponent, base = 16
685+
value, rest = decoder.decode(
686+
bytes((9, 5, 162, 0, 255, 255, 1))
687+
)
688+
689+
assert tuple(value) == (1, 2, 262140)
690+
assert rest == b''
691+
692+
def testBin7(self): # large exponent in 4-octet form, base = 16
693+
value, rest = decoder.decode(
694+
bytes((9, 7, 227, 4, 1, 35, 69, 103, 1))
695+
)
696+
697+
assert tuple(value) == (-1, 2, 76354972)
698+
assert rest == b''
699+
700+
def testLargeBinaryRoundTrip(self):
701+
substrate = encoder.encode(univ.Real((-1, 2, 76354972)))
702+
value, rest = decoder.decode(substrate)
703+
704+
assert tuple(value) == (-1, 2, 76354972)
705+
assert rest == b''
684706

685-
# def testBin6(self):
686-
# assert decoder.decode(
687-
# bytes((9, 5, 162, 0, 255, 255, 1))
688-
# ) == (univ.Real((1, 2, 262140)), b'')
707+
def testLongFormBinaryRealExponentLength(self):
708+
value, rest = decoder.decode(
709+
bytes((9, 6, 0x83, 3, 0x0f, 0x42, 0x40, 1))
710+
)
711+
712+
assert tuple(value) == (1, 2, 1000000)
713+
assert rest == b''
714+
715+
def testLargeBinaryPrettyPrintOverflow(self):
716+
value, rest = decoder.decode(
717+
b'\t\t\xeb\x060662.666\xd0B\x00\x00\x00\x00\x00\x00\x00'
718+
)
689719

690-
# def testBin7(self):
691-
# assert decoder.decode(
692-
# bytes((9, 7, 227, 4, 1, 35, 69, 103, 1))
693-
# ) == (univ.Real((-1, 2, 76354972)), b'')
720+
assert value.prettyPrint() == '<overflow>'
721+
assert rest == b'6\xd0B\x00\x00\x00\x00\x00\x00\x00'
722+
723+
try:
724+
float(value)
725+
except OverflowError:
726+
pass
727+
else:
728+
assert 0, '__float__() tolerated overflow'
694729

695730
def testPlusInf(self):
696731
assert decoder.decode(

‎tests/codec/cer/test_decoder.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pyasn1.type import opentype
1515
from pyasn1.type import univ
1616
from pyasn1.codec.cer import decoder
17+
from pyasn1.codec.cer import encoder
1718
from pyasn1.error import PyAsn1Error
1819

1920

@@ -65,6 +66,15 @@ def testLongMode(self):
6566
# TODO: test failures on short chunked and long unchunked substrate samples
6667

6768

69+
class RealDecoderTestCase(BaseTestCase):
70+
def testLargeBinaryRoundTrip(self):
71+
substrate = encoder.encode(univ.Real((-1, 2, 76354972)))
72+
value, rest = decoder.decode(substrate)
73+
74+
assert tuple(value) == (-1, 2, 76354972)
75+
assert rest == b''
76+
77+
6878
class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
6979
def setUp(self):
7080
openType = opentype.OpenType(

‎tests/codec/der/test_decoder.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pyasn1.type import opentype
1515
from pyasn1.type import univ
1616
from pyasn1.codec.der import decoder
17+
from pyasn1.codec.der import encoder
1718
from pyasn1.error import PyAsn1Error
1819

1920

@@ -71,6 +72,24 @@ def testChunkedMode(self):
7172
assert 0, 'chunked encoding tolerated'
7273

7374

75+
class RealDecoderTestCase(BaseTestCase):
76+
def testCanonicalLargeBinaryReal(self):
77+
substrate = encoder.encode(univ.Real((1, 2, 1000000)))
78+
assert substrate == bytes((9, 5, 0x82, 0x0f, 0x42, 0x40, 1))
79+
80+
value, rest = decoder.decode(substrate)
81+
82+
assert tuple(value) == (1, 2, 1000000)
83+
assert rest == b''
84+
85+
def testLargeBinaryRoundTrip(self):
86+
substrate = encoder.encode(univ.Real((-1, 2, 76354972)))
87+
value, rest = decoder.decode(substrate)
88+
89+
assert tuple(value) == (-1, 2, 76354972)
90+
assert rest == b''
91+
92+
7493
class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
7594
def setUp(self):
7695
openType = opentype.OpenType(

‎tests/type/test_univ.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,49 @@ def testLong(self):
752752
def testFloat(self):
753753
assert float(univ.Real(4.0)) == 4.0, '__float__() fails'
754754

755+
def testFloatBase10Precision(self):
756+
assert float(univ.Real((3, 10, 23))) == 3e23, '__float__() lost base-10 behavior'
757+
758+
def testFloatOverflow(self):
759+
try:
760+
float(univ.Real((1, 2, 1000000)))
761+
except OverflowError:
762+
pass
763+
else:
764+
assert 0, '__float__() tolerated overflow'
765+
766+
assert univ.Real((1, 2, 1000000)).prettyPrint() == '<overflow>'
767+
768+
def testFloatUnderflow(self):
769+
assert float(univ.Real((1, 2, -1000000))) == 0.0, '__float__() failed underflow'
770+
771+
def testFloatZeroMantissa(self):
772+
assert float(univ.Real((0, 10, 1000000000))) == 0.0, '__float__() failed zero mantissa'
773+
assert float(univ.Real((0, 2, 1000000000))) == 0.0, '__float__() failed zero mantissa'
774+
775+
def testFloatBase10Overflow(self):
776+
try:
777+
float(univ.Real((1, 10, sys.float_info.max_10_exp + 1)))
778+
except OverflowError:
779+
pass
780+
else:
781+
assert 0, '__float__() tolerated base-10 overflow'
782+
783+
def testFloatBase10NormalizedOverflow(self):
784+
try:
785+
float(univ.Real((10, 10, sys.float_info.max_10_exp)))
786+
except OverflowError:
787+
pass
788+
else:
789+
assert 0, '__float__() tolerated normalized base-10 overflow'
790+
755791
def testPrettyIn(self):
756792
assert univ.Real((3, 10, 0)) == 3, 'prettyIn() fails'
757793

794+
def testPrettyInBigBase10Mantissa(self):
795+
assert tuple(univ.Real((10 ** 400, 10, 0))) == (1, 10, 400), \
796+
'prettyIn() big mantissa normalization fails'
797+
758798
# infinite float values
759799
def testStrInf(self):
760800
assert str(univ.Real('inf')) == 'inf', 'str() fails'

0 commit comments

Comments
 (0)