o
    v&i.                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZmZ ddl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 dd
lmZ eeZejjZG dd dZ d"ddZ!G dd dZ"d#ddZ#d#ddZ$d$ddZ%d$d d!Z&dS )%zExecute files of Python code.    )annotationsN)
ModuleSpec)CodeType
ModuleType)Any)CoverageExceptionNoCodeNoSource_ExceptionDuringRun)canonical_filenamepython_reported_file)isolate_module)get_python_sourcec                   @  s   e Zd ZdZddd	Zd
S )DummyLoaderzzA shim for the pep302 __loader__, emulating pkgutil.ImpLoader.

    Currently only implements the .fullname attribute
    fullnamestr_argsr   returnNonec                 G  s
   || _ d S )N)r   )selfr   r    r   W/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/coverage/execfile.py__init__$   s   
zDummyLoader.__init__N)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   
modulenamer   r   "tuple[str | None, str, ModuleSpec]c              
   C  s   zt j| }W n ty } ztt||d}~ww |s%td| |j}|j}|jrM| d }t j|}|sGtd| d| d |j}|j}|	dd }|||fS )zFind the module named `modulename`.

    Returns the file path of the module, the name of the enclosing
    package, and the spec.
    NzNo module named z	.__main__z; z- is a package and cannot be directly executed.r   )
	importlibutil	find_specImportErrorr	   r   originnamesubmodule_search_locations
rpartition)r   specerrpathnamepackagenamemod_mainr   r   r   find_module(   s.   

r-   c                   @  s:   e Zd ZdZddd	d
ZdddZdddZdddZdS )PyRunnerzwMulti-stage execution of Python code.

    This is meant to emulate real Python execution as closely as possible.

    Fargs	list[str]	as_moduleboolr   r   c                 C  s8   || _ || _|d | _d | _d | _d | _d | _d | _d S )Nr   )r/   r1   arg0packager   r*   loaderr(   )r   r/   r1   r   r   r   r   M   s   

zPyRunner.__init__c                 C  s   t tjddr
d}n| jrt }ntj| jr| j}ntj	tj
| j}tjtjd r]t d d jj}tj	tjd }tj	tj
|}t|}t|}||kr\d}ntjd tj
tjd krotjd= |dur|t|tjd< dS dS )zsSet sys.path properly.

        This needs to happen before any importing, and without importing anything.
        	safe_pathFNr      )getattrsysflagsr1   osgetcwdpathisdirr3   abspathdirnameinspectstackf_codeco_filenamer   r   )r   path0top_filesys_path_0_abstop_file_dir_absr   r   r   prepareX   s*   
zPyRunner.preparec                 C  s"  | j r8| j| _t| j\}| _| _| jdur| jj| _t| j| _|dus'J t	j
|| _| j | jd< | _nQt	j
| jrdD ]}t	j
| jd| }t	j
|}t	j
|r_|| _ n
qAtd| j dt|}tjjdd|d| _d| j_d	| _td| _ntd| _t| j| _dS )
zDo more preparation to run Python code.

        Includes finding the module to run and adjusting sys.argv[0].
        This method is allowed to import code.

        Nr   )z.py.pyc.pyo__main__z!Can't find '__main__' module in '')r$   T )r1   r3   r   r-   r4   r(   r%   r   r5   r<   r>   r@   r*   r/   r?   joinexistsr	   r   r    	machineryr   has_location)r   r*   exttry_filenamer   r   r   	_prepare2   s2   


zPyRunner._prepare2c                 C  s  |    td}| jd}| j|_|r|jdd |_| jdur%| j|_| j|_| j	dur2| j	|_
tjd |_|tjd< | jt_z|rJt| j}nt| j}W n& tyX     tyv } zd| j d|jj d| }t||d}~ww t }zzt||j W n ty     ty)   t \}}}	|dusJ |dusJ |	dusJ t|d	d z|jdusJ |jj|_t|||	j W nY ty     ty! } zEtj !d
 t \}
}}|
dusJ |dusJ |dusJ d|_"|jdusJ |jj|_t#|
||j tj !d t$|||	j|d}~ww t%d Y n	w W t&| dS W t&| dS t&| w )zRun the Python code!rM   )rK   rL   Nr7   builtinszCouldn't run 'z' as Python code: z: __context__zError in sys.excepthook:
Tz
Original exception was:
r8   )'rV   r   r3   endswith__file__r4   __package__r5   
__loader__r(   __spec__r:   modules__builtins__r/   argvmake_code_from_pycmake_code_from_pyr   	Exception	__class__r   r<   r=   exec__dict__
SystemExitexc_infor9   __traceback__tb_next
excepthookstderrwrite__suppress_context____excepthook__r
   exitchdir)r   main_modfrom_pyccodeexcmsgcwdtypr)   tbtyp2err2tb2r   r   r   run   s~   






.zPyRunner.runN)F)r/   r0   r1   r2   r   r   )r   r   )r   r   r   r   r   rJ   rV   r}   r   r   r   r   r.   F   s    

*)r.   r/   r0   r   c                 C      t | dd}|  |  dS )a  Run a Python module, as though with ``python -m name args...``.

    `args` is the argument array to present as sys.argv, including the first
    element naming the module being executed.

    This is a helper for tests, to encapsulate how to use PyRunner.

    Tr1   Nr.   rJ   r}   r/   runnerr   r   r   run_python_module  s   	r   c                 C  r~   )aG  Run a Python file as if it were the main program on the command line.

    `args` is the argument array to present as sys.argv, including the first
    element naming the file being executed.  `package` is the name of the
    enclosing package, if any.

    This is a helper for tests, to encapsulate how to use PyRunner.

    Fr   Nr   r   r   r   r   run_python_file  s   
r   filenamer   c              
   C  sP   zt | }W n ttfy } z	td|  d|d}~ww t|| ddd}|S )z8Get source from `filename` and make a code object of it.No file to run: 'rN   Nre   T)modedont_inherit)r   OSErrorr	   compile)r   sourceru   rt   r   r   r   rb   #  s   rb   c              
   C  s   zt | d}W n ty } z	td|  d|d}~ww |I |d}|tkr3td|dttd|dd	 }|d
@ }|rJ|d n
|d |d t|}t	|t
s`J W d   |S 1 skw   Y  |S )z#Get a code object from a .pyc file.rbr   rN   N   zBad magic number in .pyc file: z != z<Lr   r8      )openr   r   readPYC_MAGIC_NUMBERstructunpackmarshalload
isinstancer   )r   fpycru   magicr;   
hash_basedrt   r   r   r   ra   .  s,   




ra   )r   r   r   r   )r/   r0   r   r   )r   r   r   r   )'r   
__future__r   importlib.machineryr    importlib.utilrB   r   r<   r   r:   r   typesr   r   typingr   coverage.exceptionsr   r   r	   r
   coverage.filesr   r   coverage.miscr   coverage.pythonr   r!   MAGIC_NUMBERr   r   r-   r.   r   r   rb   ra   r   r   r   r   <module>   s4   

 
A

