o
    1&i                     @   s  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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% Z0eej/d&d' Z1eej2d(d) Z3eej4d*d+ Z5eej4d,d- Z6eej7d.d/ Z8eej9d0d1 Z:eej9d2d3 Z;eej<d4d5 Z=eej<d6d7 Z>eej?d8d9 Z@eejAd:d; ZBeejAd<d= ZCeejDd>d? ZEeejDd@dA ZFeejGdBdC ZHeejIdDdE ZJeejKdFdG ZLeejMdHdI ZNeejOdJdK ZPeejMdLdM ZQeejReejSdNdO ZTeejUeejVdPdQ ZWeejXdRdS ZYeejZdTdU Z[G dVdW dWe\Z]dXdY Z^eejZdZd[ Z_e	ejZd\d] Z`d^d_ Zaeejbd`da Zcdbdc Zdeejbddde Zee	ejbdfdg Zfeejgdhdi Zheejgdjdk Zieejjdldm Zkeejjdndo Zleejmdpdq Zneejmdrds Zoeejpeejqdtdu Zreejpeejqdvdw Zseejtdxdy Zueejvdzd{ Zweejvd|d} Zxeejyd~d Zzeejyddu Zrdd Z{dd Z|eej}dd Z~eejdd Zeejdd Ze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TypingError)typeofPurpose)setobjlistobj)numpy_support)contextmanager	ExitStackc                 C      |j |S N)pyapiZbool_from_booltypvalc r   <C:\wamp64\www\opt\env\Lib\site-packages\numba/core/boxing.pybox_bool      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   sB   | j r|j||jj}|j|S |j||jj}|j|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r$|j	|}n|j
|}|j| |j|j||| W d    n1 sFw   Y  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   r4   c                 C   s<   | t jkr|j||jj}n	| t jksJ |}|j|S r   )r   float32r"   fpextr   doublefloat64Zfloat_from_double)r   r   r   dbvalr   r   r   	box_floatA   s
   
r:   c                 C   sj   |j |}|j |}|j | | tjkr#|j||j	| }n	| tj
ks*J |}t||j  dS r-   )r   Znumber_floatZfloat_as_doubler0   r   r5   r"   Zfptruncr(   r.   r8   r   r$   )r   r%   r   Zfobjr9   r   r   r   r   unbox_floatJ   s   

r;   c                 C   sr   |j j|j| |d}| tjkr$|j|j|jj}|j|j	|jj}n| tj
ks+J |j|j	}}|j||S Nvalue)r(   make_complexr"   r   	complex64r6   realr   r7   imag
complex128Zcomplex_from_doubles)r   r   r   cvalZfrealZfimagr   r   r   box_complexX   s   
rE   c                 C   s   |j |jtj}|j|| }t	|j|}t
|j| |jdd| f  W d    n1 s5w   Y  | tjkrd|j |j| }|j |j|jtjtj|_|j |j|jtjtj|_n	| tjkskJ |}t| |dS )NPyExc_TypeErrorzconversion to %s failedr   )r(   r?   r"   r   rC   r   Zcomplex_adaptor_getpointerr   Zis_falseZif_unlikelyerr_set_stringr@   r)   rA   r8   r5   rB   r   	_getvalue)r   r%   r   Zc128okfailedZcplxr   r   r   unbox_complexd   s&   
rL   c                 C   s
   |j  S r   )r   	make_noner   r   r   r   box_none|   s   
rN   c                 C      t |j S r   r   r(   get_dummy_valuer   r   r   r   
unbox_none      rR   c                 C      |j || jS r   )r   Zcreate_np_datetime	unit_coder   r   r   r   box_npdatetime      rV   c                 C      |j |}t||j  dS r-   )r   Zextract_np_datetimer   r$   r   r%   r   r   r   r   r   unbox_npdatetime      rZ   c                 C   rT   r   )r   Zcreate_np_timedeltarU   r   r   r   r   box_nptimedelta   rW   r\   c                 C   rX   r-   )r   Zextract_np_timedeltar   r$   rY   r   r   r   unbox_nptimedelta   r[   r]   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   ra   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   ri   c                 C   s   |j |d}|| j|S )z=
    Convert an enum member's value to its native value.
    r>   )r   object_getattr_stringr   rb   )r   r%   r   rg   r   r   r   
unbox_enum   s   rk   c                 C   s   d}t |)z7This type cannot be boxed, there's no Python equivalentzJUndefVar type cannot be boxed, there is no Python equivalent of this type.)r
   )r   r   r   msgr   r   r   box_undefvar   s   rm   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_recordrb   env_manager)r   r   r   sizeptrr   r   r   
box_record   s   ru   c                    s\   j   j | }tj|}j| }j||} fdd}t	|||dS )Nc                         j   d S r   r   Zrelease_bufferr   bufr   r   r   cleanup      zunbox_record.<locals>.cleanuprz   r   )
r   alloca_bufferZextract_record_datar   is_nullr"   r(   r^   rq   r   )r   r%   r   rt   r   Zltypr   rz   r   rx   r   unbox_record   s   
r   c              
   C   sR  |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A\}|j|j||j||
g}|jt
|j| |j|j||	| W d    n1 sw   Y  W d    n1 sw   Y  |j|}|j |||S )N)         r=   r   r   )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"   rq   cstringintprp   r!   	loop_nestr3   gepmulif_thenis_not_nullr1   addZstring_from_kind_and_data)r   r   r   Zunicode_kindkindrawptrstrptrfullsizer&   onesteprp   idxchstrlenr   r   r   box_unicodecharseq   s.   

"r   c                 C   s   |j | }|j|\}}}}}}	t|j|< t|j	| j
}
|jd||
}|j|||
}t|d }t|j|}t|j|j||j	|| W d    n1 sXw   Y  |j|}t||j|dS Nz<=r   )r(   r^   r   Zstring_as_string_size_and_kindr   	if_likelyr"   r   r    r!   rp   icmp_unsignedselectr   memcpyrq   r3   r   not_)r   r%   r   ltyrJ   bufferrs   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nrtr0   )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<\}	|j|j||	g}
|jt |j|
 |j|j|	|| W d    n1 sew   Y  W d    n1 stw   Y  |j|}|j||S )Nr=   r   r   )r   r   r"   rq   r   r   r(   r   r   r   rp   r!   r   r3   r   r   r   r1   r   r   )r   r   r   r   r   r   r&   r   rp   r   r   r   r   r   r   box_charseq  s   

r   c                 C   s   |j | }|j|\}}}t|j|< t|j	| j
}|jd||}|j|||}t|d }	t|j|	}
t|j|j|
|j	|| W d    n1 sUw   Y  |j|
}t||j|dS r   )r(   r^   r   Zstring_as_string_and_sizer   r   r"   r   r    r!   rp   r   r   r   r   rq   r3   r   r   )r   r%   r   r   rJ   r   rs   r   r   r   r   r   r   r   r   unbox_charseq1  s   r   c              	   C   s   |j |j| |}t|j|j }|j|jG\}}| |	| j
|j}|j|| W d    n1 s9w   Y  | |j|j | W d    n1 sUw   Y  W d    n1 sdw   Y  |j|S r   )r(   r   r"   r   r   r   borrow_noneif_elseZvalidr   r!   r   r1   rM   r3   )r   r   r   optvalr   then	otherwiseZvalidresr   r   r   box_optionalM  s   r   c              	      sR   j  j| j} jd| j t j|j}t	 jtj
} jV\}}|)  | j| j  j| jj} j||  jj| W d   n1 s[w   Y  |  j|| W d   n1 stw   Y  W d   n1 sw   Y  jdu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 d    d S 1 sw   Y  d S r   )r"   r   rz   r   r   Zis_not_nonenativer   r   rz   q  s   
"zunbox_optional.<locals>.cleanupr   rz   )r(   Zmake_optional_noner"   r!   r#   r   r   r   r/   r   	false_bitr   r   Zmake_optional_valuer>   r1   r   rz   r3   r   )r   r%   r   ZnonevalZretptrZerrptrr   orelseZjustrz   r   r   r   r   unbox_optionalZ  s2   

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   rI   r   )	r   r%   r   r   rJ   r   r   r   Zslir   r   r   unbox_slice|  s   r   c                 C   s   | j }dD ],}t||}t|tr1zt|t W q ty0 } ztd| dt| d }~ww 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   rd   rc   callr0   )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  s4   


