U
    9%e6                     @   sp  d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZ eejd
d Zeejdd Zeejeejdd Zeejdd Zeejdd Z eej!dd Z"eej!dd Z#eej$dd Z%eej$dd Z&eej'dd Z(eej'eej)dd Z*eej+d d! Z,eej+d"d# Z-eej.d$d% Z/eej.d&d' Z0eej1d(d) Z2eej3d*d+ Z4eej3d,d- Z5eej6d.d/ Z7eej6d0d1 Z8eej9d2d3 Z:eej9d4d5 Z;eej<d6d7 Z=eej>d8d9 Z?eej>d:d; Z@eejAd<d= ZBeejAd>d? ZCeejDd@dA ZEeejFdBdC ZGeejHdDdE ZIeejJdFdG ZKeejLdHdI ZMeejJdJdK ZNeejOeejPdLdM ZQeejReejSdNdO ZTeejUdPdQ ZVeejWdRdS ZXG dTdU dUeYZZdVdW Z[eejWdXdY Z\e	ejWdZd[ Z]d\d] Z^eej_d^d_ Z`d`da Zaeej_dbdc Zbe	ej_ddde Zceejddfdg Zeeejddhdi Zfeejgdjdk Zheejgdldm Zieejjdndo Zkeejjdpdq Zleejmeejndrds Zoeejmeejndtdu Zpeejqdvdw Zreejsdxdy Zteejsdzd{ Zueejvd|d} Zweejvd~ds Zodd Zxdd Zyeejzdd Z{eej|dd Z}eej|dd Z~eejdd Zeejdd Zeejdd ZedZeejdd Zeejdd ZdS )zG
Boxing and unboxing of native Numba values to / from CPython objects.
    )ir)typescgutils)boxunboxreflectNativeValue)NumbaNotImplementedError)typeofPurpose)setobjlistobj)numpy_support)contextmanager	ExitStackc                 C   s   |j |S N)pyapiZbool_from_booltypvalc r   P/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/core/boxing.pybox_bool   s    r   c                 C   s<   |j |}t|jd}|jd||}t||j  dS )Nr   !=is_error)	r   Zobject_istruer   Constanttypebuildericmp_signedr   c_api_error)r   objr   Zistruezeror   r   r   r   unbox_boolean   s    r$   c                 C   s$   |j |j|| | j}|| j|S r   )contextcastr   Zliteral_typer   r   r   r   r   box_literal_integer!   s    r'   c                 C   sF   | j r$|j||jj}|j|S |j||jj}|j|S d S r   )	signedr   Zsextr   ZlonglongZlong_from_longlongZzextZ	ulonglongZlong_from_ulonglong)r   r   r   ZivalZullvalr   r   r   box_integer(   s
    r)   c              	   C   s   |j | }t|j|}|j|}|j|J | jrH|j	|}n|j
|}|j| |j|j||| W 5 Q R X t|j||j dS Nr   )r%   get_argument_typer   alloca_oncer   r   Znumber_longZif_object_okr(   Zlong_as_longlongZlong_as_ulonglongdecrefstoretruncr   loadr!   )r   r"   r   Zll_typer   ZlongobjZllvalr   r   r   unbox_integer1   s    "r1   c                 C   s<   | t jkr|j||jj}n| t jks,t|}|j|S r   )	r   float32r   fpextr   doublefloat64AssertionErrorZfloat_from_double)r   r   r   dbvalr   r   r   	box_floatA   s
    
r8   c                 C   sj   |j |}|j |}|j | | tjkrF|j||j	| }n| tj
ksTt|}t||j  dS r*   )r   Znumber_floatZfloat_as_doubler-   r   r2   r   Zfptruncr%   r+   r5   r6   r   r!   )r   r"   r   Zfobjr7   r   r   r   r   unbox_floatJ   s    

