o
    ۾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 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eej eej!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)eej+dd Z-eej.d d! Z/eej.eej0d"d# Z1eej0d$d% Z2eej3d&d' Z4eej3eej5d(d) Z6eej7d*d+ Z8eej7d,d- Z9eej:d.d/ Z;eej:d0d1 Z<eej=d2d3 Z>eej?d4d5 Z@eej?d6d7 ZAeejBd8d9 ZCeejDd:d; ZEeejDd<d= ZFeejGd>d? ZHeejGd@dA ZIeejJdBdC ZKeejLdDdE ZMeejLdFdG ZNeejOdHdI ZPeejOdJdK ZQeejRdLdM ZSeejTdNdO ZUeejVdPdQ ZWeejXdRdS ZYeejZdTdU Z[eejXdVdW Z\eej]eej^dXdY Z_eej`eejadZd[ Zbeejcd\d] Zdeejed^d_ ZfG d`da daegZhdbdc Zieejeddde Zje	ejedfdg Zkdhdi Zleejmdjdk Zndldm Zoeejmdndo Zpe	ejmdpdq Zqeejrdrds Zseejrdtdu Zteejudvdw Zveejudxdy Zweejxdzd{ Zyeejxd|d} Zzeej{eej|d~d Z}eej{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 Z}dd Zdd Zeejdd Ze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   sN  ||||}t |j|j }t |jt |j|} |j|jj	d}|j
|}t |jt |j|L | jdd }	|j||	}
|j| t |jt |j|
 |j|j|
|f| |j|
 W d    n1 sxw   Y  W d    n1 sw   Y  |j| W d    n1 sw   Y  |j|S )Nnumpynp_   )r   alloca_once_valuebuilderpyapiborrow_none	if_likelyis_not_nullcontextinsert_const_stringmoduleimport_modulenamesplitobject_getattr_stringdecrefstorecall_function_objargsload)typvalcpy_boxing_funcpy_type	py_scalar	np_scalar
numpy_namenumpy_moduletype_strnp_scalar_constructor r1   I/home/ubuntu/.local/lib/python3.10/site-packages/numba/core/new_boxing.pybox_np_scalars   s$   r3   c                 C      |j |S N)r   bool_from_boolr&   r'   r(   r1   r1   r2   box_py_bool&      r8   c                 C      t | ||ttjS r5   )r3   r8   r   py_boolr7   r1   r1   r2   box_np_bool+      r<   c                 C   s<   |j |}t|jd}|jd||}t||j  dS )Nr   !=is_error)	r   object_istruer   Constanttyper   icmp_signedr   c_api_error)r&   objr(   istruezeror'   r1   r1   r2   unbox_boolean0   s   rI   c                 C   s$   |j |j|| | j}|| j|S r5   )r   castr   literal_typer   r7   r1   r1   r2   box_literal_integer9   s   rL   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   number_longif_object_oksignedlong_as_longlonglong_as_ulonglongr"   r#   truncr   r%   rE   )r&   rF   r(   ll_typer'   longobjllvalr1   r1   r2   unbox_integerB   s   rY   c                 C   s   |j ||jj}|j|S r5   )r   sextr   longlonglong_from_longlong)r&   r'   r(   ivalr1   r1   r2   box_py_integerS   s   r^   c                 C   r:   r5   )r3   r^   r   py_intr7   r1   r1   r2   box_np_integerY   r=   r`   c                 C   r4   r5   )r   float_from_doubler7   r1   r1   r2   box_py_float^   r9   rb   c                 C   s.   | j dkr|j||jj}t| ||ttjS )N    )	bitwidthr   fpextr   doubler3   rb   r   py_floatr7   r1   r1   r2   box_np_floatc   s   
rh   c                 C   s\   |j |}|j |}|j | | jdkr#|j||j| }n|}t	||j 
 dS )Nrc   r?   )r   number_floatfloat_as_doubler"   rd   r   fptruncr   rN   r   rE   )r&   rF   r(   fobjdbvalr'   r1   r1   r2   unbox_floatj   s   

