Skip to content

Fix Fortran array layout when decoding binary NumPy data - #651

Merged
Theelx merged 3 commits into
jsonpickle:mainfrom
AryanRG:fix-fortran-array-decode-order
Nov 16, 2026
Merged

Theelx merged 3 commits into
jsonpickle:mainfrom
AryanRG:fix-fortran-array-decode-order

Conversation

@AryanRG

@AryanRG AryanRG commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • If this PR changes any non-documentation portion of the codebase, I have updated the CHANGES.rst file for this change.
    • In the CHANGES.rst update, I added a link to the PR number that this will be (e.g. (+611)) and (if applicable) added a link to the issue that this fixes (e.g. (#608)).
  • If this PR fixes a bug or adds a feature, I have added appropriate tests.
  • I have run the tests with pytest and run formatting with ruff format.
  • If generative AI of any kind was used in creating this PR, I have followed the these guidelines:
    • I ensured that this PR is not entirely written by any generative AI model.
      • Note: Exceptions may be made to this on a case-by-case basis, however if a contributor consistently submits PRs that are entirely written by generative AI without obvious human input, then the contributor may not be allowed to contribute in the future.
    • I ensured that any portions of the code written by any generative AI model are free from copyrighted code.
    • I ensured that I included a Co-authored-by or Assisted-by tag in the footer of every commit where AI is used containing the word "LLM", without saying the name of the company/model used to avoid providing free advertising. Example: "Co-authored-by: LLM"
    • I ensured that all code in this PR submitted verbatim from generative AI output is limited to be no more than 50 lines of code added, excluding tests and documentation.
    • I understand that if I try to split large PRs generated by AI into many smaller PRs to satisfy the above requirements, I may be banned from contributing.
    • I understand that if I used AI and the above checkmarks are not all true when I submit this PR, my PR will be closed.
  • I understand that if this checklist is deleted from the PR body, my PR will be closed.

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

  • Confirmed the updated regression fails before the fix and passes afterward.
  • Ran the NumPy and pandas test modules with NumPy 1.26.4 and pandas 2.3.3. 99 passed, 2 skipped.

Additional independent validation passed with 508 tests and 2 skips including doctests. Ruff format and lint checks pass.

@AryanRG
AryanRG marked this pull request as ready for review November 16, 2026 13:43
Comment thread jsonpickle/ext/numpy.py
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh, got it! I forgot that order="F" was specifically Fortran.

Comment thread tests/numpy_test.py Outdated
a = np.asfortranarray(np.arange(100).reshape((10, 10)))
_a = roundtrip(a)
npt.assert_array_equal(a, _a)
assert _a.flags.f_contiguous

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread CHANGES.rst Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Theelx
Theelx merged commit 16f4e6f into jsonpickle:main Nov 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants