
    3j7                     P   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZm	Z	m
Z
 ddlmZmZ ddgZej                  dk(  rdnd	Zd
 Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      ZddefdZd Zy)    N   )BaseHandlerregister
unregister)	b64decode	b64encoderegister_handlersunregister_handlerslittle<>c                 H    | j                   j                  }|dk(  rt        S |S )z%translate equals sign to native order=)dtype	byteordernative_byteorder)arrr   s     A/DATA/.local/lib/python3.12/site-packages/jsonpickle/ext/numpy.pyget_byteorderr      s$    		##I(C/>Y>    c                       e Zd Zd Zd Zy)NumpyBaseHandlerc                     t        |d      r|j                         |d<   y t        |      }d}|j                  |      r|t	        |      d }||d<   y )Ntostringr   z(numpy.record, )hasattrr   str
startswithlen)selfr   dataprefixs       r   flatten_dtypezNumpyBaseHandler.flatten_dtype   sQ    5*%!NN,DMJE&F'c&kB/!DMr   c                     |d   }|j                  d      rt        j                  |      }t        j                  |      S )Nr   ){[)r   astliteral_evalnpr   )r    r!   r   s      r   restore_dtypezNumpyBaseHandler.restore_dtype"   s7    WJ'$$U+Exxr   N)__name__
__module____qualname__r#   r*    r   r   r   r      s    "r   r   c                       e Zd Zd Zd Zy)NumpyDTypeHandlerc                 *    | j                  ||       |S N)r#   r    objr!   s      r   flattenzNumpyDTypeHandler.flatten*   s    3%r   c                 $    | j                  |      S r2   )r*   r    r!   s     r   restorezNumpyDTypeHandler.restore.   s    !!$''r   Nr+   r,   r-   r5   r8   r.   r   r   r0   r0   )   s    (r   r0   c                       e Zd Zd Zd Zy)NumpyGenericHandlerc                     | j                  |j                  j                  d      |       | j                  j	                  |j                         d      |d<   |S )NNFresetvalue)r#   r   newbyteordercontextr5   tolistr3   s      r   r5   zNumpyGenericHandler.flatten3   sI    39911#6=,,SZZ\,GWr   c                     | j                   j                  |d   d      }| j                  |      j                  |      S )Nr@   Fr>   )rB   r8   r*   type)r    r!   r@   s      r   r8   zNumpyGenericHandler.restore8   s;    $$T']%$@!!$',,U33r   Nr9   r.   r   r   r;   r;   2   s    
4r   r;   c                       e Zd ZdZd Zy)NumpyDatetimeHandlerzEExtend NumpyGenericHandler to handle nanosecond-resolution datetime64c                     | j                   j                  |d   d      }|d   }|j                  d      r!| j                  |      j	                  |d      S | j                  |      j	                  |      S )Nr@   Fr>   r   z[ns]ns)rB   r8   endswithr*   rE   )r    r!   r@   r   s       r   r8   zNumpyDatetimeHandler.restore@   sn    $$T']%$@W>>&!%%d+00==!!$',,U33r   N)r+   r,   r-   __doc__r8   r.   r   r   rG   rG   =   s
    O4r   rG   c                   (     e Zd ZdZ fdZd Z xZS )UnpickleableNumpyGenericHandlerz
    From issue #381, this is used for simplifying the output of numpy arrays
    when unpicklable=False (the default is True).
    c                     | j                   j                  s+| j                   j                  |j                         d      S t        t
        |   ||      S )NFr>   )rB   unpicklabler5   rC   superr;   )r    r4   r!   	__class__s      r   r5   z'UnpickleableNumpyGenericHandler.flattenN   sE    ||''<<''

E'BB,d;CFFr   c                     t         r2   )NotImplementedErrorr7   s     r   r8   z'UnpickleableNumpyGenericHandler.restoreT   s    !!r   )r+   r,   r-   rK   r5   r8   __classcell__rQ   s   @r   rM   rM   H   s    
G"r   rM   c                   (    e Zd ZdZd Zd Zd Zd Zy)NumpyNDArrayHandlerz>Stores arrays as text representation, without regard for viewsc                 @    |j                   j                  du rd|d<   y y )NF	writeable)flagsrY   r3   s      r   flatten_flagsz!NumpyNDArrayHandler.flatten_flags[   s#    99%' %D (r   c                 L    |j                  dd      sd|j                  _        y y )NrY   TF)getrZ   rY   )r    r!   r   s      r   restore_flagsz!NumpyNDArrayHandler.restore_flags_   s!    xxT*"'CII +r   c                    | j                  |j                  j                  d      |       | j                  ||       | j                  j                  |j                         d      |d<   d|j                  v r|j                  |d<   |S )Nr=   Fr>   valuesr   shape)r#   r   rA   r[   rB   r5   rC   ra   r3   s      r   r5   zNumpyNDArrayHandler.flattenc   ss    39911#6=3%--cjjl%-HX		>  IIDMr   c                 "   | j                   j                  |d   d      }t        j                  || j	                  |      |j                  dd            }|j                  dd       }||j                  |      }| j                  ||       |S )Nr`   Fr>   orderCr   rc   ra   )rB   r8   r)   arrayr*   r]   reshaper^   )r    r!   r`   r   ra   s        r   r8   zNumpyNDArrayHandler.restorem   s    %%d8nE%Bhh$,,T2$((7C:P
 $'++e$C4%
