Skip to content

Commit 121fb88

Browse files
committed
float/float2int*: Make actually be parsable for MICROPY_LONGINT_IMPL_NONE.
The use of large literal numbers is a big no-no when it comes to writing programs which work with different int representations. Also, some checks are pretty adhoc (e.g using struct module to check for 64-bitness). This change bases entire detection on sys.maxsize and integer operarions, and thus more correct, even if longer. Note that this change doesn't mean that any of these tests can pass with anything but MPZ - even despite checking for various int representations, the tests aren't written to be portable among them.
1 parent 325c447 commit 121fb88

3 files changed

Lines changed: 62 additions & 36 deletions

File tree

tests/float/float2int.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@
55
except:
66
import struct
77

8+
import sys
9+
10+
maxsize_bits = 0
11+
maxsize = sys.maxsize
12+
while maxsize:
13+
maxsize >>= 1
14+
maxsize_bits += 1
15+
816
# work out configuration values
9-
is_64bit = struct.calcsize("P") == 8
17+
is_64bit = maxsize_bits > 32
1018
# 0 = none, 1 = long long, 2 = mpz
11-
try:
12-
dummy = 0x7fffffffffffffff
13-
try:
14-
if (0xffffffffffffffff + 1) > 0:
15-
ll_type = 2
16-
else:
17-
ll_type = 1
18-
except:
19-
# in case the sum in the if statement above changes to raising an exception on overflow
19+
ll_type = None
20+
if is_64bit:
21+
if maxsize_bits < 63:
22+
ll_type = 0
23+
else:
24+
if maxsize_bits < 31:
25+
ll_type = 0
26+
if ll_type is None:
27+
one = 1
28+
if one << 65 < one << 62:
2029
ll_type = 1
21-
except:
22-
ll_type = 0
30+
else:
31+
ll_type = 2
32+
2333

2434
# basic conversion
2535
print(int(14187745.))

tests/float/float2int_doubleprec.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
except:
66
import struct
77

8+
import sys
9+
maxsize_bits = 0
10+
maxsize = sys.maxsize
11+
while maxsize:
12+
maxsize >>= 1
13+
maxsize_bits += 1
14+
815
# work out configuration values
9-
is_64bit = struct.calcsize("P") == 8
16+
is_64bit = maxsize_bits > 32
1017
# 0 = none, 1 = long long, 2 = mpz
11-
try:
12-
dummy = 0x7fffffffffffffff
13-
try:
14-
if (0xffffffffffffffff + 1) > 0:
15-
ll_type = 2
16-
else:
17-
ll_type = 1
18-
except:
19-
# in case the sum in the if statement above changes to raising an exception on overflow
18+
ll_type = None
19+
if is_64bit:
20+
if maxsize_bits < 63:
21+
ll_type = 0
22+
else:
23+
if maxsize_bits < 31:
24+
ll_type = 0
25+
if ll_type is None:
26+
one = 1
27+
if one << 65 < one << 62:
2028
ll_type = 1
21-
except:
22-
ll_type = 0
29+
else:
30+
ll_type = 2
2331

2432
# This case occurs with time.time() values
2533
if ll_type != 0:

tests/float/float2int_fp30.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
except:
66
import struct
77

8+
import sys
9+
maxsize_bits = 0
10+
maxsize = sys.maxsize
11+
while maxsize:
12+
maxsize >>= 1
13+
maxsize_bits += 1
14+
815
# work out configuration values
9-
is_64bit = struct.calcsize("P") == 8
16+
is_64bit = maxsize_bits > 32
1017
# 0 = none, 1 = long long, 2 = mpz
11-
try:
12-
dummy = 0x7fffffffffffffff
13-
try:
14-
if (0xffffffffffffffff + 1) > 0:
15-
ll_type = 2
16-
else:
17-
ll_type = 1
18-
except:
19-
# in case the sum in the if statement above changes to raising an exception on overflow
18+
ll_type = None
19+
if is_64bit:
20+
if maxsize_bits < 63:
21+
ll_type = 0
22+
else:
23+
if maxsize_bits < 31:
24+
ll_type = 0
25+
if ll_type is None:
26+
one = 1
27+
if one << 65 < one << 62:
2028
ll_type = 1
21-
except:
22-
ll_type = 0
29+
else:
30+
ll_type = 2
2331

2432
# basic conversion
2533
print(int(14187744.))

0 commit comments

Comments
 (0)