r9   c                 C   sr   |j j|j| |d}| tjkrH|j|j|jj}|j|j	|jj}n| tj
ksVt|j|j	 }}|j||S Nvalue)r%   make_complexr   r   	complex64r3   realr   r4   imag
complex128r6   Zcomplex_from_doubles)r   r   r   cvalZfrealZfimagr   r   r   box_complexX   s    
rC   c              	   C   s   |j |jtj}|j|| }t	|j|}t
|j| |jdd| f  W 5 Q R X | tjkr|j |j| }|j |j|jtjtj|_|j |j|jtjtj|_n| tjkst|}t| |dS )NPyExc_TypeErrorzconversion to %s failedr   )r%   r=   r   r   rA   r   Zcomplex_adaptor_getpointerr   Zis_falseZif_unlikelyerr_set_stringr>   r&   r?   r5   r2   r@   r6   r   	_getvalue)r   r"   r   Zc128okfailedZcplxr   r   r   unbox_complexd   s(    
  rJ   c                 C   s
   |j  S r   )r   	make_noner   r   r   r   box_none|   s    rL   c                 C   s   t |j S r   r   r%   get_dummy_valuer   r   r   r   
unbox_none   s    rO   c                 C   s   |j || jS r   )r   Zcreate_np_datetime	unit_coder   r   r   r   box_npdatetime   s    rQ   c                 C   s   |j |}t||j  dS r*   )r   Zextract_np_datetimer   r!   r   r"   r   r   r   r   r   unbox_npdatetime   s    rS   c                 C   s   |j || jS r   )r   Zcreate_np_timedeltarP   r   r   r   r   box_nptimedelta   s    rT   c                 C   s   |j |}t||j  dS r*   )r   Zextract_np_timedeltar   r!   rR   r   r   r   unbox_nptimedelta   s    rU   c                 C   s*   |j tj}|j||}|tj|S )z0
    Convert a raw pointer to a Python int.
    )r%   get_value_typer   uintpr   Zptrtointr   )r   r   r   Zll_intpaddrr   r   r   box_raw_pointer   s    rY   c                 C   s4   | | j|}|j|j| j}|j||fS )z6
    Fetch an enum member given its native value.
    )r   dtyper   unserializeserialize_objectinstance_classcall_function_objargs)r   r   r   valobjcls_objr   r   r   box_enum   s    ra   c                 C   s   |j |d}|| j|S )z=
    Convert an enum member's value to its native value.
    r<   )r   object_getattr_stringr   rZ   )r   r"   r   r_   r   r   r   
unbox_enum   s    rc   c                 C   sH   t t d|jjj}|j|t t d}|j	
||| j|jS )N       )r   r   ZIntTyper   Zpointeecountr   bitcastZPointerTyper   Zrecreate_recordrZ   env_manager)r   r   r   sizeptrr   r   r   
box_record   s    rk   c                    s\   j   j | }tj|}j| }j||} fdd}t	|||dS )Nc                      s   j   d S r   r   Zrelease_bufferr   bufr   r   r   cleanup   s    zunbox_record.<locals>.cleanupro   r   )
r   alloca_bufferZextract_record_datar   is_nullr   r%   rV   rg   r   )r   r"   r   rj   r   Zltypr   ro   r   rm   r   unbox_record   s    
rs   c              
   C   s*  |j j|j j|j jdtj }|jtj	|}t
j|j|d}|j||j j}|jtj| j}|d}|d}	|tj}
t
|j|}t
|j|g|jd\}|j|j||j||
g}|jt
|j| |j|j||	| W 5 Q R X W 5 Q R X |j|}|j |||S )N)         r;   r   rt   )r   Zpy_unicode_1byte_kindZpy_unicode_2byte_kindZpy_unicode_4byte_kindr   Zsizeof_unicode_charr%   get_constantr   Zint32r   alloca_once_valuer   rg   cstringintprf   r   	loop_nestr0   gepmulif_thenis_not_nullr.   addZstring_from_kind_and_data)r   r   r   Zunicode_kindkindrawptrstrptrfullsizer#   onesteprf   idxchstrlenr   r   r   box_unicodecharseq   s(    

",r   c              	   C   s   |j | }|j|\}}}}}}	t|j|n t|j	| j
}
|jd||
}|j|||
}t|d }t|j|}t|j|j||j	|| W 5 Q R X |j|}t||j|dS Nz<=r   )r%   rV   r   Zstring_as_string_size_and_kindr   	if_likelyr   r   r   r   rf   icmp_unsignedselectrx   memcpyrg   r0   r   not_)r   r"   r   ltyrH   bufferri   r   Zis_asciiZhashvstorage_size	size_fitsnull_stringoutspaceretr   r   r   unbox_unicodecharseq   s     
 r   c                 C   s<   |j |j| |}|j|j|j}|j j|j| | |S r   )	r%   make_helperr   r   bytes_from_string_and_sizedatanitemsnrtr-   )r   r   r   r"   r   r   r   r   	box_bytes
  s    r   c              
   C   s   t j|j|d}|j||jj}|jtj	| j
}|d}|d}t |j|}t |j|g|jZ\}	|j|j||	g}
|jt |j|
 |j|j|	|| W 5 Q R X W 5 Q R X |j|}|j||S )Nr;   r   rt   )r   rx   r   rg   r   ry   r%   rw   r   rz   rf   r   r{   r0   r|   r~   r   r.   r   r   )r   r   r   r   r   r   r#   r   rf   r   r   r   r   r   r   box_charseq  s    

