Read an array from an NPY file. Parameters ---------- fp : file_like object If this is not a real file object, then this may take extra memory and time. allow_pickle : bool, optional Whether to allow writing pickled data. Default: False .. versi
(fp, allow_pickle=False, pickle_kwargs=None, *,
max_header_size=_MAX_HEADER_SIZE)
| 736 | |
| 737 | |
| 738 | def read_array(fp, allow_pickle=False, pickle_kwargs=None, *, |
| 739 | max_header_size=_MAX_HEADER_SIZE): |
| 740 | """ |
| 741 | Read an array from an NPY file. |
| 742 | |
| 743 | Parameters |
| 744 | ---------- |
| 745 | fp : file_like object |
| 746 | If this is not a real file object, then this may take extra memory |
| 747 | and time. |
| 748 | allow_pickle : bool, optional |
| 749 | Whether to allow writing pickled data. Default: False |
| 750 | |
| 751 | .. versionchanged:: 1.16.3 |
| 752 | Made default False in response to CVE-2019-6446. |
| 753 | |
| 754 | pickle_kwargs : dict |
| 755 | Additional keyword arguments to pass to pickle.load. These are only |
| 756 | useful when loading object arrays saved on Python 2 when using |
| 757 | Python 3. |
| 758 | max_header_size : int, optional |
| 759 | Maximum allowed size of the header. Large headers may not be safe |
| 760 | to load securely and thus require explicitly passing a larger value. |
| 761 | See :py:func:`ast.literal_eval()` for details. |
| 762 | This option is ignored when `allow_pickle` is passed. In that case |
| 763 | the file is by definition trusted and the limit is unnecessary. |
| 764 | |
| 765 | Returns |
| 766 | ------- |
| 767 | array : ndarray |
| 768 | The array from the data on disk. |
| 769 | |
| 770 | Raises |
| 771 | ------ |
| 772 | ValueError |
| 773 | If the data is invalid, or allow_pickle=False and the file contains |
| 774 | an object array. |
| 775 | |
| 776 | """ |
| 777 | if allow_pickle: |
| 778 | # Effectively ignore max_header_size, since `allow_pickle` indicates |
| 779 | # that the input is fully trusted. |
| 780 | max_header_size = 2**64 |
| 781 | |
| 782 | version = read_magic(fp) |
| 783 | _check_version(version) |
| 784 | shape, fortran_order, dtype = _read_array_header( |
| 785 | fp, version, max_header_size=max_header_size) |
| 786 | if len(shape) == 0: |
| 787 | count = 1 |
| 788 | else: |
| 789 | count = numpy.multiply.reduce(shape, dtype=numpy.int64) |
| 790 | |
| 791 | # Now read the actual data. |
| 792 | if dtype.hasobject: |
| 793 | # The array contained Python objects. We need to unpickle the data. |
| 794 | if not allow_pickle: |
| 795 | raise ValueError("Object arrays cannot be loaded when " |
nothing calls this directly
no test coverage detected