Closed philipstarkey closed 5 years ago
Original comment by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
The bug report for Python is here:
https://bugs.python.org/issue24313
The cause is that numpy integers do not inherit from Python integers on Python 3, because Python integers no longer have a fixed size. In Python 2 there were ints and long ints, and the matching numpy dtypes inherited from them. But no such thing is possible in Python 3. Some cooperation between numpy and Python could make it work, but the bug report is old and there is a decent workaround, so I suspect no fix is coming any time soon.
The workaround is this:
def default(o):
if isinstance(o, np.integer): return int(o)
raise TypeError
import json
import numpy as np
json.dumps([np.int64(0)], default=default)
I'll make the change to labscript_utils.properties
.
Original comment by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
Fixed by PR #82
Original comment by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
Merged in cbillington/labscript_utils/bugfix (pull request #82)
Fix issue #24: can't save integer connection table properties that are globals
β \<\<cset ec867a32c1564e493b36cc97f358d8c94637293a>>
Original comment by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
Fix issue #24: can't save integer connection table properties that are globals
Applied workaround from Python bug report: https://bugs.python.org/issue24313
β \<\<cset ac59cf45ee457d2cda8e787367134e59ba4276dc>>
Original comment by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
Merged in cbillington/labscript_utils/bugfix (pull request #82)
Fix issue #24: can't save integer connection table properties that are globals
β \<\<cset ec867a32c1564e493b36cc97f358d8c94637293a>>
Original report (archived issue) by Chris Billington (Bitbucket: cbillington, GitHub: chrisjbillington).
In Python 3.7 with latest numpy (observed on numpy 1.16.3), one cannot save a connection table attribute or unit calibration parameter that is an integer, and that has been through a HDF5 file as a global. JSON serialisation chokes on the integer, saying it can't serialise it. Here is a minimal breaking example:
Of course JSON can serialise normal Python integers, but having been through the HDF5 file, the integer became a np.int32. So an even more minimal breaking example might be:
And it doesn't matter if it is a np.int32 or np.int64, both break .
This works fine in Python 2 with the same numpy version and works if you convert the integer to a float instead. Looks like a regression in either Python or numpy, I'm not sure which. But I'll see if I can figure out which to report a bug to. We could work around it in labscript suite code, but should not bother if it is to be imminently fixed upstream.