r   c                 C   rO   r   rP   r   r%   r   r   r   r   unbox_string_literal  rS   r   c           	      C   s   |j | }||j |j|d}|j jr7t| j}|j|j	|}|j
| ||}|j j|j| | |S |j}|j
| |S r<   )r(   
make_arrayr"   
enable_nrtr   as_dtyperb   rr   Z
read_constZ	add_constr   Znrt_adapt_ndarray_to_pythonr   r0   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rEj  | nj  | W d   n1 sVw   Y   fdd}	tj|||	dS )zK
    Convert a Py_buffer-providing object to a native array structure.
    Nc                      rv   r   rw   r   rx   r   r   rz     r{   zunbox_buffer.<locals>.cleanupr   )r   r}   
get_bufferr   r   r"   r(   r   rG   r   r   rq   voidptrr   Znrt_adapt_buffer_from_pythonZnumba_buffer_adaptorr   r3   )
r   r%   r   resr   r   r   aryptrrt   rz   r   rx   r   unbox_buffer  s"   
r   c                 C   s  |j | }||j |j}| }|j||jj}|j jr&|j||}n|j	||}z	t
| jj}W n tyB   tj}	Y nw |j|}|jd|j|}	|jt|j||	}
|jj|
dd |jdd W d   n1 sxw   Y  t|j||
dS )zC
    Convert a Numpy array object to a native array structure.
    r   FZlikelyrF   zXcan't unbox array from PyObject into native value.  The object maybe of a different typeNr   )r(   r   r"   rG   rq   r   r   r   Znrt_adapt_ndarray_from_pythonZnumba_array_adaptorr   r   rb   itemsizer	   r   r   r!   r   or_r   r   rH   r   r3   )r   r%   r   r   r   r   rt   errcodeZexpected_itemsizeZitemsize_mismatchrK   r   r   r   unbox_array  s8   

r   c                 C   sN   |j | j}t| D ]\}}|j||}|||}|j ||| q|S )zD
    Convert native array or structure *val* to a tuple object.
    )r   	tuple_newrp   	enumerater"   extract_valuer   Ztuple_setitem)r   r   r   Z	tuple_valirb   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   rc   rd   re   r   r   r0   )r   r   r   rh   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 d   n1 sZw   Y  t| D ]C\}
}|j||
}|||}||j |jj|jdd |jtj| W d   n1 sw   Y  |jdur |j qc|j|j| |}|j||  r|jj|	dd  fdd	}W d   n1 sw   Y  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   rz   O  s   zunbox_tuple.<locals>.cleanupr|   )lenr(   r^   r   r   r"   r   r/   r   Z
tuple_sizer   r   r    r!   r   r   
err_formatr1   true_bitr   Ztuple_getitemr   appendr>   r   rz   Z
make_tupler   r3   )r   r%   r   nvaluesr   is_error_ptrZ	value_ptrZactual_sizeZsize_matchesr   Zeltypeelemr   r>   rz   r   r   r   unbox_tuple'  sN   
	