r   N)r+   r,   r-   rK   r[   r^   r5   r8   r.   r   r   rW   rW   X   s    H&(
r   rW   c                   D     e Zd ZdZdefdZd Zd Z fdZ fdZ	 xZ
S )NumpyNDArrayHandlerBinaryzstores arrays with size greater than 'size_threshold' as
    (optionally) compressed base64

    Notes
    -----
    This would be easier to implement using np.save/np.load, but
    that would be less language-agnostic
       c                      || _         || _        y)a  
        :param size_threshold: nonnegative int or None
            valid values for 'size_threshold' are all nonnegative
            integers and None
            if size_threshold is None, values are always stored as nested lists
        :param compression: a compression module or None
            valid values for 'compression' are {zlib, bz2, None}
            if compression is None, no compression is applied
        N)size_thresholdcompression)r    rl   rm   s      r   __init__z"NumpyNDArrayHandlerBinary.__init__   s     -&r   c                 X    |j                   j                  }|dk7  rt        |      |d<   y y )N|r   )r   r   r   )r    r4   r!   r   s       r   flatten_byteorderz+NumpyNDArrayHandlerBinary.flatten_byteorder   s-    II''	 -c 2D r   c                 n    |j                  dd       }|r!|j                  j                  |      |_        y y )Nr   )r]   r   rA   )r    r!   r   r   s       r   restore_byteorderz+NumpyNDArrayHandlerBinary.restore_byteorder   s0    HH[$/			..y9CI r   c                    | j                   | j                   |j                  k\  rt        |   ||      }|S |j                  t
        k(  r2t        j                  |j                               j                         }n1t        |d      r|j                  d      }n|j                  d      }| j                  r| j                  j                  |      }t        |      |d<   |j                   |d<   | j#                  |j                  j%                  d      |       | j'                  ||       | j)                  ||       |j*                  j,                  sd|d<   |S )	encode numpy to jsontobytesa)rc   r`   ra   r=   Frc   )rl   sizerP   r5   r   objectjsondumpsrC   encoder   rv   r   rm   compressr   ra   r#   rA   rq   r[   rZ   c_contiguous)r    r4   r!   bufrQ   s       r   r5   z!NumpyNDArrayHandlerBinary.flatten   s'   &$*=*=*I7?3-DH C yyF" jj.557i( kkk, lll-&&//4&s^DNIIDMsyy55c:DA""3-sD)99)) #Wr   c           	          |d   }t        |t              rt        |   |      }|S t        |t        t
        f      r)t        j                  |g| j                  |            }|S | j                  |      }t        |      }| j                  r| j                  j                  |      }|t        k(  rqt        j                  |j                               }t        j                  |||j!                  dd            }|j!                  dd      }|j|j#                  |      }nXt        j$                  |||j!                  d      |j!                  dd            j'                         }| j)                  ||       | j+                  ||       |S )	decode numpy from jsonr`   )r   rc   rd   re   ra   N)bufferr   ra   rc   )
