Skip to content

Commit efbba8a

Browse files
committed
Warn on implicit blosc2.open append mode
Implement the first phase of plans/changing-default-open-mode.md by tracking omitted mode= with a sentinel and emitting a FutureWarning when blosc2.open() relies on the current implicit "a" behavior. Update mmap-related tests, examples, and docstrings to pass explicit mode="r" so they keep exercising their intended paths without tripping the migration warning.
1 parent 2cb4295 commit efbba8a

31 files changed

Lines changed: 259 additions & 204 deletions

examples/mmap-rw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
blosc2.asarray(a, urlpath=urlpath, mmap_mode="w+", initial_mapping_size=initial_mapping_size)
2525

2626
# Read the ndarray back via the general open function
27-
a_read = blosc2.open(urlpath, mmap_mode="r")
27+
a_read = blosc2.open(urlpath, mode="r", mmap_mode="r")
2828

2929
assert np.all(a == a_read)
3030
blosc2.remove_urlpath(urlpath)

examples/ndarray/blosc2_3_10_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# Reopen persistent expression, compute, and write to disk with blosc2
3535
t0 = time.time()
36-
lexpr = blosc2.open(urlpath=url_path)
36+
lexpr = blosc2.open(urlpath=url_path, mode="r")
3737
dt = time.time() - t0
3838
print(f"In {round(dt * 1000, 3)} ms opened lazy expression: shape = {lexpr.shape}, dtype = {lexpr.dtype}")
3939
t1 = time.time()

examples/ndarray/compute_expr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
# Get a LazyExpr instance
5252
(da**2 + db**2 + 2 * da * db + 1).save(urlpath="c.b2nd")
53-
dc = blosc2.open("c.b2nd")
53+
dc = blosc2.open("c.b2nd", mode="r")
5454

5555
# Evaluate: output is a NDArray
5656
dc2 = dc.compute()

examples/ndarray/dsl_save.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def heat_step(u, v):
4646
print("LazyUDF saved to heat_step.b2nd")
4747

4848
# ── reload in a 'fresh' context (no reference to heat_step) ─────────
49-
reloaded = blosc2.open("heat_step.b2nd")
49+
reloaded = blosc2.open("heat_step.b2nd", mode="r")
5050
assert isinstance(reloaded, blosc2.LazyUDF), "Expected a LazyUDF after open()"
5151
assert isinstance(reloaded.func, DSLKernel), "func must be a DSLKernel after reload"
5252
assert reloaded.func.dsl_source is not None, "dsl_source must survive the round-trip"
@@ -64,7 +64,7 @@ def heat_step(u, v):
6464
lazy2 = blosc2.lazyudf(heat_step, (u2, v), dtype=np.float64)
6565
lazy2.save(urlpath="heat_step2.b2nd")
6666

67-
reloaded2 = blosc2.open("heat_step2.b2nd")
67+
reloaded2 = blosc2.open("heat_step2.b2nd", mode="r")
6868
result2 = reloaded2.compute()
6969
expected2 = u2[()] + 0.1 * (v[()] - u2[()])
7070
assert np.allclose(result2[()], expected2)

examples/ndarray/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
print(a.info)
2828

2929
# Read a b2nd array from disk
30-
b = blosc2.open(urlpath)
30+
b = blosc2.open(urlpath, mode="r")
3131

3232
# Deal with meta
3333
m1 = b.schunk.meta.get("m5", b"0000")

examples/ndarray/persistency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
a = blosc2.asarray(nparray, urlpath=urlpath, mode="w")
2121

2222
# Read the array from disk
23-
b = blosc2.open(urlpath)
23+
b = blosc2.open(urlpath, mode="r")
2424
# And see its contents
2525
print(b[...])

examples/ndarray/reduce_and_enlarge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
url_path = "my_expr.b2nd"
5151
# Open the saved file
52-
lazy_expr = blosc2.open(urlpath=url_path)
52+
lazy_expr = blosc2.open(urlpath=url_path, mode="r")
5353
print(lazy_expr)
5454
print(f"expr (after open) shape: {lazy_expr.shape}; dtype: {lazy_expr.dtype}")
5555
# Evaluate and print the result of the lazy expression (should be a 2x4 arr)

