From fe0a879109f59d0b905c576213c4524b325ea9e1 Mon Sep 17 00:00:00 2001 From: 1313e Date: Thu, 17 Dec 2020 13:30:04 +1100 Subject: [PATCH] Make sure to properly handle signed and unsigned integers that cannot be stored in 64-bits. --- hickle/loaders/load_builtins.py | 2 +- hickle/tests/test_hickle.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/hickle/loaders/load_builtins.py b/hickle/loaders/load_builtins.py index d9c21b03..a4e438ad 100644 --- a/hickle/loaders/load_builtins.py +++ b/hickle/loaders/load_builtins.py @@ -76,7 +76,7 @@ def create_scalar_dataset(py_obj, h_group, name, **kwargs): kwargs.pop('compression', None) # If py_obj is an integer and cannot be stored in 64-bits, convert to str - if isinstance(py_obj, int) and (py_obj.bit_length() > 64): + if isinstance(py_obj, int) and not (-2**63 <= py_obj < 2**64): py_obj = bytes(str(py_obj), 'ascii') d = h_group.create_dataset(name, data=py_obj, **kwargs) diff --git a/hickle/tests/test_hickle.py b/hickle/tests/test_hickle.py index 5ecfb3db..695091ed 100644 --- a/hickle/tests/test_hickle.py +++ b/hickle/tests/test_hickle.py @@ -148,11 +148,16 @@ def test_65bit_int(): """ Dumping and loading an integer with arbitrary precision https://github.com/telegraphic/hickle/issues/113""" - i = 2**65-1 + i = 2**64 dump(i, 'test.hdf5') i_hkl = load('test.hdf5') assert i == i_hkl + j = -2**63-1 + dump(j, 'test.hdf5') + j_hkl = load('test.hdf5') + assert j == j_hkl + def test_list(): """ Dumping and loading a list """