
    .Ph!                         d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ d Zd Z e ed                    Zd	Zd
 ZddZ G d de          Z G d de          Z G d de	          Zd ZdS )z%Numpy pickle compatibility functions.    N)BytesIO   )_ZFILE_PREFIX	Unpickler_ensure_native_byte_order_reconstructc                 ,    d                     |           S )z(Convert an int to an hexadecimal string.z{:#x})format)an_ints    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/numpy_pickle_compat.pyhex_strr      s    >>&!!!    c                 Z    t          | t                    r| S |                     d          S )Nlatin1)
isinstancebytesencode)ss    r   asbytesr      s+    !U 88Hr   l            i   c                    |                      d           t          t                    t          z   }|                     |          }|t          t                    d         }t          |d          }|                     d          }|dk    r|                      |           t          j        |                                 d|          }t          |          |k    sJ d| z              |S )zRead the z-file and return the content as a string.

    Z-files are raw data compressed with zlib used internally by joblib
    for persistence. Backward compatibility is not guaranteed. Do not
    use for external purposes.
    r   N   r          zIIncorrect data length while decompressing %s.The file could be corrupted.)seeklenr   _MAX_LENreadintzlib
decompress)file_handleheader_lengthlength	next_bytedatas        r   
read_zfiler&       s     Q&&1Mm,,FC&&(()F__F   ##ID 	''' ?;++--r6::Dt99	')4	5  Kr   c                 P   |                      t                     t          t          |                    }|                      t	          |                    t                                         |                      t          j        t	          |          |                     dS )zWrite the data in the given file as a Z-file.

    Z-files are raw data compressed with zlib used internally by joblib
    for persistence. Backward compatibility is not guaranteed. Do not
    use for external purposes.
    N)	writer   r   r   r   ljustr   r   compress)r!   r%   r*   r#   s       r   write_zfiler+   B   s     m$$$SYYFgfll84455666dmGDMM8<<=====r   c                        e Zd ZdZddZd ZdS )NDArrayWrapperzAn object to be persisted instead of numpy arrays.

    The only thing this object does, is to carry the filename in which
    the array has been persisted, and the array subclass.
    Tc                 0    || _         || _        || _        dS z4Constructor. Store the useful information for later.N)filenamesubclass
allow_mmap)selfr0   r1   r2   s       r   __init__zNDArrayWrapper.__init__[   s      $r   c                    t           j                            |j        | j                  }t          | dd          }i }|r
|j        |d<   dt          j        |j	        j
                  j        v rd|d<    |j	        j
        |fi |}t          |          }t          |d          rJ| j        |j	        j        |j	        j        fvr+t#          | j        dd          }|                    |          S |S )zReconstruct the array.r2   T	mmap_modeallow_pickle__array_prepare__)r   b)ospathjoin_dirnamer0   getattrr6   inspect	signaturenpload
parametersr   hasattrr1   ndarraymemmapr   r8   )r3   	unpicklerr0   r2   kwargsarray	new_arrays          r   r   zNDArrayWrapper.reada   s   7<<	 2DMBB T<66
 	6"+"5F;W.y|/@AALLL &*F>"!	!(55f55 *%00 5-.. 	4=L LI
 4
 4

 %T]D#>>I..u555Lr   N)T__name__
__module____qualname____doc__r4   r    r   r   r-   r-   T   sA         % % % %    r   r-   c                       e Zd ZdZd Zd ZdS )ZNDArrayWrapperaT  An object to be persisted instead of numpy arrays.

    This object store the Zfile filename in which
    the data array has been persisted, and the meta information to
    retrieve it.
    The reason that we store the raw buffer data of the array and
    the meta information, rather than array representation routine
    (tobytes) is that it enables us to use completely the strided
    model to avoid memory copies (a and a.T store as fast). In
    addition saving the heavy information separately can avoid
    creating large temporary buffers when unpickling data with
    large arrays.
    c                 0    || _         || _        || _        dS r/   )r0   state	init_args)r3   r0   rU   rT   s       r   r4   zZNDArrayWrapper.__init__   s     