examples/ndarray/reduce_expr_save.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
# Get a LazyExpr instance
2525
c = a**2 + b**2 + 2 * a * b + 1
2626
c.save(urlpath="c.b2nd")
27-
c = blosc2.open("c.b2nd")
27+
c = blosc2.open("c.b2nd", mode="r")
2828
# Evaluate: output is a NDArray
2929
d = blosc2.lazyexpr("a + c.sum() + a.std()", operands={"a": a, "c": c})
3030
d.save(urlpath="lazy-d.b2nd")
3131

3232
# Load the expression from disk
33-
d = blosc2.open("lazy-d.b2nd")
33+
d = blosc2.open("lazy-d.b2nd", mode="r")
3434
print(f"Expression: {d}")
3535
assert isinstance(d, blosc2.LazyExpr)
3636
e = d.compute()

src/blosc2/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def load_tensor(urlpath: str, dparams: dict | None = None) -> tensorflow.Tensor
834834
:func:`~blosc2.save_tensor`
835835
:func:`~blosc2.pack_tensor`
836836
"""
837-
schunk = blosc2.open(urlpath, dparams=dparams)
837+
schunk = blosc2.open(urlpath, mode="r", dparams=dparams)
838838
return _unpack_tensor(schunk)
839839

840840

src/blosc2/indexing.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ def _open_sidecar_handle(array: blosc2.NDArray, token: str, category: str, name:
935935
raise RuntimeError("sidecar handle path is not available")
936936
handle = legacy if isinstance(legacy, blosc2.NDArray) else blosc2.asarray(np.asarray(legacy))
937937
else:
938-
handle = blosc2.open(path, mmap_mode=_INDEX_MMAP_MODE)
938+
handle = blosc2.open(path, mode="r", mmap_mode=_INDEX_MMAP_MODE)
939939
_SIDECAR_HANDLE_CACHE[cache_key] = handle
940940
return handle
941941

@@ -1054,7 +1054,7 @@ def _compute_sorted_boundaries_from_sidecar(
10541054
) -> np.ndarray:
10551055
nsegments = math.ceil(length / segment_len)
10561056
boundaries = np.empty(nsegments, dtype=_boundary_dtype(dtype))
1057-
sidecar = blosc2.open(path, mmap_mode=_INDEX_MMAP_MODE)
1057+
sidecar = blosc2.open(path, mode="r", mmap_mode=_INDEX_MMAP_MODE)
10581058
start_value = np.empty(1, dtype=dtype)
10591059
end_value = np.empty(1, dtype=dtype)
10601060
for idx in range(nsegments):
@@ -1301,7 +1301,7 @@ def _sidecar_storage_geometry(
13011301
) -> tuple[int, int]:
13021302
if path is None:
13031303
return fallback_chunk_len, fallback_block_len
1304-
sidecar = blosc2.open(path, mmap_mode=_INDEX_MMAP_MODE)
1304+
sidecar = blosc2.open(path, mode="r", mmap_mode=_INDEX_MMAP_MODE)
13051305
return int(sidecar.chunks[0]), int(sidecar.blocks[0])
13061306

13071307

@@ -1385,7 +1385,7 @@ def _stream_copy_sidecar_array(
13851385
blocks: tuple[int, ...],
13861386
cparams: dict | None = None,
13871387
) -> None:
1388-
source = blosc2.open(str(source_path), mmap_mode=_INDEX_MMAP_MODE)
1388+
source = blosc2.open(str(source_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)
13891389
blosc2.remove_urlpath(str(dest_path))
13901390
kwargs = {"chunks": chunks, "blocks": blocks, "urlpath": str(dest_path), "mode": "w"}
13911391
if cparams is not None:
@@ -2754,7 +2754,7 @@ def _copy_sidecar_to_temp_run(
27542754
cparams: dict | None = None,
27552755
) -> Path:
27562756
out_path = workdir / f"{prefix}.b2nd"
2757-
sidecar = blosc2.open(path, mmap_mode=_INDEX_MMAP_MODE)
2757+
sidecar = blosc2.open(path, mode="r", mmap_mode=_INDEX_MMAP_MODE)
27582758
output = _create_blosc2_temp_array(out_path, length, dtype, FULL_OOC_MERGE_BUFFER_ITEMS, cparams)
27592759
chunk_len = int(sidecar.chunks[0])
27602760
for chunk_id, start in enumerate(range(0, length, chunk_len)):
@@ -2814,10 +2814,10 @@ def _merge_run_pair(
28142814
tracker: TempRunTracker | None = None,
28152815
cparams: dict | None = None,
28162816
) -> SortedRun:
2817-
left_values_mm = blosc2.open(str(left.values_path), mmap_mode=_INDEX_MMAP_MODE)
2818-
left_positions_mm = blosc2.open(str(left.positions_path), mmap_mode=_INDEX_MMAP_MODE)
2819-
right_values_mm = blosc2.open(str(right.values_path), mmap_mode=_INDEX_MMAP_MODE)
2820-
right_positions_mm = blosc2.open(str(right.positions_path), mmap_mode=_INDEX_MMAP_MODE)
2817+
left_values_mm = blosc2.open(str(left.values_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)
2818+
left_positions_mm = blosc2.open(str(left.positions_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)
2819+
right_values_mm = blosc2.open(str(right.values_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)
2820+
right_positions_mm = blosc2.open(str(right.positions_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)
28212821

28222822
out_values_path = workdir / f"full_merge_values_{merge_id}.b2nd"
28232823
out_positions_path = workdir / f"full_merge_positions_{merge_id}.b2nd"
@@ -3024,8 +3024,8 @@ def _build_full_descriptor_ooc(
30243024
array, token, kind, full, final_run, dtype, persistent, tracker, cparams
30253025
)
30263026
else:
3027-
sorted_values = blosc2.open(str(final_run.values_path), mmap_mode=_INDEX_MMAP_MODE)[:]
3028-
positions = blosc2.open(str(final_run.positions_path), mmap_mode=_INDEX_MMAP_MODE)[:]
3027+
sorted_values = blosc2.open(str(final_run.values_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)[:]
3028+
positions = blosc2.open(str(final_run.positions_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)[:]
30293029
values_sidecar = _store_array_sidecar(
30303030
array, token, kind, "full", "values", sorted_values, persistent, cparams=cparams
30313031
)
@@ -3257,14 +3257,14 @@ def iter_index_components(array: blosc2.NDArray, descriptor: dict):
32573257

32583258
def _component_nbytes(array: blosc2.NDArray, descriptor: dict, component: IndexComponent) -> int:
32593259
if component.path is not None:
3260-
return int(blosc2.open(component.path, mmap_mode=_INDEX_MMAP_MODE).nbytes)
3260+
return int(blosc2.open(component.path, mode="r", mmap_mode=_INDEX_MMAP_MODE).nbytes)
32613261
token = descriptor["token"]
32623262
return int(_load_array_sidecar(array, token, component.category, component.name).nbytes)
32633263

32643264

32653265
def _component_cbytes(array: blosc2.NDArray, descriptor: dict, component: IndexComponent) -> int:
32663266
if component.path is not None:
3267-
return int(blosc2.open(component.path, mmap_mode=_INDEX_MMAP_MODE).cbytes)
3267+
return int(blosc2.open(component.path, mode="r", mmap_mode=_INDEX_MMAP_MODE).cbytes)
32683268
token = descriptor["token"]
32693269
sidecar = _load_array_sidecar(array, token, component.category, component.name)
32703270
kwargs = {}
@@ -3804,8 +3804,8 @@ def compact_index(array: blosc2.NDArray, field: str | None = None, name: str | N
38043804
array, descriptor, final_run.values_path, final_run.positions_path, final_run.length
38053805
)
38063806
else:
3807-
sorted_values = blosc2.open(str(final_run.values_path), mmap_mode=_INDEX_MMAP_MODE)[:]
3808-
positions = blosc2.open(str(final_run.positions_path), mmap_mode=_INDEX_MMAP_MODE)[:]
3807+
sorted_values = blosc2.open(str(final_run.values_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)[:]
3808+
positions = blosc2.open(str(final_run.positions_path), mode="r", mmap_mode=_INDEX_MMAP_MODE)[:]
38093809
_replace_full_descriptor(array, descriptor, sorted_values, positions, descriptor["persistent"])
38103810
del sorted_values, positions
38113811
final_run.values_path.unlink(missing_ok=True)
@@ -4859,7 +4859,7 @@ def _bucket_batch_result_dtype(where_x) -> np.dtype:
48594859

48604860
def _bucket_worker_source(where_x):
48614861
if _supports_block_reads(where_x) and getattr(where_x, "urlpath", None) is not None:
4862-
return blosc2.open(str(where_x.urlpath), mmap_mode=_INDEX_MMAP_MODE)
4862+
return blosc2.open(str(where_x.urlpath), mode="r", mmap_mode=_INDEX_MMAP_MODE)
48634863
return where_x
48644864

48654865

@@ -4877,7 +4877,7 @@ def _gather_mmap_source(where_x):
48774877
urlpath = str(urlpath)
48784878
handle = _GATHER_MMAP_HANDLES.get(urlpath)
48794879
if handle is None:
4880-
handle = blosc2.open(urlpath, mmap_mode=_INDEX_MMAP_MODE)
4880+
handle = blosc2.open(urlpath, mode="r", mmap_mode=_INDEX_MMAP_MODE)
48814881
_GATHER_MMAP_HANDLES[urlpath] = handle
48824882
return handle
48834883

@@ -5104,17 +5104,17 @@ def process_batch(chunk_ids: np.ndarray) -> tuple[list[tuple[int, np.ndarray]],
51045104
batch_values = (
51055105
values_sidecar
51065106
if bucket.get("values_path") is None
5107-
else blosc2.open(bucket["values_path"], mmap_mode=_INDEX_MMAP_MODE)
5107+
else blosc2.open(bucket["values_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
51085108
)
51095109
batch_buckets = (
51105110
bucket_sidecar
51115111
if bucket.get("bucket_positions_path") is None
5112-
else blosc2.open(bucket["bucket_positions_path"], mmap_mode=_INDEX_MMAP_MODE)
5112+
else blosc2.open(bucket["bucket_positions_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
51135113
)
51145114
batch_l2 = (
51155115
l2_sidecar
51165116
if bucket.get("l2_path") is None
5117-
else blosc2.open(bucket["l2_path"], mmap_mode=_INDEX_MMAP_MODE)
5117+
else blosc2.open(bucket["l2_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
51185118
)
51195119
batch_results = []
51205120
batch_candidate_segments = 0
@@ -5251,9 +5251,9 @@ def _partial_chunk_nav_positions_cython(
52515251
def process_cython_batch(chunk_ids: np.ndarray) -> tuple[np.ndarray, int]:
52525252
if len(chunk_ids) == 0:
52535253
return np.empty(0, dtype=np.int64), 0
5254-
batch_values = blosc2.open(partial["values_path"], mmap_mode=_INDEX_MMAP_MODE)
5255-
batch_positions = blosc2.open(partial["positions_path"], mmap_mode=_INDEX_MMAP_MODE)
5256-
batch_l2 = blosc2.open(partial["l2_path"], mmap_mode=_INDEX_MMAP_MODE)
5254+
batch_values = blosc2.open(partial["values_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
5255+
batch_positions = blosc2.open(partial["positions_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
5256+
batch_l2 = blosc2.open(partial["l2_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
52575257
batch_l2_row = np.empty(nsegments_per_chunk, dtype=l2_boundary_dtype)
52585258
batch_span_values = np.empty(chunk_len, dtype=dtype)
52595259
batch_local_positions = np.empty(chunk_len, dtype=local_position_dtype)
@@ -5300,17 +5300,17 @@ def process_batch(chunk_ids: np.ndarray) -> tuple[list[np.ndarray], int]:
53005300
batch_values = (
53015301
values_sidecar
53025302
if partial.get("values_path") is None
5303-
else blosc2.open(partial["values_path"], mmap_mode=_INDEX_MMAP_MODE)
5303+
else blosc2.open(partial["values_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
53045304
)
53055305
batch_positions = (
53065306
positions_sidecar
53075307
if partial.get("positions_path") is None
5308-
else blosc2.open(partial["positions_path"], mmap_mode=_INDEX_MMAP_MODE)
5308+
else blosc2.open(partial["positions_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
53095309
)
53105310
batch_l2 = (
53115311
l2_sidecar
53125312
if partial.get("l2_path") is None
5313-
else blosc2.open(partial["l2_path"], mmap_mode=_INDEX_MMAP_MODE)
5313+
else blosc2.open(partial["l2_path"], mode="r", mmap_mode=_INDEX_MMAP_MODE)
53145314
)
53155315
batch_parts = []
53165316
batch_candidate_segments = 0

0 commit comments

Comments
 (0)