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.
This commit is contained in:
Paul Sokolovsky 2017-03-06 16:23:09 +01:00
parent 325c4473a5
commit 121fb88988
3 changed files with 62 additions and 36 deletions

View File

@ -5,21 +5,31 @@ try:
except: except:
import struct import struct
import sys
maxsize_bits = 0
maxsize = sys.maxsize
while maxsize:
maxsize >>= 1
maxsize_bits += 1
# work out configuration values # work out configuration values
is_64bit = struct.calcsize("P") == 8 is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz # 0 = none, 1 = long long, 2 = mpz
try: ll_type = None
dummy = 0x7fffffffffffffff if is_64bit:
try: if maxsize_bits < 63:
if (0xffffffffffffffff + 1) > 0: ll_type = 0
ll_type = 2 else:
else: if maxsize_bits < 31:
ll_type = 1 ll_type = 0
except: if ll_type is None:
# in case the sum in the if statement above changes to raising an exception on overflow one = 1
if one << 65 < one << 62:
ll_type = 1 ll_type = 1
except: else:
ll_type = 0 ll_type = 2
# basic conversion # basic conversion
print(int(14187745.)) print(int(14187745.))

View File

@ -5,21 +5,29 @@ try:
except: except:
import struct import struct
import sys
maxsize_bits = 0
maxsize = sys.maxsize
while maxsize:
maxsize >>= 1
maxsize_bits += 1
# work out configuration values # work out configuration values
is_64bit = struct.calcsize("P") == 8 is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz # 0 = none, 1 = long long, 2 = mpz
try: ll_type = None
dummy = 0x7fffffffffffffff if is_64bit:
try: if maxsize_bits < 63:
if (0xffffffffffffffff + 1) > 0: ll_type = 0
ll_type = 2 else:
else: if maxsize_bits < 31:
ll_type = 1 ll_type = 0
except: if ll_type is None:
# in case the sum in the if statement above changes to raising an exception on overflow one = 1
if one << 65 < one << 62:
ll_type = 1 ll_type = 1
except: else:
ll_type = 0 ll_type = 2
# This case occurs with time.time() values # This case occurs with time.time() values
if ll_type != 0: if ll_type != 0:

View File

@ -5,21 +5,29 @@ try:
except: except:
import struct import struct
import sys
maxsize_bits = 0
maxsize = sys.maxsize
while maxsize:
maxsize >>= 1
maxsize_bits += 1
# work out configuration values # work out configuration values
is_64bit = struct.calcsize("P") == 8 is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz # 0 = none, 1 = long long, 2 = mpz
try: ll_type = None
dummy = 0x7fffffffffffffff if is_64bit:
try: if maxsize_bits < 63:
if (0xffffffffffffffff + 1) > 0: ll_type = 0
ll_type = 2 else:
else: if maxsize_bits < 31:
ll_type = 1 ll_type = 0
except: if ll_type is None:
# in case the sum in the if statement above changes to raising an exception on overflow one = 1
if one << 65 < one << 62:
ll_type = 1 ll_type = 1
except: else:
ll_type = 0 ll_type = 2
# basic conversion # basic conversion
print(int(14187744.)) print(int(14187744.))