,r   c              	   C   s   |j | }|j|\}}}t|j|n t|j	| j
}|jd||}|j|||}t|d }	t|j|	}
t|j|j|
|j	|| W 5 Q R X |j|
}t||j|dS r   )r%   rV   r   Zstring_as_string_and_sizer   r   r   r   r   r   rf   r   r   rx   r   rg   r0   r   r   )r   r"   r   r   rH   r   ri   r   r   r   r   r   r   r   r   unbox_charseq)  s     r   c              
   C   s   |j |j| |}t|j|j }|j|j\\}}|$ |	| j
|j}|j|| W 5 Q R X | |j|j | W 5 Q R X W 5 Q R X |j|S r   )r%   r   r   r   rx   r   borrow_noneif_elseZvalidr   r   r   r.   rK   r0   )r   r   r   optvalr   then	otherwiseZvalidresr   r   r   box_optionalE  s    (r   c              
      s   j  j| j} jd| j t j|j}t	 jtj
} jz\}}|H  | j| j  j| jj} j||  jj| W 5 Q R X |  j|| W 5 Q R X W 5 Q R X jdk	r fdd}	nd}	 j|}
t|
 j||	dS )z>
    Convert object *obj* to a native optional structure.
    r   Nc                	      s$    j    W 5 Q R X d S r   )r   r~   ro   r   r   Zis_not_nonenativer   r   ro   i  s    zunbox_optional.<locals>.cleanupr   ro   )r%   Zmake_optional_noner   r   r    r   r   r   r,   rx   	false_bitr   r   Zmake_optional_valuer<   r.   r   ro   r0   r   )r   r"   r   ZnonevalZretptrZerrptrr   ZorelseZjustro   r   r   r   r   unbox_optionalR  s,    
 "
r   c           	      C   sZ   ddl m} |j|\}}}}|j|j| }||_||_||_	t
| |j|dS )z;
    Convert object *obj* to a native slice structure.
    r   )slicingr   )numba.cpythonr   r   Zslice_as_intsr%   r   r   startstopr   r   rG   r   )	r   r"   r   r   rH   r   r   r   Zslir   r   r   unbox_slicet  s    r   c                 C   s   | j }dD ]b}t||}t|tr
zt|t W q
 tk
rj } ztd| dt| W 5 d }~X Y q
X q
| j  \}}|j	
|}	|j	
|}
|j	|	}|j	|
}|j	||}|j	| |j	| |S )N)r   r   r   z7Unable to create literal slice. Error encountered with z attribute. )literal_valuegetattr
isinstanceintr
   r   
ValueErrorstr
__reduce__r   r\   r[   callr-   )r   r   r   Z	slice_lit
field_nameZ	field_objeZpy_ctorZpy_argsZserialized_ctorZserialized_argsZctorargsr"   r   r   r   box_slice_literal  s&    

r   c                 C   s   t |j S r   rM   r   r"   r   r   r   r   unbox_string_literal  s    r   c           	      C   s   |j | }||j |j|d}|j jrnt| j}|j|j	|}|j
| ||}|j j|j| | |S |j}|j
| |S d S r:   )r%   
make_arrayr   
enable_nrtr   as_dtyperZ   rh   Z
read_constZ	add_constr   Znrt_adapt_ndarray_to_pythonr   r-   parentincref)	r   r   r   nativearycls	nativearynp_dtypeZdtypeptrZnewaryr   r   r   r   	box_array  s    r   c           
   	      s   j   j | }tj|}j| }|jj}| }t	jj
|> j|j j}jjrj  | nj  | W 5 Q R X  fdd}	tj|||	dS )zK
    Convert a Py_buffer-providing object to a native array structure.
    c                      s   j   d S r   rl   r   rm   r   r   ro     s    zunbox_buffer.<locals>.cleanupr   )r   rq   
