
    rh7                     T   S SK r S SKrS SKrS SKrS SKrS SKrSSKJrJ	r	J
r
  SSKJrJr  SS/r\R                  S:X  a  SOS	rS
 r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rSS\4S jrS rg)    N   )BaseHandlerregister
unregister)	b64decode	b64encoderegister_handlersunregister_handlerslittle<>c                 J    U R                   R                  nUS:X  a  [        $ U$ )z%translate equals sign to native order=)dtype	byteordernative_byteorder)arrr   s     N/home/james-whalen/.local/lib/python3.13/site-packages/jsonpickle/ext/numpy.pyget_byteorderr      s$    		##I(C/>Y>    c                        \ rS rSrS rS rSrg)NumpyBaseHandler   c                     [        US5      (       a  UR                  5       US'   g [        U5      nSnUR                  U5      (       a  U[	        U5      S nXS'   g )Ntostringr   z(numpy.record, )hasattrr   str
startswithlen)selfr   dataprefixs       r   flatten_dtypeNumpyBaseHandler.flatten_dtype   sU    5*%%!NN,DMJE&F''c&kB/!Mr   c                     US   nUR                  S5      (       a  [        R                  " U5      n[        R                  " U5      $ )Nr   ){[)r   astliteral_evalnpr   )r!   r"   r   s      r   restore_dtypeNumpyBaseHandler.restore_dtype"   s:    WJ''$$U+Exxr    N)__name__
__module____qualname____firstlineno__r$   r,   __static_attributes__r.   r   r   r   r      s    "r   r   c                        \ rS rSrS rS rSrg)NumpyDTypeHandler)   c                 (    U R                  X5        U$ N)r$   r!   objr"   s      r   flattenNumpyDTypeHandler.flatten*   s    3%r   c                 $    U R                  U5      $ r8   )r,   r!   r"   s     r   restoreNumpyDTypeHandler.restore.   s    !!$''r   r.   Nr/   r0   r1   r2   r;   r?   r3   r.   r   r   r5   r5   )   s    (r   r5   c                        \ rS rSrS rS rSrg)NumpyGenericHandler2   c                     U R                  UR                  R                  S5      U5        U R                  R	                  UR                  5       SS9US'   U$ )NNFresetvalue)r$   r   newbyteordercontextr;   tolistr9   s      r   r;   NumpyGenericHandler.flatten3   sI    39911#6=,,SZZ\,GWr   c                 |    U R                   R                  US   SS9nU R                  U5      R                  U5      $ )NrI   FrG   )rK   r?   r,   type)r!   r"   rI   s      r   r?   NumpyGenericHandler.restore8   s;    $$T']%$@!!$',,U33r   r.   NrA   r.   r   r   rC   rC   2   s    
4r   rC   c                       \ rS rSrSrS rSrg)NumpyDatetimeHandler=   zEExtend NumpyGenericHandler to handle nanosecond-resolution datetime64c                     U R                   R                  US   SS9nUS   nUR                  S5      (       a!  U R                  U5      R	                  US5      $ U R                  U5      R	                  U5      $ )NrI   FrG   r   z[ns]ns)rK   r?   endswithr,   rO   )r!   r"   rI   r   s       r   r?   NumpyDatetimeHandler.restore@   sq    $$T']%$@W>>&!!%%d+00==!!$',,U33r   r.   N)r/   r0   r1   r2   __doc__r?   r3   r.   r   r   rR   rR   =   s
    O4r   rR   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )UnpickleableNumpyGenericHandlerH   zx
From issue #381, this is used for simplifying the output of numpy arrays
when unpicklable=False (the default is True).
c                    > U R                   R                  (       d(  U R                   R                  UR                  5       SS9$ [        [
        U ]  X5      $ )NFrG   )rK   unpicklabler;   rL   superrC   )r!   r:   r"   	__class__s      r   r;   'UnpickleableNumpyGenericHandler.flattenN   sC    ||''<<''