rn   c                 C   s>   |j j|j| |d}| tjksJ |j|j}}|j||S Nvalue)	r   make_complexr   r   
py_complexrealimagr   complex_from_doubles)r&   r'   r(   cvalfrealfimagr1   r1   r2   box_py_complexx   s   rz   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| |dS )NPyExc_TypeErrorzconversion to %s failedr?   )r   rr   r   r   rs   r   complex_adaptor_getpointerr   is_falseif_unlikelyerr_set_stringnp_complex64rJ   rt   rg   
np_float32ru   r   	_getvalue)r&   rF   r(   c128okfailedcplxr1   r1   r2   unbox_py_complex   s$   
r   c              	   C   sp  |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||}t|j|j }t|jt|j|[ |j |jjd}|j|}	| jdd }
|j|	|
}t|jt|j| |j|j||f| |j| W d    n1 sw   Y  |j| W d    n1 sw   Y  |j|S )Nrp   r   r   r   )r   rr   r   r   r   re   rt   r   rf   ru   np_complex128rv   r   r   r   r   r   r   r   r   r   r    r!   r#   r$   r"   r%   )r&   r'   r(   rw   rx   ry   r+   r,   r-   r.   r/   r0   r1   r1   r2   box_np_complex   s(   
	r   c                 C   s
   |j  S r5   )r   	make_noner7   r1   r1   r2   box_none   s   
r   c                 C      t |j S r5   r   r   get_dummy_valuer7   r1   r1   r2   
unbox_none      r   c                 C      |j || jS r5   )r   create_np_datetime	unit_coder7   r1   r1   r2   box_npdatetime      r   c                 C      |j |}t||j  dS rM   )r   extract_np_datetimer   rE   r&   rF   r(   r'   r1   r1   r2   unbox_npdatetime      r   c                 C   r   r5   )r   create_np_timedeltar   r7   r1   r1   r2   box_nptimedelta   r   r   c                 C   r   rM   )r   extract_np_timedeltar   rE   r   r1   r1   r2   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   ptrtointr   )r&   r'   r(   ll_intpaddrr1   r1   r2   box_raw_pointer   s   r   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_classr$   )r&   r'   r(   valobjcls_objr1   r1   r2   box_enum   s   r   c                 C   s   |j |d}|| j|S )z=
    Convert an enum member's value to its native value.
    rq   )r   r!   r   r   )r&   rF   r(   r   r1   r1   r2   
unbox_enum   s   r   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(   msgr1   r1   r2   box_undefvar   s   r   c                 C   sH   t t d|jjj}|j|t t d}|j	
||| j|jS )Nrc      )r   rB   IntTyperC   pointeecountr   bitcastPointerTyper   recreate_recordr   env_manager)r&   r'   r(   sizeptrr1   r1   r2   
box_record   s   r   c                    s\   j   j | }tj|}j| }j||} fdd}t	|||dS )Nc                         j   d S r5   r   release_bufferr1   bufr(   r1   r2   cleanup     zunbox_record.<locals>.cleanupr   r@   )
r   alloca_bufferextract_record_datar   is_nullr   r   r   r   r   )r&   rF   r(   r   r@   ltypr'   r   r1   r   r2   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         rp   r   r   )r   py_unicode_1byte_kindpy_unicode_2byte_kindpy_unicode_4byte_kindr   sizeof_unicode_charr   get_constantr   int32r   r   r   r   cstringintpr   rC   	loop_nestr%   gepmulif_thenr   r#   addstring_from_kind_and_data)r&   r'   r(   unicode_kindkindrawptrstrptrfullsizerH   onestepr   idxchstrlenr1   r1   r2   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   string_as_string_size_and_kindr   r   r   r   rB   rC   r   icmp_unsignedselectr   memcpyr   r%   r   not_)r&   rF   r(   ltyr   bufferr   r   is_asciihashvstorage_size	size_fitsnull_stringoutspaceretr1   r1   r2   unbox_unicodecharseq'  s    
r   c                 C   s<   |j |j| |}|j|j|j}|j j|j| | |S r5   )	r   make_helperr   r   bytes_from_string_and_sizedatanitemsnrtr"   )r&   r'   r(   rF   r   r1   r1   r2   	box_bytesE  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 )Nrp   r   r   )r   r   r   r   r   r   r   r   r   r   r   rC   r   r%   r   r   r   r#   r   r   )r&   r'   r(   r   r   r   rH   r   r   r   r   r   r1   r1   r2   box_charseqM  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   string_as_string_and_sizer   r   r   r   rB   rC   r   r   r   r   r   r   r%   r   r   )r&   rF   r(   r   r   r   r   r   r   r   r   r   r1   r1   r2   unbox_charseqd  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 r5   )r   r   r   r   r   r   r   if_elsevalidr   rC   r   r#   r   r%   )r&   r'   r(   optvalr   then	otherwisevalidresr1   r1   r2   box_optional  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 r5   )r   r   r   r1   r(   is_not_nonenativer1   r2   r     s   
"zunbox_optional.<locals>.cleanupr@   r   )r   make_optional_noner   rC   rD   r   r   r   rO   r   	false_bitr   r   make_optional_valuerq   r#   r@   r   r%   r   )r&   rF   r(   nonevalretptrerrptrr   orelsejustr   r   r1   r  r2   unbox_optional  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   slice_as_intsr   r   r   startstopr   r   r   r   )	r&   rF   r(   r  r   r  r  r   slir1   r1   r2   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   r   r   callr"   )r&   r'   r(   	slice_lit
field_name	field_objepy_ctorpy_argsserialized_ctorserialized_argsctorargsrF   r1   r1   r2   box_slice_literal  s4   


