o
    ge/                     @  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mZ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ListOptionalTuple)env)CoverageException_ExceptionDuringRunNoCodeNoSource)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   Q/var/www/html/bloggers_ems/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[Optional[str], 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.   

r1   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   )r3   r5   arg0packager!   r.   loaderr,   )r   r3   r5   r   r   r   r   L   s   

zPyRunner.__init__c                 C  s   | j rt }ntj| jr| j}ntjtj| j}tjtjd rSt	
 d d jj}tjtjd }tjtj|}t|}t|}||krRd}ntjd tjtjd kretjd= |durrt|tjd< dS dS )zsSet sys.path properly.

        This needs to happen before any importing, and without importing anything.
        r   N   )r5   osgetcwdpathisdirr7   abspathdirnamesysinspectstackf_codeco_filenamer   r   )r   path0top_filesys_path_0_abstop_file_dir_absr   r   r   prepareW   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< | _nUt	j
| jrdD ]"}t	j
| jd| }tjdkrXt	j
|}t	j
|rc|| _ 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 )r5   r7   r!   r1   r8   r,   r)   r   r9   r<   r>   r@   r.   r3   r?   joinr
   	PYVERSIONexistsr   r   r$   	machineryr   has_location)r   r.   exttry_filenamer   r   r   	_prepare2~   s4   



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!rN   )rL   rM   Nr:   builtinszCouldn't run 'z' as Python code: z: __context__zError in sys.excepthook:
Tz
Original exception was:
r;   )'r[   r   r7   endswith__file__r8   __package__r9   
__loader__r,   __spec__rB   modules__builtins__r3   argvmake_code_from_pycmake_code_from_pyr   	Exception	__class__r   r<   r=   exec__dict__
SystemExitexc_infogetattr__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)r3   r4   r5   r6   r   r   )r   r   )r   r   r   r    r   rK   r[   r   r   r   r   r   r2   F   s    

'+r2   r3   r4   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r5   Nr2   rK   r   r3   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  sL   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: 'rR   Nrj   T)dont_inherit)r   OSErrorr   compile)r   sourcer{   r   r   r   rg   !  s   rg   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   rR   N   zBad magic number in .pyc file: z != z<Lr   r;   rP   )openr   r   readPYC_MAGIC_NUMBERstructunpackmarshalload
isinstancer   )r   fpycr{   magicflags
hash_basedrz   r   r   r   rf   ,  s,   




rf   )r!   r   r   r"   )r3   r4   r   r   )r   r   r   r   ),r    
__future__r   importlib.machineryr$   importlib.utilrC   r   r<   r   rB   r   typesr   r   typingr   r   r   r	   coverager
   coverage.exceptionsr   r   r   r   coverage.filesr   r   coverage.miscr   coverage.pythonr   r%   MAGIC_NUMBERr   r   r1   r2   r   r   rg   rf   r   r   r   r   <module>   s6   
	 
?

