You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 28768dd
Browse filesBrowse the repository at this point in the historyBrowse files
Reading data encoded with the `pickle` codec now requires an explicit opt-in, because unpickling data from an untrusted store can execute arbitrary code ([GHSA-jqc8-qjfv-5m6r](https://github.com/zarr-developers/numcodecs/security/advisories/GHSA-jqc8-qjfv-5m6r)). Enable it with `zarr.config.set({"array.allow_unsafe_pickle_decode": True})` or the environment variable `ZARR_ARRAY__ALLOW_UNSAFE_PICKLE_DECODE=True`. Without the opt-in, reading such data raises `zarr.errors.PickleCodecDisabledError`. numcodecs has its own opt-in (`numcodecs.pickles.ALLOW_UNSAFE_PICKLE_DECODE`), which must also be enabled. Writing with the `pickle` codec is unchanged.
`zarr.open_array` and `zarr.open` (sync and async) now apply the `config` argument when opening an existing array. Previously `config` was only used when a new array was created, so `zarr.open_array(store, config={"read_missing_chunks": False})` silently ran an existing array with the global defaults. `config` is now a named parameter of both functions and of `Array.open`/`AsyncArray.open`. `zarr.open` raises a `TypeError` naming the problem when `config` is given for a group.
Fixed several ways `ZipStore` could lose data or fail. A store created with `mode="w"` or `mode="x"` now keeps its entries when it is used again after `close()`, `move()`, or unpickling, instead of truncating the archive. Every method now opens the archive on first use, under a lock, so `get`, `get_partial_values`, `set_if_not_exists`, and `clear` no longer raise `AttributeError` on a store that was never opened, and threads sharing a store no longer open the archive twice. An unpickled store now opens its archive on first use rather than when it is unpickled, so writing a dask array to a `ZipStore` no longer produces an unreadable file. Where reopening an archive would silently drop its entries, the store now raises instead: `RuntimeError` after a failed `close()`, `zipfile.BadZipFile` if the file is not a complete zip archive, and `io.UnsupportedOperation` for a file object that is not readable and seekable. The type of the `mode` parameter now includes `"x"`. Builds on [#3593](https://github.com/zarr-developers/zarr-python/pull/3593) by [@oelhammouchi](https://github.com/oelhammouchi).
The minimum supported numcodecs version is now 0.16.6, and 0.17.0 is excluded. Those versions leave `Pickle.decode` enabled by default, which allows arbitrary code execution when reading an untrusted Zarr v2 array ([GHSA-jqc8-qjfv-5m6r](https://github.com/zarr-developers/numcodecs/security/advisories/GHSA-jqc8-qjfv-5m6r)).
[Zarr format 3 specification](https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#chunk-encoding)
203
+
classifies each codec by the input and output of its encode transform, as
204
+
"array -> array", "array -> bytes", or "bytes -> bytes", and requires the list to
205
+
contain zero or more array -> array codecs, followed by exactly one
206
+
array -> bytes codec, followed by zero or more bytes -> bytes codecs.
207
+
208
+
Zarr-Python's array creation functions expose that list through three
209
+
parameters: `filters`, `serializer`, and `compressors`. These names are not
210
+
defined in the Zarr format 3 specification. They are Zarr-Python aliases for
211
+
the specification's codec kinds:
212
+
213
+
-`filters`: the array -> array codecs, given as
214
+
[`zarr.abc.codec.ArrayArrayCodec`][] instances. These transform chunk arrays
215
+
into chunk arrays before serialization.
216
+
-`serializer`: the array -> bytes codec, given as one
217
+
[`zarr.abc.codec.ArrayBytesCodec`][] instance. This transforms a chunk array
218
+
into bytes. Every Zarr format 3 array needs exactly one array -> bytes codec,
219
+
either supplied explicitly or chosen by default.
220
+
-`compressors`: the bytes -> bytes codecs, given as
221
+
[`zarr.abc.codec.BytesBytesCodec`][] instances. These transform bytes into
222
+
bytes after serialization.
223
+
224
+
The `compressors` parameter is only for bytes-to-bytes codecs. If a codec is an
225
+
`ArrayBytesCodec`, pass it with `serializer`, not `compressors`. For example, the
226
+
built-in [`zarr.codecs.BytesCodec`][] can be supplied explicitly as the serializer:
The same rule applies to third-party Zarr format 3 codecs: if the codec is
243
+
documented as an `ArrayBytesCodec`, provide an instance as `serializer=...`.
244
+
197
245
## Compressors
198
246
199
247
A number of different compressors can be used with Zarr. Zarr includes Blosc,
200
248
Zstandard and Gzip compressors. Additional compressors are available through
201
249
a separate package called [NumCodecs](https://numcodecs.readthedocs.io/en/stable/) which provides various
202
250
compressor libraries including LZ4, Zlib, BZ2 and LZMA.
203
-
Different compressors can be provided via the `compressors` keyword
251
+
Different bytes-to-bytes compressors can be provided via the `compressors` keyword
204
252
argument accepted by all array creation functions. For example:
- Whether missing chunks are filled with the array's fill value on read `array.read_missing_chunks` (default `True`). Set to `False` to raise a [`ChunkNotFoundError`][zarr.errors.ChunkNotFoundError] instead.
43
+
- Whether data encoded with the `pickle` codec can be read `array.allow_unsafe_pickle_decode` (default `False`). Unpickling can execute arbitrary code, so only enable this for data from a source you trust, e.g. with `zarr.config.set({'array.allow_unsafe_pickle_decode': True})` or the environment variable `ZARR_ARRAY__ALLOW_UNSAFE_PICKLE_DECODE=True`. While it is disabled, reading such data raises a [`PickleCodecDisabledError`][zarr.errors.PickleCodecDisabledError]. Writing with the `pickle` codec is not affected. numcodecs has a separate opt-in for the same reason (`numcodecs.pickles.ALLOW_UNSAFE_PICKLE_DECODE = True`, or `NUMCODECS_ALLOW_UNSAFE_PICKLE_DECODE=1` before importing numcodecs); both must be enabled to read pickled data.
43
44
- Async and threading options, e.g. `async.concurrency` and `threading.max_workers`
44
45
- Selections of implementations of codecs, codec pipelines and buffers
45
46
- Enabling GPU support with `zarr.config.enable_gpu()`. See [GPU support](gpu.md) for more.
"""Raise if `config` is given for a node that `open` resolves to a group."""
446
+
ifconfigisnotNone:
447
+
raiseTypeError(
448
+
f"config is the runtime configuration of an array, but {store_path} resolves to a "
449
+
"group. Groups have no runtime configuration."
450
+
)
451
+
452
+
438
453
asyncdefopen_consolidated(
439
454
*args: Any, use_consolidated: Literal[True] =True, **kwargs: Any
440
455
) ->AsyncGroup:
@@ -1058,6 +1073,13 @@ async def create(
1058
1073
1059
1074
The elements of `codecs` specify the transformation from array values to stored bytes.
1060
1075
Zarr format 3 only. Zarr format 2 arrays should use `filters` and `compressor` instead.
1076
+
The [Zarr format 3 specification](https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#chunk-encoding)
1077
+
requires, in order, zero or more array -> array codecs
1078
+
([`zarr.abc.codec.ArrayArrayCodec`][]), exactly one array -> bytes codec
1079
+
([`zarr.abc.codec.ArrayBytesCodec`][]), and zero or more bytes -> bytes codecs
1080
+
([`zarr.abc.codec.BytesBytesCodec`][]). Elsewhere in Zarr-Python these are
1081
+
called filters, the serializer, and compressors; those names are Zarr-Python
1082
+
aliases, not terms defined in the specification.
1061
1083
1062
1084
If no codecs are provided, default codecs will be used based on the data type of the array.
1063
1085
For most data types, the default codecs are the tuple `(BytesCodec(), ZstdCodec())`;
@@ -1306,6 +1328,7 @@ async def open_array(
1306
1328
zarr_format: ZarrFormat|None=None,
1307
1329
path: PathLike="",
1308
1330
storage_options: dict[str, Any] |None=None,
1331
+
config: ArrayConfigLike|None=None,
1309
1332
**kwargs: Any, # TODO: type kwargs as valid args to save
1310
1333
) ->AnyAsyncArray:
1311
1334
"""Open an array using file-mode-like semantics.
@@ -1323,6 +1346,9 @@ async def open_array(
1323
1346
storage_options : dict
1324
1347
If using an fsspec URL to create the store, these will be passed to
1325
1348
the backend implementation. Ignored otherwise.
1349
+
config : ArrayConfigLike or None, default=None
1350
+
Runtime configuration for the array, whether it is opened or created. Keys not
1351
+
specified are taken from the global configuration.
1326
1352
**kwargs
1327
1353
Any keyword arguments to pass to [`create`][zarr.api.asynchronous.create].
1328
1354
@@ -1341,7 +1367,7 @@ async def open_array(
1341
1367
# mode "w" replaces any existing array, so there is nothing to open
Copy file name to clipboardExpand all lines: src/zarr/api/synchronous.py
+31-7Lines changed: 31 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -186,6 +186,7 @@ def open(
186
186
zarr_format: ZarrFormat|None=None,
187
187
path: str|None=None,
188
188
storage_options: dict[str, Any] |None=None,
189
+
config: ArrayConfigLike|None=None,
189
190
**kwargs: Any, # TODO: type kwargs as valid args to async_api.open
190
191
) ->AnyArray|Group:
191
192
"""Open a group or array using file-mode-like semantics.
@@ -210,6 +211,11 @@ def open(
210
211
storage_options : dict
211
212
If using an fsspec URL to create the store, these will be passed to
212
213
the backend implementation. Ignored otherwise.
214
+
config : ArrayConfigLike or None, default=None
215
+
Runtime configuration for the array, whether it is opened or created. Keys not
216
+
specified are taken from the global configuration. Groups have no runtime
217
+
configuration, so passing `config` when a group is opened or created raises a
218
+
`TypeError`.
213
219
**kwargs
214
220
Additional parameters are passed through to `zarr.open_array` or
215
221
`zarr.open_group`.
@@ -237,6 +243,7 @@ def open(
237
243
zarr_format=zarr_format,
238
244
path=path,
239
245
storage_options=storage_options,
246
+
config=config,
240
247
**kwargs,
241
248
)
242
249
)
@@ -775,6 +782,13 @@ def create(
775
782
776
783
The elements of `codecs` specify the transformation from array values to stored bytes.
777
784
Zarr format 3 only. Zarr format 2 arrays should use `filters` and `compressor` instead.
785
+
The [Zarr format 3 specification](https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#chunk-encoding)
786
+
requires, in order, zero or more array -> array codecs
787
+
([`zarr.abc.codec.ArrayArrayCodec`][]), exactly one array -> bytes codec
788
+
([`zarr.abc.codec.ArrayBytesCodec`][]), and zero or more bytes -> bytes codecs
789
+
([`zarr.abc.codec.BytesBytesCodec`][]). Elsewhere in Zarr-Python these are
790
+
called filters, the serializer, and compressors; those names are Zarr-Python
791
+
aliases, not terms defined in the specification.
778
792
779
793
If no codecs are provided, default codecs will be used based on the data type of the array.
780
794
For most data types, the default codecs are the tuple `(BytesCodec(), ZstdCodec())`;
@@ -928,10 +942,13 @@ def create_array(
928
942
filters are applied (if any are specified) and the data is serialized into bytes.
929
943
930
944
For Zarr format 3, a "compressor" is a codec that takes a bytestream, and
931
-
returns another bytestream. Multiple compressors may be provided for Zarr format 3.
932
-
If no `compressors` are provided, a default set of compressors will be used.
933
-
These defaults can be changed by modifying the value of `array.v3_default_compressors`
934
-
in [`zarr.config`][zarr.config].
945
+
returns another bytestream. These values must be instances of
946
+
[`zarr.abc.codec.BytesBytesCodec`][], or dict representations of
947
+
[`zarr.abc.codec.BytesBytesCodec`][]. Multiple compressors may be provided
948
+
for Zarr format 3. Codecs that take an array and return bytes are serializers
949
+
and must be supplied with `serializer` instead.
950
+
If no `compressors` are provided, the default for Zarr format 3 is
951
+
`(ZstdCodec(),)`.
935
952
Use `None` to omit default compressors.
936
953
937
954
For Zarr format 2, a "compressor" can be any numcodecs codec. Only a single compressor may
0 commit comments