isinstancelistrP   r8   intfloatr)   rf   r*   r   rm   
decompressrz   r{   loadsdecoder]   rg   ndarraycopyrs   r^   )r    r!   r`   r   r   r   ra   rQ   s          r   r8   z!NumpyNDArrayHandlerBinary.restore   sY   hfd#'/$'C8 
7 e-((F84+=+=d+CDC2 
- &&t,EF#C&&11#6 CJJL1hhvU$((7C:PQ$/$++e,Cjj((7+((7C0	
 $&  &&tS1tS)
r   )r+   r,   r-   rK   zlibrn   rq   rs   r5   r8   rT   rU   s   @r   ri   ri   z   s.     ')d '3
:
(T! !r   ri   c                   >     e Zd ZdZddef fd	Z fdZ fdZ xZS )NumpyNDArrayHandlerViewa  Pickles references inside ndarrays, or array-views

    Notes
    -----
    The current implementation has some restrictions.

    'base' arrays, or arrays which are viewed by other arrays,
    must be f-or-c-contiguous.
    This is not such a large restriction in practice, because all
    numpy array creation is c-contiguous by default.
    Relaxing this restriction would be nice though; especially if
    it can be done without bloating the design too much.

    Furthermore, ndarrays which are views of array-like objects
    implementing __array_interface__,
    but which are not themselves nd-arrays, are deepcopied with
    a warning (by default),
    as we cannot guarantee whatever custom logic such classes
    implement is correctly reproduced.
    warnrj   c                 4    t         |   ||       || _        y)aO  
        :param mode: {'warn', 'raise', 'ignore'}
            How to react when encountering array-like objects whose
            references we cannot safely serialize
        :param size_threshold: nonnegative int or None
            valid values for 'size_threshold' are all nonnegative
            integers and None
            if size_threshold is None, values are always stored as nested lists
        :param compression: a compression module or None
            valid values for 'compression' are {zlib, bz2, None}
            if compression is None, no compression is applied
        N)rP   rn   mode)r    r   rl   rm   rQ   s       r   rn   z NumpyNDArrayHandlerView.__init__   s     	5	r   c                 0   |j                   }|C|j                  j                  r-t        |   ||      }|j                  j
                  sd|d<   |S t        |t        j                        rG|j                  j                  r0| j                  j	                  |d      |d<   |j                  j                  |j                  j                  z
  }|r||d<   |j                  j
                  s|j                  |d<   |j                  |d	<   | j                  |j                  j!                  d
      |       | j#                  ||       t%        |      dk7  r"t%        |      t%        |      k7  rdnd}|r||d<   | j&                  | j&                  |j(                  k\  rt        t*        |   ||       |S | j,                  dk(  rd}t/        j0                  |       n| j,                  dk(  rd}t3        |      t        |   |j5                         |      }|S )ru   Nrx   rc   Fr>   baseoffsetstridesra   r=   rp   Sr   r   zndarray is defined by reference to an object we do not know how to serialize. A deep copy is serialized instead, breaking memory aliasing.raisezMndarray is defined by reference to an object we do not know how to serialize.)r   rZ   forcrP   r5   r   r   r)   r   rB   ctypesr!   r   ra   r#   r   rA   r[   r   rl   ry   ri   r   warningsr   
ValueErrorr   )r    r4   r!   r   r   r   msgrQ   s          r   r5   zNumpyNDArrayHandlerView.flatten  s   xx<CIINN7?3-D 99)) #WV U bjj)djjoo<<//E/BDLZZ__t{{'7'77F!'X99))"%++YIIDMsyy55c:DAsD)S!S(#0#5t9L#LCRV	(1D%""*d.A.ASXX.M />sDI& ! yyF"'  c"g%1  !o%7?388:t4Dr   c           
      J   |j                  dd      }|t        | 	  |      }|S | j                  j                  |d      }t	        |t
        j                        st        j                  |      }|j                  j                  sJ d       t        j                  |j                  | j                  |      j                  |j                  dd            |j                  d      |j                  d	d
      |j                  dd            }| j                  ||       |S )r   r   NFr>   z8Current implementation assumes base is C or F contiguousr   rp   ra   r   r   r   )r   r   ra   r   r   )r]   rP   r8   rB   r   r)   r   rf   rZ   r   r!   r*   rA   r^   )r    r!   r   r   rQ   s       r   r8   zNumpyNDArrayHandlerView.restoreE  s    xx%<'/$'C* 
% <<''E':DdBJJ/xx~

JIJ **yy((.;;DHH[RU<VWhhw'xx!,D1C tS)
r   )	r+   r,   r-   rK   r   rn   r5   r8   rT   rU   s   @r   r   r      s&    * #24  4l r   r   r   rj   c                     t        | ||      }t        t        j                  |d       t        t        j                  t
        d       t        t        j                  t        d       t        t        j                  t        j                        j                  t
        d       t        t        j                  t        j                        j                  t
        d       t        t        j                  t        j                        j                  t
        d       t        t        j                  t        j                        j                  t
        d       t        t        j                  t        d       y)a  Register handlers for numpy types

    :param ndarray_abc_xyz: Forward constructor arguments to NumpyNDArrayHandlerView.
        Options with an 'ndarray_' prefix correspond to the same-named
        NumpyNDArrayHandlerView constructor options, sans the 'ndarray_' prefix.
    )r   rl   rm   T)r   N)r   r   r)   r   r   r0   genericr;   voidrQ   float32int32
datetime64rG   )ndarray_modendarray_size_thresholdndarray_compressionndarray_handlers       r   r	   r	   b  s     .-'O
 RZZt4RXX(t4RZZ,48RXXbgg((*;$GRXXbjj!++->TJRXXbhh))+<4HRXXbmm$..0AMR]]0t<r   c                  J   t        t        j                         t        t        j                         t        t        j                         t        t        j                  t        j
                        j                         t        t        j                  t        j                        j                         t        t        j                  t        j                        j                         t        t        j                  t        j                        j                         y)z/Remove numpy handlers from the handler registryN)
r   r)   r   r   r   r   rQ   r   r   r   r.   r   r   r
   r
   }  s    rxxrzzrzzrxx **+rxx

#--.rxx!++,rxx&001r   )r'   r{   sysr   r   numpyr)   handlersr   r   r   utilr   r   __all__r   r   r   r   r0   r;   rG   rM   rW   ri   r   r	   r
   r.   r   r   <module>r      s    
  
    8 8 ' 5
6--833 ?{ $(( (4* 44. 4"&9 " * Dl 3 l^v7 vt =6	2r   