
    He:/                    T   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 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      Z"	 	 	 	 ddZ# G d d      Z$ddZ%ddZ&ddZ'ddZ(y)zExecute files of Python code.    )annotationsN)
ModuleSpec)CodeType
ModuleType)Any)env)CoverageException_ExceptionDuringRunNoCodeNoSource)canonical_filenamepython_reported_file)isolate_module)get_python_sourcec                      e Zd ZdZddZy)DummyLoaderzzA shim for the pep302 __loader__, emulating pkgutil.ImpLoader.

    Currently only implements the .fullname attribute
    c                    || _         y )N)fullname)selfr   _argss      3/usr/lib/python3/dist-packages/coverage/execfile.py__init__zDummyLoader.__init__$   s	         N)r   strr   r   returnNone)__name__
__module____qualname____doc__r    r   r   r   r      s    !r   r   c                   	 t         j                  j                  |       }|st	        d|       |j                  }|j                  }|j                  rS| dz   }t         j                  j                  |      }|st	        d| d| dz         |j                  }|j                  }|j                  d      d   }|||fS # t        $ r}t	        t        |            |d}~ww xY w)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)
modulenamespecerrpathnamepackagenamemod_mains         r   find_moduler2   (   s    *~~''
3 )*899{{H))K&&+~~''1"8*B/. MNO  ;;ii((-a0K[$&&#  *s3x c)*s   C 	C$
CC$c                  2    e Zd ZdZdddZd	dZd	dZd	dZy)
PyRunnerzwMulti-stage execution of Python code.

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

    c                z    || _         || _        |d   | _        d | _        d | _        d | _        d | _        d | _        y )Nr   )args	as_modulearg0packager,   r/   loaderr-   )r   r6   r7   s      r   r   zPyRunner.__init__L   s>    	"G	#'&*$(*.'+	r   c                   | j                   rt        j                         }n|t        j                  j	                  | j
                        r| j
                  }nFt        j                  j                  t        j                  j                  | j
                              }t        j                  j	                  t        j                  d         rt        j                         d   d   j                  j                  }t        j                  j                  t        j                  d         }t        j                  j                  t        j                  j                  |            }t        |      }t        |      }||k7  rXd}nUt        j                  d   t        j                  j                  t        j                  d         k(  rt        j                  d= |t        |      t        j                  d<   yy)zsSet sys.path properly.

        This needs to happen before any importing, and without importing anything.
        r   N   )r7   osgetcwdpathisdirr8   abspathdirnamesysinspectstackf_codeco_filenamer   r   )r   path0top_filesys_path_0_abstop_file_dir_abss        r   preparezPyRunner.prepareW   s@    >>IIKEWW]]499% IIEGGOOBGGOODII$>?E77==!% }}r*1-44@@HWW__SXXa[9N!wwrwwx/HI/?N12BC!11
 xx{bggoochhqk::HHQK.u5CHHQK r   c                F   | j                   r| j                  | _        t        | j                        \  }| _        | _        | j
                  | j
                  j                  | _        t        | j                        | _        |J t        j                  j                  |      | _        | j                  x| j                  d<   | _        n<t        j                  j                  | j                        rdD ]  }t        j                  j                  | j                  d|z         }t         j"                  dk\  rt        j                  j                  |      }t        j                  j%                  |      s|| _         n t'        d| j                   d      t)        |      }t*        j,                  j/                  dd|      | _        d	| j
                  _        d