r   c                 C   s  t |j|j| |}|j}t|j|}|jt|j|\}}| |j	
| W d   n1 s5w   Y  |g |j}|j	|}|jjt|j|dd: t|j|#}	||	j}
||
 || j|
}|j	||	j| W d   n1 sw   Y  W d   n1 sw   Y  |j|| W d   n1 sw   Y  W d   n1 sw   Y  |jj|j| | |j|S )z5
    Convert native list *val* to a list object.
    NTr   )r   ListInstancer(   r"   r   r   r   r   r   r   r   rs   list_newr   	for_rangegetitemindexincref_valuer   rb   list_setitemr1   r   r0   r3   )r   r   r   listr%   r   
has_parentr   r   loopr   itemobjr   r   r   box_listY  s6   
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_modulerj   	typeof_fnr0   )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   r0   r  )r   r   kwargsr   r   r   r   __exit__  s   z_NumbaTypeHelper.__exit__c                 C   s   | j j| j|g}|S r   )r   r   rf   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    	r   c                    s   fdd}t j j j| |\}} jj|dd\}	}
|	 ||_t|j	d} jj
 jd||dd t s}| j||}t j|L j|j}||||  | j|} jj
|jdd  jtj   W d   n1 sw   Y  |jj|jdd	 W d   n1 sw   Y   j| W d   n1 sw   Y  W d   n1 sw   Y  | jr||_ jj
 j jdd  j ||j! W d   n1 sw   Y  |"d  j|j| W d   n	1 sw   Y  |
  jtj W d   n	1 s,w   Y  W d   n	1 s<w   Y   j
 j  jj# j| |j W d   dS 1 scw   Y  dS )
z9
    Construct a new native list from a Python list.
    c                    s   |  |} jjt j|dd  jtj   W d    n1 s)w   Y   jd||} jj|dd#  jtj  j	
dd||  j	|   W d    n1 sdw   Y   j	| d S )NFr   r   rF   z(can't unbox heterogeneous list: %S != %S)r   r"   r   r   r~   r1   r   do_breakr#   r   r   r0   )nthr   expected_typobjtypobjtype_mismatchr   errorptrr   r   r   check_element_type  s(   


	z2_python_list_to_native.<locals>.check_element_typeTr   r   >FN)r   )$r   r   allocate_exr(   r"   r   rs   r   r    r!   r   r#   r   r   r   Zlist_getitemr   r   r   r   rb   r   r1   r   r  setitemr>   r0   	reflectedr   r   r3   object_set_private_datameminfo	set_dirtyr   )r   r%   r   rs   listptrr  r  rJ   r   if_ok	if_not_okr&   r  r  r   r   r   r  r   _python_list_to_native  sV   


 %$r  c              	      s6   j }t jtj}t j j| } j 	} j
t j|S\}}|$ tj j j| |}	||	_| jrD|	_ j|	j| W d   n1 sVw   Y  | t|  ||| W d   n1 sqw   Y  W d   n1 sw   Y   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.
    Nc                          j  d S r   r   Zobject_reset_private_datar   r   r%   r   r   rz     rW   zunbox_list.<locals>.cleanupr   )r   	list_sizer   r   r"   r   r/   r(   r^   object_get_private_datar   r   r   r   from_meminfors   r  r   r1   r>   r  r   r3   )r   r%   r   rs   r  r  rt   has_meminfor   r   rz   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|	\}
}|
w t|j|#}||j}|| || j|}|j||j| W d   n1 sw   Y  t|j|.}|j||j}||}|| || j|}|j|| |j| W d   n1 sw   Y  W d   n1 sw   Y  |D |j|||d t|j|#}||j}|| || j|}|j||j| W d   n	1 sw   Y  W d   n	1 sw   Y  W d   n	1 s+w   Y  |d W d   dS 1 sAw   Y  dS )zD
    Reflect the native list's contents into the Python object.
    Nz2cannot reflect element of reflected container: {}
Fr   z>=r   ) r  rb   format	TypeErrorr   r   r(   r"   r   dirtyr   r   r"  rs   subr#   r   r    r!   r   r   r   r   r   r   r   r   r   Zlist_appendr0   Zlist_setslicer  )r   r   r   rl   r   r%   rs   new_sizediffZ	diff_gt_0Zif_growZ	if_shrinkr   r   r   r   r   r   r   reflect_list  s\   





$r-  c                 C   s>  t j|j|j| |\}}|jj|ddW\}}	|& t|jt	|j
jd}
|j
|}|j}|j
|}|j|
}|jjt|j|ddh\}}| |j||
 W d   n1 sdw   Y  |; |jd||}|jj|dd |jtj| |j
