Skip to content

Fix packing of 0-dim arrays - #683

Merged
FrancescAlted merged 1 commit into
Blosc:mainfrom
Zentrik:fix-pack-0dim
Jul 25, 2026
Merged

Fix packing of 0-dim arrays#683
FrancescAlted merged 1 commit into
Blosc:mainfrom
Zentrik:fix-pack-0dim

Conversation

@Zentrik

@Zentrik Zentrik commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #680.

I did use claude for this, but the change seems basically what I would have written by hand.

The empty-data guard added in e5f5275 used len(data), which NumPy
refuses on 0-dim arrays, so pack_array2/pack_tensor raised
"TypeError: len() of unsized object" for them since 3.0. Test the
buffer length instead, and fold the check into the nchunks
computation, where it is needed to avoid dividing by a chunksize of 0
for empty arrays.

Fixes Blosc#680.

Copilot AI left a comment

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.

Pull request overview

This pull request fixes a regression where packing/unpacking 0‑dim (scalar) NumPy arrays fails (e.g., TypeError: len() of unsized object), restoring correct behavior for pack_tensor / pack_array2 round-trips.

Changes:

  • Update SChunk initialization to rely on buffer length (Py_buffer.len) rather than len(data)-based gating, enabling 0‑D NumPy arrays to be packed.
  • Add a pytest case covering 0‑D scalar tensors for both pack_tensor and the pack_array2/unpack_array2 wrappers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/blosc2/blosc2_ext.pyx Removes len(data)-based check and computes chunk count from the buffer length so 0‑D NumPy arrays can be packed correctly.
tests/test_tensor.py Adds regression tests ensuring scalar (0‑D) arrays round-trip via pack_tensor/unpack_tensor and pack_array2/unpack_array2.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@FrancescAlted
FrancescAlted merged commit c600c18 into Blosc:main Jul 25, 2026
15 checks passed
@FrancescAlted

Copy link
Copy Markdown
Member

Thanks @Zentrik !

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.

pack_array2/pack_tensor reject 0-D Numpy Arrays since 3.0

3 participants