r)  c                 C   r   r5   r   r&   rF   r(   r1   r1   r2   unbox_string_literal  r   r+  c           	      C   s   |j | }||j |j|d}|j jr7t| j}|j|j	|}|j
| ||}|j j|j| | |S |j}|j
| |S ro   )r   
make_arrayr   
enable_nrtr   as_dtyper   r   
read_const	add_constr   nrt_adapt_ndarray_to_pythonr   r"   parentincref)	r&   r'   r(   nativearycls	nativearynp_dtypedtypeptrnewaryr2  r1   r1   r2   	box_array  s   r9  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                      r   r5   r   r1   r   r1   r2   r     r   zunbox_buffer.<locals>.cleanupr  )r   r   
get_bufferr   r   r   r   r,  r}   r   r   r   voidptrr-  nrt_adapt_buffer_from_pythonnumba_buffer_adaptorr   r%   )
r&   rF   r(   resr@   r4  r5  aryptrr   r   r1   r   r2   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likelyr{   zXcan't unbox array from PyObject into native value.  The object maybe of a different typeNr?   )r   r,  r   r}   r   r   r;  r-  nrt_adapt_ndarray_from_pythonnumba_array_adaptorr   r.  r   itemsizer	   r   r  rC   r   or_r   r   r   r   r%   )r&   rF   r(   r4  r5  r?  r   errcodeexpected_itemsizeitemsize_mismatchr   r1   r1   r2   unbox_array  s8   

rJ  c                 C   sN   |j | j}t| D ]\}}|j||}|||}|j ||| q|S )zD
    Convert native array or structure *val* to a tuple object.
    )r   	tuple_newr   	enumerater   extract_valuer   tuple_setitem)r&   r'   r(   	tuple_valir   itemrF   r1   r1   r2   	box_tuple=  s   rR  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   rR  r  r"   )r&   r'   r(   r   	tuple_objrF   r1   r1   r2   box_namedtupleL  s   rT  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==FrA  PyExc_ValueErrorz<size mismatch for tuple, expected %d element(s) but got %%zdNTc                     s   t  D ]} |   qd S r5   )reversed)funccleanupsr1   r2   r     s   zunbox_tuple.<locals>.cleanupr   )lenr   r   r   r   r   r  rO   r   