dd |  W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  || j|}|jj|jdd |jtj| W d   n1 sw   Y  |j|j
|jdd W d   n1 sw   Y  | jr||_|jj|j|j|dd |j
||j W d   n	1 s)w   Y  | d |j|j| W d   n	1 sFw   Y  |	 |jtj| W d   n	1 saw   Y  W d   n	1 sqw   Y  |j|j| |jj!"|j| |j W d   dS 1 sw   Y  dS )	z7
    Construct a new native set from a Python set.
    Tr   NFr   rF   zcan't unbox heterogeneous set)Z	do_resize)#r   SetInstancer  r(   r"   r   r   r   r   r    r   ZpyobjZset_iterater>   get_typer3   r~   r1   r#   r   r   rH   r  r   rb   r   Z	add_pyapir  r   r   r  r  r  r   r0   )r   r%   r   rs   setptrr  rJ   instr  r  Z	typobjptrr   r   r  r  Zif_firstZif_not_firstr  r   r   r   r   _python_set_to_nativeA  sr   


,1$r2  c              	      s0   j }t jtj}t j j| } j 	} j
t j|P\}}|! tj j j| |}	| jrA|	_ j|	j| W d   n1 sSw   Y  | t|  ||| W d   n1 snw   Y  W d   n1 s}w   Y   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.
    Nc                      r  r   r   r   r!  r   r   rz     rW   zunbox_set.<locals>.cleanupr   )r   Zset_sizer   r   r"   r   r/   r(   r^   r#  r   r   r   r.  r$  r  r   r1   r>   r2  r   r3   )r   r%   r   rs   r  r0  rt   r%  r   r1  rz   r   r!  r   	unbox_set|  s,   
r3  c              	   C   s   |j }|j|}t|j|}|jj|ddl t|jt	|j
d}| ?}|j|}|jj}	|jj|j| j|	 || j|	}
|j|||
 |j|t	|j
d}|j|| W d   n1 skw   Y  W d   ||fS W d   ||fS 1 sw   Y  ||fS )z9
    Create a Python list from a native set's items.
    Tr   r   r   N)usedr   r   r   r   r"   r   r   r   r    r!   Z_iterater3   entrykeyr(   r   r   rb   r   r   r   r1   )r   payloadr   r   r   rJ   r   r   r   r   r   r   r   r   _native_set_to_python_list  s.   


r8  c              
   C   sB  t |j|j| |}|j}t|j|}|jt|j|j\}}| |j	
| W d   n1 s5w   Y  |> |j}t| ||\}	}
|jj|	dd |j	|
}|j	|
 |j|| W d   n1 snw   Y  W d   n1 s}w   Y  W d   n1 sw   Y  |jj|j| | |j|S )z3
    Convert native set *val* to a set object.
    NTr   )r   r.  r(   r"   r   r   r   r   r   r   r   r7  r8  r   Zset_newr0   r1   r   r3   )r   r   r   r1  r%   r   r   r   r7  rJ   r   r   r   r   box_set  s*   r9  c              	   C   s   | j sdS t|j|j| |}|j}|jj|jddD |j}|j	
| t| ||\}}|jj|dd |j	|| |j	| W d   n1 sMw   Y  |d W d   dS 1 sbw   Y  dS )zC
    Reflect the native set's contents into the Python object.
    NFr   T)r  r   r.  r(   r"   r7  r   r)  r   r   Z	set_clearr8  Z
set_updater0   r  )r   r   r   r1  r7  r%   rJ   r   r   r   r   reflect_set  s   "r:  c                 C   s   |j || |jjS r   )r   Zfrom_native_generatorrr   Zenv_ptrr   r   r   r   box_generator     r;  c                 C   s   |j || S r   )r   Zto_native_generatorr   r   r   r   unbox_generator     r=  c                 C       t | j}|j|j|S r   r   r   rb   r   rc   rd   r   r   r   r   r   r   r   	box_dtype     rB  c                 C   rO   r   rP   r   r   r   r   unbox_dtype   r>  rD  c                 C   r?  r   r@  rA  r   r   r   box_number_class  rC  rE  c                 C   rO   r   rP   r   r   r   r   unbox_number_class
  r>  rF  c                 C   s   |S r   r   r   r   r   r   box_pyobject  s   rG  c                 C      t |S r   r   r   r   r   r   unbox_pyobject     rJ  c           	   	   C   s(  | j d u r	t| |j| }tj|jt|d dd}|j	
