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 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)pyapibool_from_booltypvalc r   I/home/ubuntu/.local/lib/python3.10/site-packages/numba/core/old_boxing.pybox_bool      r   c                 C   s<   |j |}t|jd}|jd||}t||j  dS )Nr   !=is_error)	r   object_istruer   Constanttypebuildericmp_signedr   c_api_error)r   objr   istruezeror   r   r   r   unbox_boolean   s   r*   c                 C   s$   |j |j|| | j}|| j|S r   )contextcastr$   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$   sextr   longlonglong_from_longlongzext	ulonglonglong_from_ulonglong)r   r   r   ivalullvalr   r   r   box_integer(   s
   r8   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_okr/   long_as_longlonglong_as_ulonglongdecrefstoretruncr   loadr&   )r   r'   r   ll_typer   longobjllvalr   r   r   unbox_integer1   s   rG   c                 C   s<   | t jkr|j||jj}n	| t jksJ |}|j|S r   )r   float32r$   fpextr   doublefloat64float_from_double)r   r   r   dbvalr   r   r   	box_floatA   s
   
rN   c                 C   sj   |j |}|j |}|j | | tjkr#|j||j	| }n	| tj
ks*J |}t||j  dS r9   )r   number_floatfloat_as_doubler@   r   rH   r$   fptruncr+   r:   rK   r   r&   )r   r'   r   fobjrM   r   r   r   r   unbox_floatJ   s   

