Repository navigation
Fix Fortran array layout when decoding binary NumPy data - #651
Conversation
Assisted-by: LLM
Assisted-by: LLM
| shape=data.get("shape"), # type: ignore[arg-type] | ||
| order=data.get("order", "C"), | ||
| ).copy() # make a copy, to force the result to own the data | ||
| ).copy(order="A") # copy to own the data, keeping its memory order |
There was a problem hiding this comment.
This doesn't make sense. The order from the previous step is gotten from data.get("order", "C") which allows for C or Fortran ordering, however this change would make it unconditionally take Fortran ordering, which seems like it would break the vastly more common case of C ordered arrays.
There was a problem hiding this comment.
order="A" keeps Fortran order for a Fortran-contiguous array and C order otherwise. I've added the C-order case to the same test and moved the changelog entry to the end of 5.0.0. The suite and doctests pass with 509 passed and 2 skipped.
There was a problem hiding this comment.
Ahhhh, got it! I forgot that order="F" was specifically Fortran.
| a = np.asfortranarray(np.arange(100).reshape((10, 10))) | ||
| _a = roundtrip(a) | ||
| npt.assert_array_equal(a, _a) | ||
| assert _a.flags.f_contiguous |
There was a problem hiding this comment.
If the test suite passes when C ordered arrays are unconditionally converted to Fortran-ordered arrays, then we'll need to add a test that's analogous to this to ensure that C base arrays decode properly too.
| now encoded as a reference to that global, so decoding returns the same object. (+645) | ||
| * Fix bug where compiled regex objects lost their flags (e.g. ``re.IGNORECASE``) | ||
| when roundtripped through encode/decode. (+607) | ||
| * Fix bug where a Fortran-ordered numpy array stored as binary was decoded in C |
There was a problem hiding this comment.
Since this is PR 651 and that's not between 607 and 608/616, please put this change entry at the end of the 5.0.0 changes.
Assisted-by: LLM
Thank you for contributing to jsonpickle! This is just a quick template for you to fill out to make sure that jsonpickle can stay insulated from any legal uncertainty regarding copyrightability of code made by generative AI, and to make sure that we keep the code base easily maintainable.
In order for us to merge your PR, please confirm that all of these boxes are true when applied to this PR and check them if so. If any box is not applicable to your PR (e.g. if you made a documentation-only change), you should just check it instead of deleting it or leaving it un-checked.
CHANGES.rstfile for this change.(+611)) and (if applicable) added a link to the issue that this fixes (e.g.(#608)).pytestand run formatting withruff format.Fortran-ordered arrays stored as binary lose their memory layout during decoding. The array is reconstructed with the saved order, but the following .copy() converts it to C order.
This causes incorrect values when decoding a base array together with a view such as [a, a[:, 1:3]]. The view is rebuilt using its original offset and strides against the decoded base, whose memory layout has changed.
Changed the copy to .copy(order="A") so the decoded array owns its data and preserves its memory order.
Extended test_fortran_base to check that the decoded array remains Fortran-contiguous and that a slice stored alongside it round-trips with the correct values. Added a changelog entry.
Testing
Additional independent validation passed with 508 tests and 2 skips including doctests. Ruff format and lint checks pass.