| j }|j	|}t|jt|j|J |j	||f}|j	| t|jt|j|  |j	|}|j	| |j|j||| W d    n1 ssw   Y  W d    n1 sw   Y  t|j||j	 dS )NZfnptr)namer   )get_pointerNotImplementedErrorr(   Zget_function_pointer_typer   r   r"   r   r    r   rd   rc   r   r   rf   r0   Zlong_as_voidptrr1   rq   r   r3   r$   )	r   r%   r   Zptrtyr   ZserrM  Zintobjrt   r   r   r   unbox_funcptr  s0   

	rO  c                 C   s(   |j j|  |j|dg|jd}|S )Nr   )rr   )r   Zfrom_native_valuegetr"   r   rr   )r   r   r   outr   r   r   box_deferred1  s
   rR  c                 C   sF   |j |  |}|jj|  }||j| |j}t	||j
|jdS )Nr   )r   Zto_native_valuerP  r(   Zdata_model_managersetr"   Zmake_uninitializedr>   r   r   rz   )r   r%   r   Znative_valuemodelr   r   r   r   unbox_deferred9  s   rU  c                 C   rH  r   rI  r   r   r   r   unbox_dispatcherB  rK  rV  c                 C   s   |j | |S r   )r   r   r   r   r   r   rG  H  s   c                 C   s.   |j dd|  |j| }t|tjdS )NrF   zcan't unbox {!r} typer   )r   rH   r'  r(   Zget_constant_nullr   r   r   )r   r%   r   r   r   r   r   unbox_unsupportedN  s
   rW  c                 C   s&   d| f }|j d| |j  }|S )Nz)cannot convert native %s to Python objectrF   )r   rH   Zget_null_object)r   r   r   rl   r   r   r   r   box_unsupportedU  s   

rX  c                 C   s   | j }|j|j|S r   )r   r   rc   rd   )r   r   r   retvalr   r   r   box_literal\  s   rZ  c                 C   r   r   )r   Znrt_meminfo_as_pyobjectr   r   r   r   box_meminfo_pointere  r   r[  c                 C   s&   |j |}t|j|}t||dS r-   )r   Znrt_meminfo_from_pyobjectr   r~   r"   r   )r   r%   r   r   Zerroredr   r   r   unbox_meminfo_pointerj  s   r\  c                 C   s   t |j tjdS r-   )r   r(   rQ   r   r   r   r   r   r   unbox_typerefp  r<  r]  c                 C   s   t | ||S r   )rX  r   r   r   r   box_LiteralStrKeyDictu  r   r^  c           	   
      s  t  jt jg  fdd fddt B	 fddt |  j j
|
_|dt  j	   W d   n1 sMw   Y  d	}t  j	|   W d   n1 smw   Y  t	
d	 
tj|j d
}t  j	|   W d   n1 sw   Y  |d}t  j	|   W d   n1 sw   Y  t	
d
 