E'BB,d;CFFr   c                     [         er8   )NotImplementedErrorr>   s     r   r?   'UnpickleableNumpyGenericHandler.restoreT   s    !!r   r.   )	r/   r0   r1   r2   rX   r;   r?   r3   __classcell__r_   s   @r   rZ   rZ   H   s    
G" "r   rZ   c                   0    \ rS rSrSrS rS rS rS rSr	g)	NumpyNDArrayHandlerX   z>Stores arrays as text representation, without regard for viewsc                 B    UR                   R                  SL a  SUS'   g g )NF	writeable)flagsrj   r9   s      r   flatten_flags!NumpyNDArrayHandler.flatten_flags[   s#    99%' %D (r   c                 V    UR                  SS5      (       d  SUR                  l        g g )Nrj   TF)getrk   rj   )r!   r"   r   s      r   restore_flags!NumpyNDArrayHandler.restore_flags_   s$    xxT**"'CII +r   c                    U R                  UR                  R                  S5      U5        U R                  X5        U R                  R                  UR                  5       SS9US'   SUR                  ;   a  UR                  US'   U$ )NrF   FrG   valuesr   shape)r$   r   rJ   rl   rK   r;   rL   rt   r9   s      r   r;   NumpyNDArrayHandler.flattenc   sq    39911#6=3%--cjjl%-HX		>  IIDMr   c                    U R                   R                  US   SS9n[        R                  " X R	                  U5      UR                  SS5      S9nUR                  SS 5      nUb  UR                  U5      nU R                  X5        U$ )Nrs   FrG   orderCr   rw   rt   )rK   r?   r+   arrayr,   ro   reshaperp   )r!   r"   rs   r   rt   s        r   r?   NumpyNDArrayHandler.restorem   s    %%d8nE%Bhh,,T2$((7C:P
 $'++e$C4%
r   r.   N)
r/   r0   r1   r2   rX   rl   rp   r;   r?   r3   r.   r   r   rg   rg   X   s    H&(
r   rg   c                   R   ^  \ rS rSrSrS\4S jrS rS rU 4S jr	U 4S jr
S	rU =r$ )
NumpyNDArrayHandlerBinaryz   zstores 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                     Xl         X l        g)ah  
: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!   r   r   s      r   __init__"NumpyNDArrayHandlerBinary.__init__   s     -&r   c                 Z    UR                   R                  nUS:w  a  [        U5      US'   g g )N|r   )r   r   r   )r!   r:   r"   r   s       r   flatten_byteorder+NumpyNDArrayHandlerBinary.flatten_byteorder   s-    II''	 -c 2D r   c                 x    UR                  SS 5      nU(       a!  UR                  R                  U5      Ul        g g )Nr   )ro   r   rJ   )r!   r"   r   r   s       r   restore_byteorder+NumpyNDArrayHandlerBinary.restore_byteorder   s0    HH[$/			..y9CI r   c                   > U R                   b  U R                   UR                  :  a  [        TU ]  X5      nU$ UR                  [
        :X  a3  [        R                  " UR                  5       5      R                  5       nO0[        US5      (       a  UR                  SS9nOUR                  SS9nU R                  (       a  U R                  R                  U5      n[        U5      US'   UR                   US'   U R#                  UR                  R%                  S5      U5        U R'                  X5        U R)                  X5        UR*                  R,                  (       d  SUS'   U$ )	encode numpy to jsontobytesa)rw   rs   rt   rF   Frw   )r   sizer^   r;   r   objectjsondumpsrL   encoder   r   r   r   compressr   rt   r$   rJ   r   rl   rk   c_contiguous)r!   r:   r"   bufr_   s       r   r;   !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-s)99)) #Wr   c           	      2  > US   n[        U[        5      (       a  [        TU ]  U5      nU$ [        U[        [
        45      (       a'  [        R                  " U/U R                  U5      S9nU$ U R                  U5      n[        U5      nU R                  (       a  U R                  R                  U5      nU[        :X  ap  [        R                  " UR                  5       5      n[        R                  " X$UR!                  SS5      S9nUR!                  SS5      nUb  UR#                  U5      nOU[        R$                  " UUUR!                  S5      UR!                  SS5      S9R'                  5       nU R)                  X5        U R+                  X5        U$ )	decode numpy from jsonrs   )r   rw   rx   ry   rt   N)bufferr   rt   rw   )