"r   c                 $   t           j                            |j        | j                  }t          | j         }t          |d          5 }t          |          }ddd           n# 1 swxY w Y   | j	        |fz   }|
                    |           |S )z?Reconstruct the array from the meta-information and the z-file.rbN)r:   r;   r<   r=   r0   r   rU   openr&   rT   __setstate__)r3   rG   r0   rI   fr%   rT   s          r   r   zZNDArrayWrapper.read   s     7<<	 2DMBBdn-(D!! 	!Qa==D	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!
dW$5!!!s   	A%%A),A)NrK   rP   r   r   rR   rR      s<         # # #
 
 
 
 
r   rR   c                   x    e Zd ZdZej                                        ZddZd Zd Z	e	ee
j        d         <   dS )ZipNumpyUnpicklerz:A subclass of the Unpickler to unpickle our numpy pickles.Nc                 J   t           j                            |          | _        t           j                            |          | _        || _        |                     |          | _        t          j
        | | j                   	 ddl}n# t          $ r d}Y nw xY w|| _        dS )zConstructor.r   N)r:   r;   basename	_filenamedirnamer=   r6   _open_pickler!   r   r4   numpyImportErrorrA   )r3   r0   r!   r6   rA   s        r   r4   zZipNumpyUnpickler.__init__   s    ))(3311",,[994!1222	 	 	 	BBB	s   B
 
BBc                 :    t          t          |                    S N)r   r&   )r3   r!   s     r   ra   zZipNumpyUnpickler._open_pickle   s    z+..///r   c                 .   t          j        |            t          | j        d         t                    r`| j        t          d          | j                                        }|                    |           }| j        	                    |           dS dS )zSet the state of a newly created object.

        We capture it to replace our place-holder objects,
        NDArrayWrapper, by the array we are interested in. We
        replace them directly in the stack of pickler.
        Nz@Trying to unpickle an ndarray, but numpy didn't import correctly)
r   
load_buildr   stackr-   rA   rc   popr   append)r3   nd_array_wrapperrI   s      r   rh   zZipNumpyUnpickler.load_build   s     	T"""djnn55 	%w!V    $z~~//$))$//EJe$$$$$	% 	%r   r   re   )rL   rM   rN   rO   r   dispatchcopyr4   ra   rh   pickleBUILDrP   r   r   r\   r\      sn        DD!&&((H   0 0 0% % %" !+HV\!_r   r\   c                    t          | d          5 }t          | |          }	 |                                }n*# t          $ r}t	          d          }||_        |d}~ww xY w	 t          |d          r|j                                         n/# t          |d          r|j                                         w w xY w|cddd           S # 1 swxY w Y   dS )a&  Reconstruct a Python object from a file persisted with joblib.dump.

    This function ensures the compatibility with joblib old persistence format
    (<= 0.9.3).

    Parameters
    ----------
    filename: string
        The name of the file from which to load the object

    Returns
    -------
    result: any Python object
        The object stored in the file.

    See Also
    --------
    joblib.dump : function to save an object

    Notes
    -----

    This function can load numpy array files saved separately during the
    dump.
    rW   )r!   zyYou may be trying to read with python 3 a joblib pickle generated with python 2. This feature is not supported by joblib.Nr!   )	rX   r\   rB   UnicodeDecodeError
ValueError	__cause__rD   r!   close)r0   r!   rG   objexcnew_excs         r   load_compatibilityry      sH   4 
h		 
 &hKHHH		...""CC! 	 	 	 ; G
 !$GM	  y-00 .%++--- y-00 .%++----.)                 sC   C9B
A AA  B$*C,B::CCC)r   )rO   r?   r:   ro   r   ior   numpy_pickle_utilsr   r   r   r   r   r   r   r   _CHUNK_SIZEr&   r+   objectr-   rR   r\   ry   rP   r   r   <module>r~      sw   + +  				                   " " "
   3wwu~~  D> > > >$* * * * *V * * *Z    n   D&+ &+ &+ &+ &+	 &+ &+ &+R. . . . .r   