get_bufferr   r   r   r%   r   rE   r   r   rg   voidptrr   Znrt_adapt_buffer_from_pythonZnumba_buffer_adaptorr   r0   )
r   r"   r   resr   r   r   aryptrrj   ro   r   rm   r   unbox_buffer  s    
r   c              	   C   s   |j | }||j |j}| }|j||jj}|j jrL|j||}n|j	||}zt
| jj}W n tk
r   tj}	Y n X |j|}|jd|j|}	|jt|j||	}
|jj|
dd |jdd W 5 Q R X t|j||
dS )zC
    Convert a Numpy array object to a native array structure.
    r   FZlikelyrD   zXcan't unbox array from PyObject into native value.  The object maybe of a different typer   )r%   r   r   rE   rg   r   r   r   Znrt_adapt_ndarray_from_pythonZnumba_array_adaptorr   r   rZ   itemsizer	   r   r   r   r   or_r   r~   rF   r   r0   )r   r"   r   r   r   r   rj   errcodeZexpected_itemsizeZitemsize_mismatchrI   r   r   r   unbox_array  s4    
r   c                 C   sN   |j | j}t| D ]2\}}|j||}|||}|j ||| q|S )zD
    Convert native array or structure *val* to a tuple object.
    )r   	tuple_newrf   	enumerater   extract_valuer   Ztuple_setitem)r   r   r   Z	tuple_valirZ   itemr"   r   r   r   	box_tuple  s    r   c                 C   sL   |j |j | j}t| ||}|j ||}|j | |j | |S )zI
    Convert native array or structure *val* to a namedtuple object.
    )r   r[   r\   r]   r   r   r-   )r   r   r   r`   Z	tuple_objr"   r   r   r   box_namedtuple  s    r   c              
      s  t | }g }g  |j| }t|jtj}t|j|}|j	|}|j
d|t|j|}	|jj|j|	dd, |jdd|f | |jtj| W 5 Q R X t| D ]r\}
}|j||
}|||}||j |jj|jdd |jtj| W 5 Q R X |jdk	r |j q|j|j| |}|j||  rv|jj|	dd  fdd	}W 5 Q R X nd}t|j|||j|d
S )zN
    Convert tuple *obj* to a native array (if homogeneous) or structure.
    z==Fr   ZPyExc_ValueErrorz<size mismatch for tuple, expected %d element(s) but got %%zdNTc                     s   t  D ]
} |   qd S r   )reversed)funcZcleanupsr   r   ro   G  s    zunbox_tuple.<locals>.cleanuprp   )lenr%   rV   r   rx   r   r   r,   r   
tuple_sizer   r   r   r   r~   r   
err_formatr.   true_bitr   Ztuple_getitemr   appendr<   r   ro   Z
make_tupler   r0   )r   r"   r   nvaluesr   is_error_ptrZ	value_ptrZactual_sizeZsize_matchesr   Zeltypeelemr   r<   ro   r   r   r   unbox_tuple  sD    


r   c                 C   s0  t |j|j| |}|j}t|j|}|jt|j|\}}| |j	
| W 5 Q R X | |j}|j	|}|jjt|j|ddV t|j|<}	||	j}
||
 || j|
}|j	||	j| W 5 Q R X W 5 Q R X |j|| W 5 Q R X W 5 Q R X |jj|j| | |j|S )z5
    Convert native list *val* to a list object.
    Tr   )r   ListInstancer%   r   r   r   rx   r   r   r   r   ri   list_newr~   	for_rangegetitemindexincref_valuer   rZ   list_setitemr.   r   r-   r0   )r   r   r   listr"   r   
has_parentr   r   loopr   itemobjr   r   r   box_listQ  s(    
&"r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_NumbaTypeHelpera  A helper for acquiring `numba.typeof` for type checking.

    Usage
    -----

        # `c` is the boxing context.
        with _NumbaTypeHelper(c) as nth:
            # This contextmanager maintains the lifetime of the `numba.typeof`
            # function.
            the_numba_type = nth.typeof(some_object)
            # Do work on the type object
            do_checks(the_numba_type)
            # Cleanup
            c.pyapi.decref(the_numba_type)
        # At this point *nth* should not be used.
    c                 C   s
   || _ d S r   )r   )selfr   r   r   r   __init__  s    z_NumbaTypeHelper.__init__c                 C   sH   | j }|j|jjd}|j|}|j|d}|| _|j	| | S )NZnumbar
   )
r   r%   insert_const_stringr   moduler   import_module_noblockrb   	typeof_fnr-   )r   r   Z
numba_nameZ	numba_modr   r   r   r   	__enter__  s    z_NumbaTypeHelper.__enter__c                 O   s   | j }|j| j d S r   )r   r   r-   r   )r   r   kwargsr   r   r   r   __exit__  s    z_NumbaTypeHelper.__exit__c                 C   s   | j j| j|g}|S r   )r   r   r^   r   )r   r"   r   r   r   r   r
     s    z_NumbaTypeHelper.typeofN)__name__
__module____qualname____doc__r   r   r   r
   r   r   r   r   r   s  s
   	r   c                    s   fdd}t j j j| |\}} jj|dd\}	}
|	h ||_t|j	d} jj
 jd||dd t }| j||}t j|z j|j}||||  | j|} jj
|jdd  jtj   W 5 Q R X |jj|jdd W 5 Q R X  j| W 5 Q R X W 5 Q R X | jrV||_ jj
 j jdd  j ||j! W 5 Q R X |"d  j|j| W 5 Q R X |
  jtj W 5 Q R X W 5 Q R X  j
 j  jj# j| |j W 5 Q R X d	S )
z9
    Construct a new native list from a Python list.
    c              	      s   |  |} jjt j|dd  jtj   W 5 Q R X  jd||} jj|dd<  jtj  j	
dd||  j	|   W 5 Q R X  j	| d S )NFr   r   rD   z(can't unbox heterogeneous list: %S != %S)r
   r   r~   r   rr   r.   r   do_breakr    r   r   r-   )nthr   expected_typobjtypobjtype_mismatchr   errorptrr   r   r   check_element_type  s&    
 z2_python_list_to_native.<locals>.check_element_typeTr   r   >F)r   N)$r   r   allocate_exr%   r   r   ri   r   r   r   r~   r    r   r
   r   Zlist_getitemr   r   r   r   rZ   r   r.   r   r   setitemr<   r-   	reflectedr   r   r0   object_set_private_datameminfo	set_dirtyr   )r   r"   r   ri   listptrr  r  rH   r   if_ok	if_not_okr#   r  r  r   r   r   r  r   _python_list_to_native  s@    
 
$r  c              
      s    j }t jtj}t j j| } j 	} j
t j|t\}}|> tj j j| |}	||	_| jr|	_ j|	j| W 5 Q R X | t|  ||| W 5 Q R X W 5 Q R X  fdd}
t j| j||
dS )z
    Convert list *obj* to a native list.

    If list was previously unboxed, we reuse the existing native list
    to ensure consistency.
    c                      s    j  d S r   r   Zobject_reset_private_datar   r   r"   r   r   ro     s    zunbox_list.<locals>.cleanupr   )r   	list_sizer   rx   r   r   r,   r%   rV   object_get_private_datar   r   r   r   from_meminfori   r  r   r.   r<   r  r   r0   )r   r"   r   ri   r  r  rj   has_meminfor   r   ro   r   r  r   
unbox_list  s&    &
r  c                 C   s  | j s
dS | jj r$d| }t|t|j|j| |}|jj|j	dd |j
}|j|}|j}|j||}|jd|t|jd}	|j|	B\}
}|
 t|j|<}||j}|| || j|}|j||j| W 5 Q R X t|j|R}|j||j}||}|| || j|}|j|| |j| W 5 Q R X W 5 Q R X |h |j|||d t|j|<}||j}|| || j|}|j||j| W 5 Q R X W 5 Q R X W 5 Q R X |d W 5 Q R X dS )zD
    Reflect the native list's contents into the Python object.
    Nz2cannot reflect element of reflected container: {}
Fr   z>=r   ) r  rZ   format	TypeErrorr   r   r%   r   r~   dirtyr   r   r  ri   subr    r   r   r   r   r   r   r   r   r   r   r   r   Zlist_appendr-   Zlist_setslicer  )r   r   r   msgr   r"   ri   new_sizediffZ	diff_gt_0Zif_growZ	if_shrinkr   r   r   r   r   r   r   reflect_list  sH    




 
0r!  c                 C   sX  t j|j|j| |\}}|jj|dd\}}	| t|jt	|j
jd}
|j
|}|j}|j
|}|j|
}|jjt|j|dd\}}| |j||
 W 5 Q R X |X |jd||}|jj|dd, |jtj| |j
dd |  W 5 Q R X W 5 Q R X W 5 Q R X || j|}|jj|jdd |jtj| W 5 Q R X |j|j
|jdd W 5 Q R X | jr||_|jj|j|j|dd |j
||j W 5 Q R X | d |j|j| W 5 Q R X |	 |jtj| W 5 Q R X W 5 Q R X |j|j| |jj!"|j| |j W 5 Q R X dS )	z7
    Construct a new native set from a Python set.
    Tr   NFr   rD   zcan't unbox heterogeneous set)Z	do_resize)#r   SetInstancer	  r%   r   r   r   rx   r   r   r   ZpyobjZset_iterater<   get_typer0   rr   r.   r    r~   r   rF   r   r   rZ   r   Z	add_pyapir  r   r   r  r  r  r   r-   )r   r"   r   ri   setptrr  rH   instr  r  Z	typobjptrr   r   r  r  Zif_firstZif_not_firstr  r   r   r   r   _python_set_to_native9  sV    
&
$r&  c              
      s    j }t jtj}t j j| } j 	} j
t j|n\}}|8 tj j j| |}	| jr|	_ j|	j| W 5 Q R X | t|  ||| W 5 Q R X W 5 Q R X  fdd}
t j| j||
dS )z
    Convert set *obj* to a native set.

    If set was previously unboxed, we reuse the existing native set
    to ensure consistency.
    c                      s    j  d S r   r  r   r  r   r   ro     s    zunbox_set.<locals>.cleanupr   )r   Zset_sizer   rx   r   r   r,   r%   rV   r  r   r   r   r"  r  r  r   r.   r<   r&  r   r0   )r   r"   r   ri   r  r$  rj   r  r   r%  ro   r   r  r   	unbox_sett  s$    &
r'  c              
   C   s   |j }|j|}t|j|}|jj|dd t|jt	|j
d}| t}|j|}|jj}	|jj|j| j|	 || j|	}
|j|||
 |j|t	|j
d}|j|| W 5 Q R X W 5 Q R X ||fS )z9
    Create a Python list from a native set's items.
    Tr   r   rt   )usedr   r   r   r   r   r~   rx   r   r   r   Z_iterater0   entrykeyr%   r   r   rZ   r   r   r   r.   )r   payloadr   r   r   rH   r   r   r   r   r   r   r   r   _native_set_to_python_list  s     
"r,  c                 C   s   t |j|j| |}|j}t|j|}|jt|j|\}}| |j	
| W 5 Q R X |^ |j}t| ||\}	}
|jj|	dd, |j	|
}|j	|
 |j|| W 5 Q R X W 5 Q R X W 5 Q R X |jj|j| | |j|S )z3
    Convert native set *val* to a set object.
    Tr   )r   r"  r%   r   r   r   rx   r   r   r   r   r+  r,  r~   Zset_newr-   r.   r   r0   )r   r   r   r%  r"   r   r   r   r+  rH   r   r   r   r   box_set  s    ,r-  c              
   C   s   | j s
dS t|j|j| |}|j}|jj|jddh |j}|j	
| t| ||\}}|jj|dd  |j	|| |j	| W 5 Q R X |d W 5 Q R X dS )zC
    Reflect the native set's contents into the Python object.
    NFr   T)r  r   r"  r%   r   r+  r~   r  r   r   Z	set_clearr,  Z
set_updater-   r  )r   r   r   r%  r+  r"   rH   r   r   r   r   reflect_set  s    r.  c                 C   s   |j || |jjS r   )r   Zfrom_native_generatorrh   Zenv_ptrr   r   r   r   box_generator  s    r/  c                 C   s   |j || S r   )r   Zto_native_generatorr   r   r   r   unbox_generator  s    r0  c                 C   s    t | j}|j|j|S r   r   r   rZ   r   r[   r\   r   r   r   r   r   r   r   	box_dtype  s    r3  c                 C   s   t |j S r   rM   r   r   r   r   unbox_dtype  s    r4  c                 C   s    t | j}|j|j|S r   r1  r2  r   r   r   box_number_class  s    r5  c                 C   s   t |j S r   rM   r   r   r   r   unbox_number_class  s    r6  c                 C   s   |S r   r   r   r   r   r   box_pyobject  s    r7  c                 C   s   t |S r   r   r   r   r   r   unbox_pyobject  s    r9  c           	   
   C   s   | j d krt| |j| }tj|jt|d dd}|j	
| j }|j	|}t|jt|j|v |j	||f}|j	| t|jt|j|6 |j	|}|j	| |j|j||| W 5 Q R X W 5 Q R X t|j||j	 dS )NZfnptr)namer   )get_pointerNotImplementedErrorr%   Zget_function_pointer_typer   rx   r   r   r   r   r\   r[   r   r   r^   r-   Zlong_as_voidptrr.   rg   r   r0   r!   )	r   r"   r   Zptrtyr   Zserr;  Zintobjrj   r   r   r   unbox_funcptr  s*    

,r=  c                 C   s(   |j j|  |j|dg|jd}|S )Nr   )rh   )r   Zfrom_native_valuegetr   r   rh   )r   r   r   outr   r   r   box_deferred)  s
    r@  c                 C   sF   |j |  |}|jj|  }||j| |j}t	||j
|jdS )Nr   )r   Zto_native_valuer>  r%   Zdata_model_managersetr   Zmake_uninitializedr<   r   r   ro   )r   r"   r   Znative_valuemodelr   r   r   r   unbox_deferred1  s    rC  c                 C   s   t |S r   r8  r   r   r   r   unbox_dispatcher:  s    rD  c                 C   s   |j | |S r   )r   r   r   r   r   r   r7  @  s    c                 C   s.   |j dd|  |j| }t|tjdS )NrD   zcan't unbox {!r} typer   )r   rF   r  r%   Zget_constant_nullr   r   r   )r   r"   r   r   r   r   r   unbox_unsupportedF  s
    rE  c                 C   s&   d| f }|j d| |j  }|S )Nz)cannot convert native %s to Python objectrD   )r   rF   Zget_null_object)r   r   r   r  r   r   r   r   box_unsupportedM  s    

rF  c                 C   s   | j }|j|j|S r   )r   r   r[   r\   )r   r   r   retvalr   r   r   box_literalT  s    rH  c                 C   s   |j |S r   )r   Znrt_meminfo_as_pyobjectr   r   r   r   box_meminfo_pointer]  s    rI  c                 C   s&   |j |}t|j|}t||dS r*   )r   Znrt_meminfo_from_pyobjectr   rr   r   r   )r   r"   r   r   Zerroredr   r   r   unbox_meminfo_pointerb  s    rJ  c                 C   s   t |j tjdS r*   )r   r%   rN   r   r   r   r   r   r   unbox_typerefh  s    rK  c                 C   s   t | ||S r   )rF  r   r   r   r   box_LiteralStrKeyDictm  s    rL  c           	   
      sB  t  jt jg  fdd fddt 	 fddt |  j j
|
_|dt  j	   W 5 Q R X d}t  j	|   W 5 Q R X t	
d 
tj|j d	}t  j	|   W 5 Q R X |d
}t  j	|   W 5 Q R X t	
d	 
tj|j  j jjd} j|}| t  j	|   W 5 Q R X |dt  j	   W 5 Q R X |dt  j	   W 5 Q R X  	
f	dd}|d |d |d   W 5 Q R X t
  jdS )ar  
    The bit_generator instance has a `.ctypes` attr which is a namedtuple
    with the following members (types):
    * state_address (Python int)
    * state (ctypes.c_void_p)
    * next_uint64 (ctypes.CFunctionType instance)
    * next_uint32 (ctypes.CFunctionType instance)
    * next_double (ctypes.CFunctionType instance)
    * bit_generator (ctypes.c_void_p)
    c                     s   D ]}  j |  qd S r   )r   r-   )Z_refr   
extra_refsr   r   clear_extra_refs  s    z9unbox_numpy_random_bitgenerator.<locals>.clear_extra_refsc                      s    j tj   d S r   )r   r.   r   r   r   )r   rO  r   r   r   handle_failure  s    z7unbox_numpy_random_bitgenerator.<locals>.handle_failurec                    s    j | |}| |S r   )r   rb   r   )r"   attrZattr_objrM  r   r   object_getattr_safely  s    
z>unbox_numpy_random_bitgenerator.<locals>.object_getattr_safelyctypesZstate_addressstater<   r&   Zc_void_pc              	      s    j | }| t j|   W 5 Q R X  j |g}t j|   W 5 Q R X  j |}|d}t j|   W 5 Q R X td|   	t
j|j d S )Nr<   Zfnptr_)r   rb   r   r   early_exit_if_nullr   Z
tuple_packr   setattrr   r   rW   r<   )r:  Zinterface_next_fnr   Zinterface_next_fn_castedZinterface_next_fn_casted_value)	r   ct_castct_voidptr_tyctypes_bindingrN  rP  rR  stack
struct_ptrr   r   wire_in_fnptrs  s(     
 z7unbox_numpy_random_bitgenerator.<locals>.wire_in_fnptrsZnext_doubleZnext_uint64Znext_uint32r   )r   rx   r   r   r   create_struct_proxyr%   r   rU  rV  r   r   rW   r<   r   r   r   r   r   r   rG   r0   )	r   r"   r   Zinterface_state_addressZinterface_stateZinterface_state_valueZctypes_nameZctypes_moduler\  r   )r   rO  rW  rX  rY  rN  rP  r   rR  rZ  r[  r   unbox_numpy_random_bitgenerators  sl    

 
 


r^  bit_generatorc           	   
   C   s   t |jt j}t }t | |j|j}|j|d}t 	|j|| |j
t j| W 5 Q R X |t|j}||_||_t d}|j|||_|j| W 5 Q R X t| |j|dS )a  
    Here we're creating a NumPyRandomGeneratorType StructModel with following fields:
    * ('bit_generator', _bit_gen_type): The unboxed BitGenerator associated with
                                        this Generator object instance.
    * ('parent', types.pyobject): Pointer to the original Generator PyObject.
    * ('meminfo', types.MemInfoPointer(types.voidptr)): The information about the memory
        stored at the pointer (to the original Generator PyObject). This is useful for
        keeping track of reference counts within the Python runtime. Helps prevent cases
        where deletion happens in Python runtime without NRT being awareness of it. 
    r_  Nr   )r   rx   r   r   r   r]  r%   r   rb   rU  r.   r   r   _bit_gen_typer<   r_  r   Z	voidptr_tZnrt_meminfo_new_from_pyobjectr  r-   r   rG   r0   )	r   r"   r   r   rZ  r[  Zbit_gen_instZunboxedZNULLr   r   r   unbox_numpy_random_generator  s     
ra  c                 C   sR   |j |j| |}|j}t|j|}|j| |j j	|j| | |j
|S r   )r%   r   r   r   r   rx   r   r   r   r-   r0   )r   r   r   r%  r"   r   r   r   r   box_numpy_random_generator  s    rb  N)r   Zllvmliter   Z
numba.corer   r   Znumba.core.pythonapir   r   r   r   Znumba.core.errorsr	   Znumba.core.typing.typeofr
   r   r   r   r   Znumba.npr   
contextlibr   r   Booleanr   r$   ZIntegerLiteralZBooleanLiteralr'   Integerr)   r1   Floatr8   r9   ComplexrC   rJ   ZNoneTyperL   ZEllipsisTyperO   Z
NPDatetimerQ   rS   ZNPTimedeltarT   rU   Z
RawPointerrY   Z
EnumMemberra   rc   ZRecordrk   rs   ZUnicodeCharSeqr   r   Bytesr   ZCharSeqr   r   Optionalr   r   Z	SliceTyper   ZSliceLiteralr   ZStringLiteralr   ZArrayr   Bufferr   r   TupleZUniTupler   
NamedTupleZNamedUniTupler   Z	BaseTupler   Listr   objectr   r  r  r!  r&  Setr'  r,  r-  r.  	Generatorr/  r0  ZDTyper3  r4  ZNumberClassr5  r6  ZPyObjectZObjectr7  r9  ZExternalFunctionPointerr=  ZDeferredTyper@  rC  Z
DispatcherrD  rE  rF  LiteralrH  ZMemInfoPointerrI  rJ  ZTypeRefrK  ZLiteralStrKeyDictrL  ZNumPyRandomBitGeneratorTyper^  r`  ZNumPyRandomGeneratorTypera  rb  r   r   r   r   <module>   s&  













	











!





/
1
!&F
&
2;
%


















x

 