rS   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   	complex64rI   realr   rJ   imag
complex128complex_from_doubles)r   r   r   cvalfrealfimagr   r   r   box_complexX   s   
r`   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+   rW   r$   r   r[   r   complex_adaptor_getpointerr   is_falseif_unlikelyerr_set_stringrX   r,   rY   rK   rH   rZ   r   	_getvalue)r   r'   r   c128okfailed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   create_np_datetime	unit_coder   r   r   r   box_npdatetime      rw   c                 C      |j |}t||j  dS r9   )r   extract_np_datetimer   r&   r   r'   r   r   r   r   r   unbox_npdatetime      r|   c                 C   rt   r   )r   create_np_timedeltarv   r   r   r   r   box_nptimedelta   rx   r   c                 C   ry   r9   )r   extract_np_timedeltar   r&   r{   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$   ptrtointr   )r   r   r   ll_intpaddrr   r   r   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_classcall_function_objargs)r   r   r   valobjcls_objr   r   r   box_enum   s   r   c                 C   s   |j |d}|| j|S )z=
    Convert an enum member's value to its native value.
    rV   )r   object_getattr_stringr   r   )r   r'   r   r   r   r   r   
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   msgr   r   r   box_undefvar   s   r   c                 C   sH   t t d|jjj}|j|t t d}|j	
||| j|jS )N       )r   r"   IntTyper#   pointeecountr$   bitcastPointerTyper   recreate_recordr   env_manager)r   r   r   sizeptrr   r   r   
box_record   s   r   c                    s\   j   j | }tj|}j| }j||} fdd}t	|||dS )Nc                         j   d S r   r   release_bufferr   bufr   r   r   cleanup      zunbox_record.<locals>.cleanupr   r    )
r   alloca_bufferextract_record_datar   is_nullr$   r+   r   r   r   )r   r'   r   r   r    ltypr   r   r   r   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)         rU   r   r   )r   py_unicode_1byte_kindpy_unicode_2byte_kindpy_unicode_4byte_kindr   sizeof_unicode_charr+   get_constantr   int32r   alloca_once_valuer$   r   cstringintpr   r#   	loop_nestrC   gepmulif_thenis_not_nullrA   addstring_from_kind_and_data)r   r   r   unicode_kindkindrawptrstrptrfullsizer)   onestepr   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   string_as_string_size_and_kindr   	if_likelyr$   r   r"   r#   r   icmp_unsignedselectr   memcpyr   rC   r   not_)r   r'   r   ltyri   bufferr   r   is_ascii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<\}	|j|j||	g}
|jt |j|
 |j|j|	|| W d    n1 sew   Y  W d    n1 stw   Y  |j|}|j||S )NrU   r   r   )r   r   r$   r   r   r   r+   r   r   r   r   r#   r   rC   r   r   r   rA   r   r   )r   r   r   r   r   r   r)   r   r   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   string_as_string_and_sizer   r   r$   r   r"   r#   r   r   r   r   r   r   rC   r   r   )r   r'   r   r   ri   r   r   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_elsevalidr   r#   r   rA   rm   rC   )r   r   r   optvalr   then	otherwise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   r   r   r   is_not_nonenativer   r   r   q  s   
"zunbox_optional.<locals>.cleanupr    r   )r+   make_optional_noner$   r#   r%   r   r   r   r;   r   	false_bitr   r   make_optional_valuerV   rA   r    r   rC   r   )r   r'   r   nonevalretptrerrptrr   orelsejustr   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   slice_as_intsr+   r   r$   startstopr   r   rg   r   )	r   r'   r   r   ri   r   r   r   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   r   r   callr@   )r   r   r   	slice_lit
field_name	field_objepy_ctorpy_argsserialized_ctorserialized_args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 rT   )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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                      r   r   r   r   r   r   r   r     r   zunbox_buffer.<locals>.cleanupr   )r   r   
get_bufferr   r   r$   r+   r  rc   r   r   r   voidptrr  nrt_adapt_buffer_from_pythonnumba_buffer_adaptorr   rC   )
r   r'   r   resr    r  r  aryptrr   r   r   r   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likelyra   zXcan't unbox array from PyObject into native value.  The object maybe of a different typeNr   )r+   r  r$   rc   r   r   r%  r  nrt_adapt_ndarray_from_pythonnumba_array_adaptorr   r  r   itemsizer	   r   r   r#   r   or_r   r   rf   r   rC   )r   r'   r   r  r  r)  r   errcodeexpected_itemsizeitemsize_mismatchrj   r   r   r   unbox_array  s8   

r4  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   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   	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+  PyExc_ValueErrorz<size mismatch for tuple, expected %d element(s) but got %%zdNTc                     s   t  D ]} |   qd S r   )reversed)funccleanupsr   r   r   O  s   zunbox_tuple.<locals>.cleanupr   )lenr+   r   r   r   r$   r   r;   r   
tuple_sizer   r   r"   r#   r   r   
err_formatrA   true_bitr6  tuple_getitemr   appendrV   r    r   
make_tupler   rC   )r   r'   r   nvaluesr   is_error_ptr	value_ptractual_sizesize_matchesr:  eltypeelemr   rV   r   r   rB  r   unbox_tuple'  sN   
	

rS  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  r   list_newr   	for_rangegetitemindexincref_valuer   r   list_setitemrA   r   r@   rC   )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 )Nnumbar   )
r   r+   insert_const_stringr$   moduler   import_moduler   	typeof_fnr@   )ra  r   
numba_name	numba_modrg  r   r   r   	__enter__  s   z_NumbaTypeHelper.__enter__c                 O   s   | j }|j| j d S r   )r   r   r@   rg  )ra  r  kwargsr   r   r   r   __exit__  s   z_NumbaTypeHelper.__exit__c                 C   s   | j j| j|g}|S r   )r   r   r   rg  )ra  r'   r(  r   r   r   r     s   z_NumbaTypeHelper.typeofN)__name__
__module____qualname____doc__rb  rj  rl  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   ra   z(can't unbox heterogeneous list: %S != %S)r   r$   r   r   r   rA   rG  do_breakr%   r   rF  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   >FN)r  )$r   rT  allocate_exr+   r$   r   r   r   r"   r#   r   r%   r`  r   r   list_getitemr   rV  rX  r   r   r    rA   rG  rq  setitemrV   r@   	reflectedr  r   rC   object_set_private_datameminfo	set_dirtyr   )r   r'   r   r   listptrrw  rx  ri   r[  if_ok	if_not_okr)   rr  rs  r^  r   r   rv  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   object_reset_private_datar   r   r'   r   r   r     rx   zunbox_list.<locals>.cleanupr   )r   	list_sizer   r   r$   r   r;   r+   r   object_get_private_datar   r   r   rT  from_meminfor   r}  r  rA   rV   r  r   rC   )r   r'   r   r   rw  r  r   has_meminfor   r[  r   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}  r   format	TypeErrorr   rT  r+   r$   r   dirtyr  r   r  r   subr%   r   r"   r#   r   r   rV  rW  rX  rY  r   rZ  r   list_appendr@   list_setslicer  )r   r   r   r   r[  r'   r   new_sizediff	diff_gt_0if_grow	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   ra   zcan't unbox heterogeneous set)	do_resize)#r   SetInstancerz  r+   r$   r   r   r   r   r"   r   pyobjset_iteraterV   get_typerC   r   rA   r%   r   rG  rf   rq  r   r   r    	add_pyapir}  r  r   r~  r  r  r   r@   )r   r'   r   r   setptrrw  ri   instr  r  	typobjptrr]  r^  rt  rs  if_firstif_not_firstru  r   r   r   r   _python_set_to_nativeA  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  r   r  r   r  r   r   r     rx   zunbox_set.<locals>.cleanupr   )r   set_sizer   r   r$   r   r;   r+   r   r  r   r   r   r  r  r}  r  rA   rV   r  r   rC   )r   r'   r   r   rw  r  r   r  r   r  r   r   r  r   	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.
    Tr+  r   r   N)usedr   rU  r   r   r$   r   r   r   r"   r#   _iteraterC   entrykeyr+   r   r  r   r   rZ  r   rA   )r   payloadr   r   r   ri   rX  r]  r:  r;  r^  r   r   r   _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.
    NTr+  )r   r  r+   r$   r  r   r   r   r   r   r  r  r  r   set_newr@   rA   r   rC   )r   r   r   r  r'   r(  r\  r   r  ri   r   r   r   r   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.
    NFr+  T)r}  r   r  r+   r$   r  r   r  r  r   	set_clearr  