tj|j  j jjd} j|}| t  j	|   W d   n1 sw   Y  |dt  j	   W d   n	1 sw   Y  |dt  j	   W d   n	1 s6w   Y   	
f	dd}|d |d |d   W d   n	1 scw   Y  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   r0   )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"   r1   r   r   r   )r   ra  r   r   r   handle_failure  s   
z7unbox_numpy_random_bitgenerator.<locals>.handle_failurec                    s    j | |}| |S r   )r   rj   r   )r%   attrZattr_objr_  r   r   object_getattr_safely  s   
z>unbox_numpy_random_bitgenerator.<locals>.object_getattr_safelyctypesNZstate_addressstater>   r)   c_void_pc                    s   j | }| t j|   W d    n1 s"w   Y   j |g}t j|   W d    n1 sEw   Y  |  j |}| |d}t j|   W d    n1 svw   Y  td|   	t
j|j d S )Nr>   Zfnptr_)r   rj   r   r   early_exit_if_nullr"   Z
tuple_packr   setattrr   r   r_   r>   )rL  Zinterface_next_fnr   Zinterface_next_fn_castedZinterface_next_fn_casted_value)	r   ct_castct_voidptr_tyctypes_bindingr`  rb  rd  stack
struct_ptrr   r   wire_in_fnptrs  s.   


z7unbox_numpy_random_bitgenerator.<locals>.wire_in_fnptrsZnext_doubleZnext_uint64Znext_uint32r   )r   r   r"   r   r   create_struct_proxyr(   r   rh  ri  r   r   r_   r>   r  r  r   r  r   r   rI   r3   )	r   r%   r   Zinterface_state_addressZinterface_stateZinterface_state_valueZctypes_nameZctypes_modulero  r   )r   ra  rj  rk  rl  r`  rb  r   rd  rm  rn  r   unbox_numpy_random_bitgenerator{  sz   





 arq  bit_generatorc           	   	   C   s   t |jt j}t Y}t | |j|j}|j|d}t 	|j|| |j
t j| W d   n1 s8w   Y  |t|j}||_||_t d}|j|||_|j| W d   n1 sgw   Y  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.
    rr  Nr   )r   r   r"   r   r   rp  r(   r   rj   rh  r1   r   r   _bit_gen_typer>   rr  r   Z	voidptr_tZnrt_meminfo_new_from_pyobjectr  r0   r   rI   r3   )	r   r%   r   r   rm  rn  Zbit_gen_instZunboxedNULLr   r   r   unbox_numpy_random_generator  s$   
ru  c                 C   sR   |j |j| |}|j}t|j|}|j| |j j	|j| | |j
|S r   )r(   r   r"   r   r   r   r   r   r   r0   r3   )r   r   r   r1  r%   r   r   r   r   box_numpy_random_generator  s   rv  N)r  Zllvmliter   Z
numba.corer   r   Znumba.core.pythonapir   r   r   r   Znumba.core.errorsr	   r
   Znumba.core.typing.typeofr   r   r   r   r   Znumba.npr   
contextlibr   r   Booleanr   r'   ZIntegerLiteralZBooleanLiteralr*   Integerr,   r4   Floatr:   r;   ComplexrE   rL   NoneTyperN   EllipsisTyperR   Z
NPDatetimerV   rZ   ZNPTimedeltar\   r]   Z
RawPointerra   Z
EnumMemberri   rk   ZUndefVarrm   ZRecordru   r   ZUnicodeCharSeqr   r   Bytesr   ZCharSeqr   r   Optionalr   r   Z	SliceTyper   ZSliceLiteralr   ZStringLiteralr   Arrayr   ZBufferr   r   TupleZUniTupler   
NamedTupleZNamedUniTupler   Z	BaseTupler   Listr   objectr   r  r&  r-  r2  Setr3  r8  r9  r:  	Generatorr;  r=  ZDTyperB  rD  ZNumberClassrE  rF  ZPyObjectZObjectrG  rJ  ZExternalFunctionPointerrO  ZDeferredTyperR  rU  Z
DispatcherrV  rW  rX  LiteralrZ  ZMemInfoPointerr[  r\  ZTypeRefr]  ZLiteralStrKeyDictr^  ZNumPyRandomBitGeneratorTyperq  rs  ZNumPyRandomGeneratorTyperu  rv  r   r   r   r   <module>   s,   













	





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