tuple_sizer   r   rB   rC   r   r   
err_formatr#   true_bitrL  tuple_getitemr   appendrq   r@   r   
make_tupler   r%   )r&   rF   r(   nvaluesr   is_error_ptr	value_ptractual_sizesize_matchesrP  eltypeelemr  rq   r   r1   rX  r2   unbox_tupleZ  sN   
	

ri  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.
    NTrA  )r   ListInstancer   r   r2  r   r   r   r   r   r3  r   list_newr   	for_rangegetitemindexincref_valuer   r   list_setitemr#   r   r"   r%   )r&   r'   r(   listrF   r>  
has_parentr   r   looprQ  itemobjr1   r1   r2   box_list  s6   
ru  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 r5   )r(   )selfr(   r1   r1   r2   __init__  s   
z_NumbaTypeHelper.__init__c                 C   sH   | j }|j|jjd}|j|}|j|d}|| _|j	| | S )Nnumbar   )
r(   r   r   r   r   r   r   r!   	typeof_fnr"   )rw  r(   
numba_name	numba_modrz  r1   r1   r2   	__enter__  s   z_NumbaTypeHelper.__enter__c                 O   s   | j }|j| j d S r5   )r(   r   r"   rz  )rw  r(  kwargsr(   r1   r1   r2   __exit__  s   z_NumbaTypeHelper.__exit__c                 C   s   | j j| j|g}|S r5   )r(   r   r$   rz  )rw  rF   r>  r1   r1   r2   r     s   z_NumbaTypeHelper.typeofN)__name__
__module____qualname____doc__rx  r}  r  r   r1   r1   r1   r2   rv    s    	rv  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 )NFrA  r>   r{   z(can't unbox heterogeneous list: %S != %S)r   r   r   r   r   r#   r]  do_breakrD   r   r\  r"   )nthrt  expected_typobjtypobjtype_mismatchr(   errorptrrs  r1   r2   check_element_type  s(   


	z2_python_list_to_native.<locals>.check_element_typeTrA  r   >FN)r3  )$r   rj  allocate_exr   r   r   r   r   rB   rC   r   rD   rv  r   r   list_getitemr   rl  rn  r   r   r@   r#   r]  r  setitemrq   r"   	reflectedr2  r   r%   object_set_private_datameminfo	set_dirtyr   )r&   rF   r(   r   listptrr  r  r   rq  if_ok	if_not_okrH   r  r  rt  r  r1   r  r2   _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 r5   r   object_reset_private_datar1   r(   rF   r1   r2   r   8  r   zunbox_list.<locals>.cleanupr  )r   	list_sizer   r   r   r  rO   r   r   object_get_private_datar   r   r   rj  from_meminfor   r  r2  r#   rq   r  r   r%   )r&   rF   r(   r   r  r  r   has_meminfor   rq  r   r1   r  r2   
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: {}
FrA  z>=r   ) r  r   format	TypeErrorr   rj  r   r   r   dirtyr2  r   r  r   subrD   r   rB   rC   r   r   rl  rm  rn  ro  r   rp  r   list_appendr"   list_setslicer  )r&   r'   r(   r   rq  rF   r   new_sizediff	diff_gt_0if_grow	if_shrinkrs  rQ  rt  r   r1   r1   r2   reflect_listA  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.
    TrA  NFr>   r{   zcan't unbox heterogeneous set)	do_resize)#r   SetInstancer  r   r   r   r   r   r   rB   r   pyobjset_iteraterq   get_typer%   r   r#   rD   r   r]  r   r  r   r   r@   	add_pyapir  r2  r   r  r  r  r   r"   )r&   rF   r(   r   setptrr  r   instr  r  	typobjptrrs  rt  r  r  if_firstif_not_firstr  r  r1   r1   r2   _python_set_to_nativet  sr   


,1$r  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  r5   r  r1   r  r1   r2   r     r   zunbox_set.<locals>.cleanupr  )r   set_sizer   r   r   r  rO   r   r   r  r   r   r   r  r  r  r2  r#   rq   r  r   r%   )r&   rF   r(   r   r  r  r   r  r   r  r   r1   r  r2   	unbox_set  s,   
r  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.
    TrA  r   r   N)usedr   rk  r   r   r   r   r   r   rB   rC   _iterater%   entrykeyr   r   r3  r   r   rp  r   r#   )r&   payloadr(   r   r   r   rn  rs  rP  rQ  rt  r1   r1   r2   _native_set_to_python_list  s.   