set_updater@   r  )r   r   r   r  r  r'   ri   r   r   r   r   reflect_set  s   "r  c                 C   s   |j || |jjS r   )r   from_native_generatorr   env_ptrr   r   r   r   box_generator     r  c                 C   s   |j || S r   )r   to_native_generatorr  r   r   r   unbox_generator     r  c                 C       t | j}|j|j|S r   r   r  r   r   r   r   r   r   r   r   r   r   r   	box_dtype     r  c                 C   ro   r   rp   r   r   r   r   unbox_dtype   r  r  c                 C   r  r   r  r  r   r   r   box_number_class  r  r  c                 C   ro   r   rp   r   r   r   r   unbox_number_class
  r  r  c                 C   s   |S r   r   r   r   r   r   box_pyobject  s   r  c                 C      t |S r   r   r  r   r   r   unbox_pyobject     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)namer   )get_pointerNotImplementedErrorr+   get_function_pointer_typer   r   r$   r   r"   r   r   r   r   r   r   r@   long_as_voidptrrA   r   r   rC   r&   )	r   r'   r   ptrtyr   serr  intobjr   r   r   r   unbox_funcptr  s0   

	r  c                 C   s(   |j j|  |j|dg|jd}|S )Nr   )r   )r   from_native_valuegetr$   r7  r   )r   r   r   outr   r   r   box_deferred1  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_uninitializedrV   r   r    r   )r   r'   r   native_valuemodelr(  r   r   r   unbox_deferred9  s   r  c                 C   r  r   r  r  r   r   r   unbox_dispatcherB  r  r  c                 C   s   |j | |S r   )r   r  r   r   r   r   r  H  s   c                 C   s.   |j dd|  |j| }t|tjdS )Nra   zcan't unbox {!r} typer   )r   rf   r  r+   get_constant_nullr   r   rG  )r   r'   r   r(  r   r   r   unbox_unsupportedN  s
   r  c                 C   s&   d| f }|j d| |j  }|S )Nz)cannot convert native %s to Python objectra   )r   rf   get_null_object)r   r   r   r   r(  r   r   r   box_unsupportedU  s   

r  c                 C   s   | j }|j|j|S r   )r  r   r   r   )r   r   r   retvalr   r   r   box_literal\  s   r  c                 C   r   r   )r   nrt_meminfo_as_pyobjectr   r   r   r   box_meminfo_pointere  r   r  c                 C   s&   |j |}t|j|}t||dS r9   )r   nrt_meminfo_from_pyobjectr   r   r$   r   )r   r'   r   r(  erroredr   r   r   unbox_meminfo_pointerj  s   r  c                 C   s   t |j tjdS r9   )r   r+   rq   r   r   r   r   r   r   unbox_typerefp  r  r  c                 C   s   t | ||S r   )r  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   r@   )_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$   rA   r   rG  r   )r   r  rM  r   r   handle_failure  s   
z7unbox_numpy_random_bitgenerator.<locals>.handle_failurec                    s    j | |}| |S r   )r   r   rI  )r'   attrattr_objr  r   r   object_getattr_safely  s   
z>unbox_numpy_random_bitgenerator.<locals>.object_getattr_safelyctypesNstate_addressstaterV   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 )NrV   fnptr_)r   r   rI  r   early_exit_if_nullr$   
tuple_packr  setattrr   r   r   rV   )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_ptrr   r   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+   r  r  r  r   r   r   rV   rd  re  r   rf  rI  r   rg   rC   )	r   r'   r   interface_state_addressinterface_stateinterface_state_valuectypes_namectypes_moduler  r   )r   r  r  r  r  r  r  rM  r  r   r  r   unbox_numpy_random_bitgenerator{  sz   





 ar  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  rA   rG  r   _bit_gen_typerV   r  r  	voidptr_tnrt_meminfo_new_from_pyobjectr  r@   r   rg   rC   )	r   r'   r   rM  r   r  bit_gen_instunboxedNULLr   r   r   unbox_numpy_random_generator  s$   
r  c                 C   sR   |j |j| |}|j}t|j|}|j| |j j	|j| | |j
|S r   )r+   r   r$   r  r   r   r   r  r   r@   rC   )r   r   r   r  r'   r(  r   r   r   box_numpy_random_generator  s   r  N)rp  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   Booleanr   r*   IntegerLiteralBooleanLiteralr.   Integerr8   rG   FloatrN   rS   Complexr`   rl   NoneTypern   EllipsisTyperr   
NPDatetimerw   r|   NPTimedeltar   r   
RawPointerr   
EnumMemberr   r   UndefVarr   Recordr   r   UnicodeCharSeqr   r   Bytesr   CharSeqr   r   Optionalr   r   	SliceTyper   SliceLiteralr  StringLiteralr  Arrayr#  Bufferr*  r4  TupleUniTupler<  
NamedTupleNamedUniTupler>  	BaseTuplerS  Listr_  objectr`  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  r   r   r   r   <module>   s,   













	





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