isinstancelistr^   r?   intfloatr+   rz   r,   r   r   
decompressr   r   loadsdecodero   r{   ndarraycopyr   rp   )r!   r"   rs   r   r   r   rt   r_   s          r   r?   !NumpyNDArrayHandlerBinary.restore   sZ   hfd##'/$'C8 
7 e--((F84+=+=d+CDC2 
- &&t,EF#C&&11#6 CJJL1hhv$((7C:PQ$/$++e,Cjj((7+((7C0	
 $&  &&t1t)
r   )r   r   )r/   r0   r1   r2   rX   zlibr   r   r   r;   r?   r3   rd   re   s   @r   r~   r~   z   s.     ')d '3
:
(T! !r   r~   c                   N   ^  \ rS rSrSrSS\4U 4S jjrU 4S jrU 4S jrSr	U =r
$ )	NumpyNDArrayHandlerView   a  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.
warnr   c                 0   > [         TU ]  X#5        Xl        g)a  
: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)r^   r   mode)r!   r   r   r   r_   s       r   r    NumpyNDArrayHandlerView.__init__   s     	5	r   c                 v  > UR                   nUcL  UR                  R                  (       a1  [        TU ]  X5      nUR                  R
                  (       d  SUS'   U$ [        U[        R                  5      (       GaX  UR                  R                  (       Ga<  U R                  R	                  USS9US'   UR                  R                  UR                  R                  -
  nU(       a  XBS'   UR                  R
                  (       d  UR                  US'   UR                  US	'   U R                  UR                  R!                  S
5      U5        U R#                  X5        [%        U5      S:w  a'  [%        U5      [%        U5      :w  a  SOSnU(       a  XRS'   U R&                  b  U R&                  UR(                  :  a  [        [*        U ]  X5        U$ U R,                  S:X  a  Sn[.        R0                  " U5        OU R,                  S:X  a  Sn[3        U5      e[        TU ]  UR5                  5       U5      nU$ )r   Nr   rw   FrG   baseoffsetstridesrt   rF   r   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   rk   forcr^   r;   r   r   r+   r   rK   ctypesr"   r   rt   r$   r   rJ   rl   r   r   r   r~   r   warningsr   
ValueErrorr   )r!   r:   r"   r   r   r   msgr_   s          r   r;   NumpyNDArrayHandlerView.flatten  s   xx<CIINN7?3-D 99)) #WV U bjj))djjooo<<//E/BDLZZ__t{{'7'77F!'X99))"%++YIIDMsyy55c:DAs)S!S(#0#5t9L#LCRV	(1%""*d.A.ASXX.M />sI& ! yyF"'  c"g%1  !o%7?388:t4Dr   c           
      V  > UR                  SS5      nUc  [        TU ]	  U5      nU$ U R                  R                  USS9n[	        U[
        R                  5      (       d  [
        R                  " U5      nUR                  R                  (       d   S5       e[
        R                  " UR                  U R                  U5      R                  UR                  SS5      5      UR                  S5      UR                  S	S
5      UR                  SS5      S9nU R                  X5        U$ )r   r   NFrG   z8Current implementation assumes base is C or F contiguousr   r   rt   r   r   r   )r   r   rt   r   r   )ro   r^   r?   rK   r   r+   r   rz   rk   r   r"   r,   rJ   rp   )r!   r"   r   r   r_   s       r   r?   NumpyNDArrayHandlerView.restoreE  s    xx%<'/$'C* 
% <<''E':DdBJJ//xx~

JIJ **yy((.;;DHH[RU<VWhhw'xx!,D1C t)
r   )r   )r/   r0   r1   r2   rX   r   r   r;   r?   r3   rd   re   s   @r   r   r      s&    * #24  4l r   r   r   r   c                    [        U UUS9n[        [        R                  USS9  [        [        R                  [
        SS9  [        [        R                  [        SS9  [        [        R                  " [        R                  5      R                  [
        SS9  [        [        R                  " [        R                  5      R                  [
        SS9  [        [        R                  " [        R                  5      R                  [
        SS9  [        [        R                  " [        R                  5      R                  [
        SS9  [        [        R                  [        SS9  g)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   r   r   T)r   N)r   r   r+   r   r   r5   genericrC   voidr_   float32int32
datetime64rR   )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                  R   [        [        R                  5        [        [        R                  5        [        [        R                  5        [        [        R                  " [        R
                  5      R                  5        [        [        R                  " [        R                  5      R                  5        [        [        R                  " [        R                  5      R                  5        [        [        R                  " [        R                  5      R                  5        g)z/Remove numpy handlers from the handler registryN)
r   r+   r   r   r   r   r_   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   r5   rC   rR   rZ   rg   r~   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   