r  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.
    NTrA  )r   r  r   r   r2  r   r   r   r   r   r3  r  r  r   set_newr"   r#   r   r%   )r&   r'   r(   r  rF   r>  rr  r   r  r   r   r1   r1   r2   box_set  s*   r  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.
    NFrA  T)r  r   r  r   r   r  r   r  r2  r   	set_clearr  
set_updater"   r  )r&   r'   r(   r  r  rF   r   r   r1   r1   r2   reflect_set  s   "r  c                 C   s   |j || |jjS r5   )r   from_native_generatorr   env_ptrr7   r1   r1   r2   box_generator%     r  c                 C   s   |j || S r5   )r   to_native_generatorr*  r1   r1   r2   unbox_generator)     r  c                 C       t | j}|j|j|S r5   r   r.  r   r   r   r   r&   r'   r(   r6  r1   r1   r2   	box_dtype.     r  c                 C   r   r5   r   r7   r1   r1   r2   unbox_dtype3  r  r  c                 C   r  r5   r  r  r1   r1   r2   box_number_class8  r  r  c                 C   r   r5   r   r7   r1   r1   r2   unbox_number_class=  r  r  c                 C   s   |S r5   r1   r7   r1   r1   r2   box_pyobjectB  s   r  c                 C      t |S r5   r   r*  r1   r1   r2   unbox_pyobjectG     r  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 )Nfnptr)r   r?   )get_pointerNotImplementedErrorr   get_function_pointer_typer   r   r   r   rB   r   r   r   r   r   r$   r"   long_as_voidptrr#   r   r   r%   rE   )	r&   rF   r(   ptrtyr   serr  intobjr   r1   r1   r2   unbox_funcptrM  s0   

	r  c                 C   s(   |j j|  |j|dg|jd}|S )Nr   )r   )r   from_native_valuegetr   rM  r   )r&   r'   r(   outr1   r1   r2   box_deferredd  s
   r  c                 C   sF   |j |  |}|jj|  }||j| |j}t	||j
|jdS )Nr  )r   to_native_valuer  r   data_model_managersetr   make_uninitializedrq   r   r@   r   )r&   rF   r(   native_valuemodelr>  r1   r1   r2   unbox_deferredl  s   r  c                 C   r  r5   r  r*  r1   r1   r2   unbox_dispatcheru  r  r  c                 C   s   |j | |S r5   )r   r3  r7   r1   r1   r2   r  {  s   c                 C   s.   |j dd|  |j| }t|tjdS )Nr{   zcan't unbox {!r} typer?   )r   r   r  r   get_constant_nullr   r   r]  )r&   rF   r(   r>  r1   r1   r2   unbox_unsupported  s
   r  c                 C   s&   d| f }|j d| |j  }|S )Nz)cannot convert native %s to Python objectr{   )r   r   get_null_object)r&   r'   r(   r   r>  r1   r1   r2   box_unsupported  s   