| _        t        d      | _        nt        d      | _        t)        | j                        | _        y)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 )r7   r8   r,   r2   r9   r-   r)   r   r:   r>   r@   rB   r/   r6   rA   joinr   	PYVERSIONexistsr   r   r$   	machineryr   has_location)r   r/   exttry_filenames       r   	_prepare2zPyRunner._prepare2~   s    >>"iiDO0;DOO0L-HdlDIyy$"&))..%doo6DK'''GGOOH5DM'+}}4DIIaL49WW]]499% / Q!ww||DIIzC7GH ==J.#%77??<#@L77>>,/ ,DIQ !B499+QOPP 0=L!++66z4P\6]DI%)DII"DL%j1DK%j1DK(3	r   c                   | j                          t        d      }| j                  j                  d      }| j                  |_        |r|j                  dd |_        | j
                  | j
                  |_        | j                  |_        | j                  | j                  |_
        t        j                  d   |_        |t        j                  d<   | j                  t        _        	 |rt!        | j                        }nt#        | j                        }t-        j.                         }	 t1        ||j2                         t-        jL                  |       y# t$        $ r  t&        $ r:}d| j                   d|j(                  j*                   d| }t%        |      |d}~ww xY w# t4        $ r  t&        $ r{ t        j6                         \  }}}	|J |J |	J t9        |d	d       	 |j:                  J |j:                  j<                  |_        t        j>                  |||	j<                         t        j@                  d
       n# t4        $ r  t&        $ r}t        jB                  jE                  d       t        j6                         \  }
}}|
J |J |J d|_#        |j:                  J |j:                  j<                  |_        t        jH                  |
||j<                         t        jB                  jE                  d       tK        |||	j<                        |d}~ww xY wY w xY w# t-        jL                  |       w xY w)zRun the Python code!rQ   )rO   rP   Nr<   builtinszCouldn't run 'z' as Python code: z: __context__r=   zError in sys.excepthook:
Tz
Original exception was:
)'r^   r   r8   endswith__file__r9   __package__r:   
__loader__r-   __spec__rD   modules__builtins__r6   argvmake_code_from_pycmake_code_from_pyr	   	Exception	__class__r   r>   r?   exec__dict__
SystemExitexc_infogetattr__traceback__tb_next
excepthookexitstderrwrite__suppress_context____excepthook__r
   chdir)r   main_modfrom_pyccodeexcmsgcwdtypr.   tbtyp2err2tb2s                r   runzPyRunner.run   s    	 j)99%%&67 II ( 1 1#2 6H<<##'<<H "kk99  $		H #J 7"*J 99
	2)$))4(3 iik/	x(()\ HHSMs ! 	 	2"499+-?@V@V?WWYZ]Y^_C#C(c1	2  	  '	 <<>LCb?"??"?>!> C-((444$'$5$5$=$=!sC4$ #   I 

  !=>"%,,.dC''''''&,0)))555%)%7%7%?%?"""4s{{;

  !>?)#sBJJ?SHI O'	R HHSMsc   *-E ,F' F$*5FF$'AL5,A
I6L5L.CL))L..L51L8 4L55L8 8MN)F)r6   	list[str]r7   boolr   r   )r   r   )r   r   r   r    r   rM   r^   r   r!   r   r   r4   r4   F   s    
	,%6N)4VXr   r4   c                ^    t        | d      }|j                          |j                          y)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r7   Nr4   rM   r   r6   runners     r   run_python_moduler     s#     dd+F
NN
JJLr   c                ^    t        | d      }|j                          |j                          y)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   s     r   run_python_filer     s#     de,F
NN
JJLr   c                    	 t        |       }t        || dd      S # t        t        f$ r}t        d|  d      |d}~ww xY w)z8Get source from `filename` and make a code object of it.No file to run: 'rU   Nrn   T)dont_inherit)r   OSErrorr   compile)filenamesourcer   s      r   rk   rk   !  sU    A"8, 68V$?? X A*8*A67S@As    A ;A c                   	 t        | d      }|5  |j                  d      }|t        k7  rt        d|dt              t        j                  d|j                  d            d	   }|d
z  }|r|j                  d       n"|j                  d       |j                  d       t        j                  |      }t        |t              sJ 	 ddd       |S # t        $ r}t        d|  d      |d}~ww xY w# 1 sw Y   S xY w)z#Get a code object from a .pyc file.rbr   rU   N   zBad magic number in .pyc file: z != z<Lr   r=   rS   )openr   r   readPYC_MAGIC_NUMBERstructunpackmarshalload
isinstancer   )r   fpycr   magicflags
hash_basedr~   s          r   rj   rj   ,  s    ?Hd# 
 * 		!$$:5)4HXG[\]]dDIIaL1!4T\
IIaL IIaLIIaL ||D!$)))%*( K/  ?(
!453>?*( Ks#   C B:C6	C3C..C36D )r,   r   r   z"tuple[str | None, str, ModuleSpec])r6   r   r   r   )r   r   r   r   ))r    
__future__r   importlib.machineryr$   importlib.utilrE   r   r>   r   rD   r   typesr   r   typingr   coverager   coverage.exceptionsr	   r
   r   r   coverage.filesr   r   coverage.miscr   coverage.pythonr   r%   MAGIC_NUMBERr   r   r2   r4   r   r   rk   rj   r!   r   r   <module>r      s    $ "     	  
 * &   X X C ( -B >>.. ! !''''<{ {|@r   