@@ -588,13 +588,13 @@ def save(file, arr, allow_pickle=True, fix_imports=np._NoValue):
588
588
pickle_kwargs = dict (fix_imports = fix_imports ))
589
589
590
590
591
- def _savez_dispatcher (file , * args , ** kwds ):
591
+ def _savez_dispatcher (file , * args , allow_pickle = True , ** kwds ):
592
592
yield from args
593
593
yield from kwds .values ()
594
594
595
595
596
596
@array_function_dispatch (_savez_dispatcher )
597
- def savez (file , * args , ** kwds ):
597
+ def savez (file , * args , allow_pickle = True , ** kwds ):
598
598
"""Save several arrays into a single file in uncompressed ``.npz`` format.
599
599
600
600
Provide arrays as keyword arguments to store them under the
@@ -614,6 +614,14 @@ def savez(file, *args, **kwds):
614
614
Arrays to save to the file. Please use keyword arguments (see
615
615
`kwds` below) to assign names to arrays. Arrays specified as
616
616
args will be named "arr_0", "arr_1", and so on.
617
+ allow_pickle : bool, optional
618
+ Allow saving object arrays using Python pickles. Reasons for
619
+ disallowing pickles include security (loading pickled data can execute
620
+ arbitrary code) and portability (pickled objects may not be loadable
621
+ on different Python installations, for example if the stored objects
622
+ require libraries that are not available, and not all pickled data is
623
+ compatible between different versions of Python).
624
+ Default: True
617
625
kwds : Keyword arguments, optional
618
626
Arrays to save to the file. Each array will be saved to the
619
627
output file with its corresponding keyword name.
@@ -678,16 +686,16 @@ def savez(file, *args, **kwds):
678
686
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
679
687
680
688
"""
681
- _savez (file , args , kwds , False )
689
+ _savez (file , args , kwds , False , allow_pickle = allow_pickle )
682
690
683
691
684
- def _savez_compressed_dispatcher (file , * args , ** kwds ):
692
+ def _savez_compressed_dispatcher (file , * args , allow_pickle = True , ** kwds ):
685
693
yield from args
686
694
yield from kwds .values ()
687
695
688
696
689
697
@array_function_dispatch (_savez_compressed_dispatcher )
690
- def savez_compressed (file , * args , ** kwds ):
698
+ def savez_compressed (file , * args , allow_pickle = True , ** kwds ):
691
699
"""
692
700
Save several arrays into a single file in compressed ``.npz`` format.
693
701
@@ -708,6 +716,14 @@ def savez_compressed(file, *args, **kwds):
708
716
Arrays to save to the file. Please use keyword arguments (see
709
717
`kwds` below) to assign names to arrays. Arrays specified as
710
718
args will be named "arr_0", "arr_1", and so on.
719
+ allow_pickle : bool, optional
720
+ Allow saving object arrays using Python pickles. Reasons for
721
+ disallowing pickles include security (loading pickled data can execute
722
+ arbitrary code) and portability (pickled objects may not be loadable
723
+ on different Python installations, for example if the stored objects
724
+ require libraries that are not available, and not all pickled data is
725
+ compatible between different versions of Python).
726
+ Default: True
711
727
kwds : Keyword arguments, optional
712
728
Arrays to save to the file. Each array will be saved to the
713
729
output file with its corresponding keyword name.
@@ -750,7 +766,7 @@ def savez_compressed(file, *args, **kwds):
750
766
True
751
767
752
768
"""
753
- _savez (file , args , kwds , True )
769
+ _savez (file , args , kwds , True , allow_pickle = allow_pickle )
754
770
755
771
756
772
def _savez (file , args , kwds , compress , allow_pickle = True , pickle_kwargs = None ):
@@ -777,17 +793,17 @@ def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
777
793
compression = zipfile .ZIP_STORED
778
794
779
795
zipf = zipfile_factory (file , mode = "w" , compression = compression )
780
-
781
- for key , val in namedict .items ():
782
- fname = key + '.npy'
783
- val = np .asanyarray (val )
784
- # always force zip64, gh-10776
785
- with zipf .open (fname , 'w' , force_zip64 = True ) as fid :
786
- format .write_array (fid , val ,
787
- allow_pickle = allow_pickle ,
788
- pickle_kwargs = pickle_kwargs )
789
-
790
- zipf .close ()
796
+ try :
797
+ for key , val in namedict .items ():
798
+ fname = key + '.npy'
799
+ val = np .asanyarray (val )
800
+ # always force zip64, gh-10776
801
+ with zipf .open (fname , 'w' , force_zip64 = True ) as fid :
802
+ format .write_array (fid , val ,
803
+ allow_pickle = allow_pickle ,
804
+ pickle_kwargs = pickle_kwargs )
805
+ finally :
806
+ zipf .close ()
791
807
792
808
793
809
def _ensure_ndmin_ndarray_check_param (ndmin ):
0 commit comments