r  c                 C   s   | j }|j|j|S r5   )r  r   r   r   )r&   r'   r(   retvalr1   r1   r2   box_literal  s   r  c                 C   r4   r5   )r   nrt_meminfo_as_pyobjectr7   r1   r1   r2   box_meminfo_pointer  r9   r  c                 C   s&   |j |}t|j|}t||dS rM   )r   nrt_meminfo_from_pyobjectr   r   r   r   )r&   rF   r(   r>  erroredr1   r1   r2   unbox_meminfo_pointer  s   r  c                 C   s   t |j tjdS rM   )r   r   r   r   r  r7   r1   r1   r2   unbox_typeref  r  r  c                 C   s   t | ||S r5   )r  r7   r1   r1   r2   box_LiteralStrKeyDict  r9   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 r5   )r   r"   )_refr(   
extra_refsr1   r2   clear_extra_refs  s   z9unbox_numpy_random_bitgenerator.<locals>.clear_extra_refsc                      s    j tj   d S r5   )r   r#   r   r]  r1   )r(   r  rc  r1   r2   handle_failure  s   
z7unbox_numpy_random_bitgenerator.<locals>.handle_failurec                    s    j | |}| |S r5   )r   r!   r_  )rF   attrattr_objr  r1   r2   object_getattr_safely  s   
z>unbox_numpy_random_bitgenerator.<locals>.object_getattr_safelyctypesNstate_addressstaterq   rJ   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 slw   Y  td|   	t
j|j d S )Nrq   fnptr_)r   r!   r_  r   early_exit_if_nullr   
tuple_packr  setattrr   r   r   rq   )r   interface_next_fnr(  interface_next_fn_castedinterface_next_fn_casted_value)	r(   ct_castct_voidptr_tyctypes_bindingr  r   r  stack
struct_ptrr1   r2   wire_in_fnptrs  s*   
z7unbox_numpy_random_bitgenerator.<locals>.wire_in_fnptrsnext_doublenext_uint64next_uint32r?   )r   r   r   r  r   create_struct_proxyr   r2  r	  r  r   r   r   rq   r   r   r   r   r_  r   r   r%   )	r&   rF   r(   interface_state_addressinterface_stateinterface_state_valuectypes_namectypes_moduler  r1   )r(   r  r  r  r  r  r   rc  r  r  r  r2   unbox_numpy_random_bitgenerator  sz   





_r  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.
    r  Nr?   )r   r   r   r  r   r  r   r   r!   r	  r#   r]  r   _bit_gen_typerq   r  r2  	voidptr_tnrt_meminfo_new_from_pyobjectr  r"   r   r   r%   )	r&   rF   r(   rc  r  r  bit_gen_instunboxedNULLr1   r1   r2   unbox_numpy_random_generator)  s$   
r&  c                 C   sR   |j |j| |}|j}t|j|}|j| |j j	|j| | |j
|S r5   )r   r   r   r2  r   r   r   r3  r   r"   r%   )r&   r'   r(   r  rF   r>  r1   r1   r2   box_numpy_random_generatorJ  s   r'  N)r  llvmliter   
numba.corer   r   numba.core.pythonapir   r   r   r   numba.core.errorsr	   r
   numba.core.typing.typeofr   r   r  r   r   numba.npr   
contextlibr   r   r3   PythonBooleanr8   NumPyBooleanr<   rI   PythonIntegerLiteralPythonBooleanLiteralNumPyIntegerLiteralNumPyBooleanLiteralrL   PythonIntegerNumPyIntegerrY   r^   r`   PythonFloatrb   
NumPyFloatrh   rn   PythonComplexrz   NumPyComplexr   r   NoneTyper   EllipsisTyper   
NPDatetimer   r   NPTimedeltar   r   
RawPointerr   
EnumMemberr   r   UndefVarr   Recordr   r   UnicodeCharSeqr   r   Bytesr   CharSeqr   r   Optionalr  r  	SliceTyper  SliceLiteralr)  StringLiteralr+  Arrayr9  Bufferr@  rJ  TupleUniTuplerR  
NamedTupleNamedUniTuplerT  	BaseTupleri  Listru  objectrv  r  r  r  r  Setr  r  r  r  	Generatorr  r  DTyper  r  NumberClassr  r  PyObjectObjectr  r  ExternalFunctionPointerr  DeferredTyper  r  
Dispatcherr  r  r  Literalr  MemInfoPointerr  r  TypeRefr  LiteralStrKeyDictr  NumPyRandomBitGeneratorTyper  r   NumPyRandomGeneratorTyper&  r'  r1   r1   r1   r2   <module>   sJ   













	





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