B
    `4                 @   s  d 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
 ddl	mZmZmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ eeZG d
d deZerdd Zndd ZG dd deZdd Zdd Z dd Z!dd Z"dS )zExecute files of Python code.    N)env)BUILTINS)PYC_MAGIC_NUMBERimpimportlib_util_find_spec)canonical_filenamepython_reported_file)CoverageExceptionExceptionDuringRunNoCodeNoSourceisolate_module)compile_unicode)get_python_sourcec               @   s   e Zd ZdZdd ZdS )DummyLoaderzzA shim for the pep302 __loader__, emulating pkgutil.ImpLoader.

    Currently only implements the .fullname attribute
    c             G   s
   || _ d S )N)fullname)selfr   _args r   Y/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/execfile.py__init__   s    zDummyLoader.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c          
   C   s   yt | }W n. tk
r: } ztt|W dd}~X Y nX |sNtd| f |j}|j}|jr| d }t |}|std|| f |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 %rz	.__main__zCNo module named %s; %r is a package and cannot be directly executed.r   )r   ImportErrorr   stroriginnamesubmodule_search_locations
rpartition)
modulenamespecerrpathnamepackagenameZmod_mainr   r   r   find_module"   s&    r'   c          
   C   s
  d}t  t  }}zyd| krF| dd\}}t|||dg}|j}nd|  }}d}t||\}}}	|dkr|dkrtd|  |dkr| }d}t|||dg}|j}t||\}}}	W n. tk
r }
 ztt	|
W dd}
~
X Y nX W d| r|
  X ||dfS )zFind the module named `modulename`.

        Returns the file path of the module, the name of the enclosing
        package, and None (where a spec would have been).
        Nr      __path__z"module does not live in a file: %r__main__)globalslocalsrsplit
__import__r)   r   r'   r   r   r   close)r"   ZopenfileZglolocr&   r   packageZ
searchpathr%   _r$   r   r   r   r'   >   s0    

"
c               @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )PyRunnerzwMulti-stage execution of Python code.

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

    Fc             C   s8   || _ || _|d | _d  | _ | _ | _ | _| _d S )Nr   )args	as_modulearg0r1   r"   r%   loaderr#   )r   r4   r5   r   r   r   r   o   s    
zPyRunner.__init__c             C   s   | j rtjjrt }qJd}n,tj| jr4| j}ntj	tj
| j}tjtjd rt d d jj}tj	tjd }tj	tj
|}t|}t|}||krd}n$tjd tj
tjd krtjd= |dk	rt|tjd< dS )zsSet sys.path properly.

        This needs to happen before any importing, and without importing anything.
         r   Nr(   )r5   r   
PYBEHAVIORZactual_syspath0_dash_mosgetcwdpathisdirr6   abspathdirnamesysinspectstackf_codeco_filenamer   r   )r   Zpath0Ztop_fileZsys_path_0_absZtop_file_dir_absr   r   r   preparev   s&    
zPyRunner.preparec             C   sH  | j rd| j| _t| j\}| _| _| jdk	r6| jj| _t| j| _t	j
|| _| j | jd< | _nt	j
| jr&xBdD ],}t	j
| jd| }t	j
|rz|| _P qzW td| j tjrt	j
| j| _yddl}W n tk
r   Y n&X t|}|jjdd|d| _d| j_d| _td| _ntjr8t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.pyz.pycz.pyor*   z$Can't find '__main__' module in '%s')r   Tr8   )r5   r6   r"   r'   r1   r#   r   r   r7   r;   r=   r?   r%   r4   r>   joinexistsr   r   PY2importlib.machineryr   r   	machinery
ModuleSpechas_locationPY3)r   r%   extZtry_filename	importlibr   r   r   	_prepare2   s:    



zPyRunner._prepare2c             C   s(  |    td}| jd}| j|_|r:|jdd |_| jdk	rL| j|_| j|_	| j
dk	rf| j
|_t|_|tjd< | jt_y|rt| j}n
t| j}W nN tk
r    Y n: tk
r } zd}t|j| j|dW dd}~X Y nX t }z yt||j W n tk
r"    Y n tk
r   t \}}}	t|dd y*t|drd|jj |_t!|||	j  W n tk
r    Y n tk
r   tj"#d	 t \}
}}d
|_$t|dr|jj |_t%|
||j  tj"#d t&|||	j Y nX t'd Y nX W dt(| X dS )zRun the Python code!r*   )z.pycz.pyoNr9   zICouldn't run '{filename}' as Python code: {exc.__class__.__name__}: {exc})filenameexc__context____traceback__zError in sys.excepthook:
Tz
Original exception was:
r(   ))rQ   types
ModuleTyper6   endswith__file__r1   __package__r7   
__loader__r#   __spec__r   __builtins__rA   modulesr4   argvmake_code_from_pycmake_code_from_pyr	   	Exceptionformatr;   r<   exec__dict__
SystemExitexc_infogetattrhasattrrU   tb_next
excepthookstderrwrite__suppress_context____excepthook__r
   exitchdir)r   Zmain_modZfrom_pyccoderS   msgcwdtypr$   tbZtyp2Zerr2Ztb2r   r   r   run   s`    



&

zPyRunner.runN)F)r   r   r   r   r   rF   rQ   rw   r   r   r   r   r3   i   s
   
).r3   c             C   s    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)r3   rF   rw   )r4   runnerr   r   r   run_python_module"  s    	ry   c             C   s    t | dd}|  |  dS )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.

    F)r5   N)r3   rF   rw   )r4   rx   r   r   r   run_python_file0  s    
rz   c          	   C   sB   yt | }W n$ ttfk
r0   td|  Y nX t|| d}|S )z8Get source from `filename` and make a code object of it.zNo file to run: '%s'rd   )r   IOErrorr   r   )rR   sourcerr   r   r   r   ra   ?  s    ra   c          	   C   s   yt | d}W n  tk
r.   td|  Y nX | |d}|tkrXtd|td}tjjrt	
d|dd }|d@ }|r|d	 d
}|r|d tjjr|d t|}W dQ R X |S )z#Get a code object from a .pyc file.rbzNo file to run: '%s'   z'Bad magic number in .pyc file: {} != {}Tz<Lr   r(      FN)openr{   r   readr   rc   r   r:   Zhashed_pyc_pep552structunpackZsize_in_pycmarshalload)rR   ZfpycmagicZ
date_basedflags
hash_basedrr   r   r   r   r`   K  s*    



r`   )#r   rB   r   r;   r   rA   rV   Zcoverager   Zcoverage.backwardr   r   r   r   Zcoverage.filesr   r   Zcoverage.miscr	   r
   r   r   r   Zcoverage.phystokensr   Zcoverage.pythonr   objectr   r'   r3   ry   rz   ra   r`   r   r   r   r   <module>   s0   	
+ :