ó
…¾^Yc           @   s  d  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 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 d e f d	 „  ƒ  YZ e	 rÞ d
 „  Z n	 d „  Z d „  Z d d d d „ Z d „  Z d „  Z d S(   s   Execute files of Python code.iÿÿÿÿN(   t   BUILTINS(   t   PYC_MAGIC_NUMBERt   impt   importlib_util_find_spec(   t   CoverageExceptiont   ExceptionDuringRunt   NoCodet   NoSourcet   isolate_module(   t   compile_unicode(   t   get_python_sourcet   DummyLoaderc           B   s   e  Z d  Z d „  Z RS(   sz   A shim for the pep302 __loader__, emulating pkgutil.ImpLoader.

    Currently only implements the .fullname attribute
    c         G   s   | |  _  d  S(   N(   t   fullname(   t   selfR   t   _args(    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyt   __init__   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyR      s   c         C   sì   y t  |  ƒ } Wn% t k
 r7 } t t | ƒ ƒ ‚ n X| sT t d |  f ƒ ‚ n  | j } | j } | j d ƒ rÏ |  j d ƒ rÏ |  d } t  | ƒ } | sº t d | |  f ƒ ‚ n  | j } | j } n  | j d ƒ d } | | f S(   s‰   Find the module named `modulename`.

        Returns the file path of the module, and the name of the enclosing
        package.
        s   No module named %rs   __init__.pyR   s	   .__main__sC   No module named %s; %r is a package and cannot be directly executedt   .i    (   R   t   ImportErrorR   t   strt   origint   namet   endswitht
   rpartition(   t
   modulenamet   spect   errt   pathnamet   packagenamet   mod_main(    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyt   find_module   s&    		
	c         C   sb  d } t ƒ  t ƒ  } } z(yü d |  k rg |  j d d ƒ \ } } t | | | d g ƒ } | j } n d |  } } d } t j | | ƒ \ } } }	 | d k rÀ | d k rÀ t d |  ƒ ‚ n  | d k r|  } d } t | | | d g ƒ } | j } t j | | ƒ \ } } }	 n  Wn% t	 k
 r?}
 t t
 |
 ƒ ƒ ‚ n XWd | rW| j ƒ  n  X| | f S(   s‰   Find the module named `modulename`.

        Returns the file path of the module, and the name of the enclosing
        package.
        R   i   t   __path__s"   module does not live in a file: %rt   __main__N(   t   Nonet   globalst   localst   rsplitt
   __import__R!   R   R    R   R   R   t   close(   R   t   openfilet   glot   locR   R   t   packaget
   searchpathR   t   _R   (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyR    :   s2     	"c      	   C   sQ   t  |  ƒ \ } } t j j | ƒ } | | d <t | | d | d |  d d ƒd S(   s  Run a Python module, as though with ``python -m name args...``.

    `modulename` is the name of the module, possibly a dot-separated name.
    `args` is the argument array to present as sys.argv, including the first
    element naming the module being executed.

    i    R,   R   t   path0t    N(   R    t   ost   patht   abspatht   run_python_file(   R   t   argsR   R   (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyt   run_python_modulee   s    
c         B   s]  | d k r$ e j d k r$ d } n  e j d } e j d ƒ } | e j d <|  | _ | rh | | _ n  | r€ e | ƒ | _	 n  e
 | _ e j } | e _ e j j |  ƒ r|  } x} d d d g D]; }	 e j j |  d |	 ƒ }
 e j j |
 ƒ rÃ |
 }  PqÃ qÃ We d |  ƒ ‚ n e j j e j j |  ƒ ƒ } e j d } | d k	 rR| n | e j d <zÓy. |  j d ƒ rƒe |  ƒ } n e |  ƒ } WnJ e k
 r¦‚  n7 e k
 rÜ} d } e | j d	 |  d
 | ƒ ƒ ‚ n Xy | | j UWnBe k
 r‚  n/e k
 r0e j ƒ  \ } } } e | d d ƒ y; e | d ƒ rX| j  j! | _  n  e j" | | | j! ƒ Wn® e k
 r…‚  q1e k
 re j# j$ d ƒ e j ƒ  \ } } } e% | _& e | d ƒ rá| j  j! | _  n  e j' | | | j! ƒ e j# j$ d ƒ e( | | | j! ƒ ‚ q1Xe j) d ƒ n XWd | e j d <| e _ | e j d <Xd S(   s  Run a Python file as if it were the main program on the command line.

    `filename` is the path to the file to execute, it need not be a .py file.
    `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.

    `modulename` is the name of the module the file was run as.

    `path0` is the value to put into sys.path[0].  If it's None, then this
    function will decide on a value.

    i   R"   s   .pys   .pycs   .pyos$   Can't find '__main__' module in '%s'i    sI   Couldn't run {filename!r} as Python code: {exc.__class__.__name__}: {exc}t   filenamet   exct   __context__t   __traceback__s   Error in sys.excepthook:
s   
Original exception was:
i   N(   i   i   (   s   .pycs   .pyo(*   R#   t   syst   version_infot   modulest   typest
   ModuleTypet   __file__t   __package__R   t
   __loader__R    t   __builtins__t   argvR1   R2   t   isdirt   joint   existsR   R3   t   dirnameR   t   make_code_from_pyct   make_code_from_pyR   t	   Exceptiont   formatt   __dict__t
   SystemExitt   exc_infot   getattrt   hasattrR:   t   tb_nextt
   excepthookt   stderrt   writet   Truet   __suppress_context__t   __excepthook__R   t   exit(   R7   R5   R,   R   R/   t   old_main_modt   main_modt   old_argvt   my_path0t   extt   try_filenamet	   old_path0t   codeR8   t   msgt   typR   t   tbt   typ2t   err2t   tb2(    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyR4   t   sx    					"		c         C   sP   y t  |  ƒ } Wn' t t f k
 r9 t d |  ƒ ‚ n Xt | |  d ƒ } | S(   s8   Get source from `filename` and make a code object of it.s   No file to run: '%s't   exec(   R
   t   IOErrorR   R	   (   R7   t   sourceRa   (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyRJ   æ   s    c         C   s­   y t  |  d ƒ } Wn! t k
 r6 t d |  ƒ ‚ n X| j | j d ƒ } | t k rh t d ƒ ‚ n  | j d ƒ t j d k r” | j d ƒ n  t j | ƒ } Wd QX| S(   s#   Get a code object from a .pyc file.t   rbs   No file to run: '%s'i   s   Bad magic number in .pyc filei   N(   i   i   (	   t   openRi   R   t   readR   R;   R<   t   marshalt   load(   R7   t   fpyct   magicRa   (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyRI   ò   s    (   R   Rn   R1   R;   R>   t   coverage.backwardR    R   R   R   t   coverage.miscR   R   R   R   R   t   coverage.phystokensR	   t   coverage.pythonR
   t   objectR   R    R6   R#   R4   RJ   RI   (    (    (    s3   /tmp/pip-build-hU8Cw8/coverage/coverage/execfile.pyt   <module>   s$   (		+	r	