
[XMc           @   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 m Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z d e f d	     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   sV   
Tests for twisted.python.modules, abstract access to imported or importable
objects.
iN(   t   TestCase(   t   modules(   t   FilePath(   t   namedAny(   t   zipitt   PySpaceTestCasec           B   s5   e  Z e e d   Z d   Z d   Z d d  Z RS(   c         C   sH   x- | j  d |  D] } | j | k r | Sq W|  j d | f  d S(   s   
        You don't ever actually want to do this, so it's not in the public API, but
        sometimes we want to compare the result of an iterative call with a
        lookup call and make sure they're the same for test purposes.
        t   importPackagess+   Unable to find module %r through iteration.N(   t   walkModulest   namet   fail(   t   selft   modnamet   whereR   t   modinfo(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   findByIteration   s    c            s4   t  j     f d   } |  j |  | t  j (d S(   sW   
        Replace sys.path, for the duration of the test, with the given value.
        c              s     t  j (d  S(   N(   t   syst   path(    (   t   originalSysPath(    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   cleanUpSysPath,   s    N(   R   R   t
   addCleanup(   R
   t   sysPathR   (    (   R   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   replaceSysPath'   s    
c            sL   t  j j       f d   } |  j |  t  j j   t  j j |  d S(   sZ   
        Replace sys.modules, for the duration of the test, with the given value.
        c              s!   t  j j   t  j j    d  S(   N(   R   R   t   cleart   update(    (   t   originalSysModules(    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   cleanUpSysModules7   s    N(   R   R   t   copyR   R   R   (   R
   t
   sysModulesR   (    (   R   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   replaceSysModules2   s
    t   test_packagec         C   sE   t  |  j    } | j d  } | j   | j d  j d  | S(   s   
        Generate a L{FilePath} with one package, named C{pkgname}, on it, and
        return the L{FilePath} of the path entry.
        R   s   __init__.pyt    (   R   t   mktempt   childt   makedirst
   setContent(   R
   t   pkgnamet   entryt   pkg(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   pathEntryWithOnePackage?   s
    
(   t   __name__t
   __module__R   t   FalseR   R   R   R&   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR      s   		t
   BasicTestsc           B   sk   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   c         C   st   g  } t  j d |  j   j g d | j d i  d i  d i d d 6 } |  j | g   |  j | d j   t  d S(   s  
        If a package has been explicitly forbidden from importing by setting a
        C{None} key in sys.modules under its name,
        L{modules.PythonPath.__getitem__} should still be able to retrieve an
        unloaded L{modules.PythonModule} for that package.
        R   t   moduleLoadert   importerCachet   sysPathHookst
   moduleDictR   N(	   R   t
   PythonPathR&   R   t   appendt   Nonet   assertEqualst   isLoadedR)   (   R
   t   shouldNotLoadR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_unimportablePackageGetItemN   s    	c         C   s   |  j    } |  j | j g  |  j i d d 6 t t j    } |  j g  | D] } | j	 ^ qR d g  |  j | d j
   t  d S(   s	  
        If a package has been explicitly forbidden from importing by setting a
        C{None} key in sys.modules under its name, L{modules.walkModules} should
        still be able to retrieve an unloaded L{modules.PythonModule} for that
        package.
        R   i    N(   R&   R   R   R   R1   t   listR   R   R2   R   R3   R)   (   R
   t   existentPatht   walkedt   m(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt#   test_unimportablePackageWalkModules_   s    
c         C   s   |  j    } t |  j    } |  j | j    |  j | j g  t j d  g } t	 t j
    } t j j | j  t	 t j
    } |  j | |  |  j | |  d S(   s~   
        Verify that L{modules.walkModules} ignores entries in sys.path which
        do not exist in the filesystem.
        R   N(   R&   R   R   t   failIft   existsR   R   R   t	   getModuleR6   R   R   R0   R2   (   R
   R7   t   nonexistentPatht   expectedt   beforeModulest   afterModules(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_nonexistentPathsp   s    c         C   s   |  j    } t |  j    } |  j | j    | j d  |  j | j g  t t	 j
    } t j j | j  t t	 j
    } |  j | |  d S(   s   
        Verify that L{modules.walkModules} ignores entries in sys.path which
        refer to regular files in the filesystem.
        s   zip file or whatever
N(   R&   R   R   R;   R<   R"   R   R   R6   R   R   R   R0   R2   (   R
   R7   t   nonDirectoryPathR@   RA   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_nonDirectoryPaths   s    c         C   s&   |  j  t j d  |  j d   d S(   s   
        Scrounge around in the top-level module namespace and make sure that
        Twisted shows up, and that the module thusly obtained is the same as
        the module that we find when we look for it explicitly by name.
        t   twistedN(   R2   R   R=   R   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_twistedShowsUp   s    c         C   s5   |  j  t j d  |  j d d t j d   d S(   so   
        Verify that the walkModules APIs will give us back subpackages, not just
        subpackages.
        s   twisted.pythonR   RE   N(   R2   R   R=   R   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_dottedNames   s    c         C   s;   x4 t  j   D]& } |  j d | j k d | j  q Wd S(   s   
        Verify that the iterModules API will only return top-level modules and
        packages, not submodules or subpackages.
        t   .s9   no nested modules should be returned from iterModules: %rN(   R   t   iterModulesR;   R   t   filePath(   R
   t   module(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_onlyTopModules   s
    c         C   sk   xd d d d d g D]P } t  |  } |  j t j |  j   |  |  j |  j |  j   |  q Wd S(   sl   
        Verify that we can locate and load packages, modules, submodules, and
        subpackages.
        t   osRE   s   twisted.pythons   twisted.python.reflectN(   R   t   failUnlessIdenticalR   R=   t   loadR   (   R
   t   nR9   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_loadPackagesAndModules   s    c         C   sF   x? d d d d g D]+ } |  j  t j |  j j j t j  q Wd S(   su   
        Verify that path entries discovered via module loading are, in fact, on
        sys.path somewhere.
        RM   RE   s   twisted.pythons   twisted.python.reflectN(   t   failUnlessInR   R=   t	   pathEntryRJ   R   R   (   R
   RP   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_pathEntriesOnPath   s    c            s   t  |  j    } | j   t j d | j g  } | j     f d   } | j d  j d  t	 j
 | j d t |  j t | j    d  | | _ |  j | d j | j d   d S(	   s~   
        Verify that .py files will always be preferred to .pyc files, regardless of
        directory listing order.
        R   c            s1    |   } | j      f d   } | | _  | S(   Nc             s!       }  |  j    |  j   |  S(   N(   t   sortt   reverse(   t   x(   t   originalChildren(    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   evilChildren   s    	

(   t   children(   t   pathNamet   oRY   (   t   originalSmartPath(   RX   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   _evilSmartPath   s
    		s   abcd.pys   
t   quieti   t   abcdN(   R   R   t   createDirectoryR   R/   R   t
   _smartPathR    R"   t
   compileallt   compile_dirt   TrueR2   t   lenRZ   RJ   (   R
   t   mypatht   ppR^   (    (   R]   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_alwaysPreferPy   s    
		c         C   s   t  |  j    } | j   t j d | j g  } | j d  } | j   | j d  j d  t j j	 | j  d d l
 } zC t | j    } |  j t |  d  |  j | d j d  Wd ~ t j d =t j j | j  Xd S(	   s   
        A package can delete its __path__ for some reasons,
        C{modules.PythonPath} should be able to deal with it.
        R   R`   s   __init__.pys   del __path__
iNi   i    (   R   R   Ra   R   R/   R   R    R"   R   R0   R`   R6   R   R2   Rf   R   t   remove(   R
   Rg   Rh   t   subpathR`   t   l(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_packageMissingPath   s    


(   R'   R(   R5   R:   RB   RD   RF   RG   RL   RQ   RT   Ri   Rm   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR*   L   s   					
					t   PathModificationTestc           B   sz   e  Z d  Z e j   j Z d   Z d   Z e	 d  Z
 d   Z d   Z d e _ d   Z d   Z d	   Z d
   Z RS(   sp   
    These tests share setup/cleanup behavior of creating a dummy package and
    stuffing some code in it.
    c         C   s   |  j    |  _ t |  j  |  _ |  j j   d |  j   f |  _ |  j j |  j  |  _ |  j j   |  j j d  j	 d  |  j j d  j	 d  |  j j d  j	 d  |  j j d  j	 d  t
 |  _ d  S(   Ns   pyspacetests%ds   __init__.pyR   s   a.pys   b.pys   c__init__.py(   R   t   pathExtensionNameR   t   pathExtensionRa   t
   _serialnumt   packageNameR    t   packagePathR"   R)   t	   pathSetUp(   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   setUp  s    c         C   s0   |  j  s t  t |  _  t j j |  j  d  S(   N(   Rt   t   AssertionErrorRe   R   R   R0   Ro   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   _setupSysPath(  s    	c         C   s   |  j    } t |  } | j   | j d  j d  |  j j d  j d | f  |  j   t j |  j	  } |  j
 |  j |  j	 d | d | | d  |  j
 | d j   j d  d  S(	   Ns	   foozle.pys   x = 123
s   __init__.pys   __path__.append(%r)
s   .foozleR   t   foozlei{   (   R   R   Ra   R    R"   Rs   Rw   R   R=   Rr   R2   R   RO   RW   (   R
   t   doImportt   moddir2t   fpmdR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   _underUnderPathTest.  s    

c         C   s   |  j    d S(   s]   
        Verify that iterModules will honor the __path__ of already-loaded packages.
        N(   R|   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt"   test_underUnderPathAlreadyImported?  s    c         C   s   |  j  t  d S(   s]   
        Verify that iterModules will honor the __path__ of already-loaded packages.
        N(   R|   R)   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt%   test_underUnderPathNotAlreadyImportedF  s    s1   This may be impossible but it sure would be nice.c         C   sh   t  j |  j  } g  | j   D] } | j j d  d ^ q } | j   |  j | d d d g  d  S(   NRH   it   at   bt	   c__init__(   R   R=   Rr   RI   R   t   splitRU   t   failUnlessEqual(   R
   t   pkginfoR   t   nfni(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   _listModulesQ  s
    ,
c         C   s   |  j    |  j   d S(   s   
        Make sure the module list comes back as we expect from iterModules on a
        package, whether zipped or not.
        N(   Rw   R   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_listingModulesY  s    
c         C   s%   |  j    t |  j  |  j   d S(   s   
        Make sure the module list comes back as we expect from iterModules on a
        package, whether zipped or not, even if the package has already been
        imported.
        N(   Rw   R   Rr   R   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt"   test_listingModulesAlreadyImportedb  s    
c         C   sZ   |  j  rV d } t j j   |  j k s4 t | d  |  j t j k sV t | d  n  d  S(   Ns<   path cleanup failed: don't be surprised if other tests breaks   , 1s   , 2(   Rt   R   R   t   popRo   Rv   (   R
   t   HORK(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   tearDownm  s    	%(   R'   R(   t   __doc__t	   itertoolst   countt   nextRq   Ru   Rw   Re   R|   R}   R~   t   todoR   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyRn     s   									t   RebindingTestc           B   s    e  Z d  Z d   Z d   Z RS(   s   
    These tests verify that the default path interrogation API works properly
    even when sys.path has been rebound to a different object.
    c         C   sI   |  j  s t  t |  _  t j |  _ t j t _ t j j |  j  d  S(   N(   Rt   Rv   Re   R   R   t   savedSysPathR0   Ro   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyRw   |  s
    	c         C   s   |  j  r |  j t _ n  d S(   sF   
        Clean up sys.path by re-binding our original object.
        N(   Rt   R   R   R   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR     s    	(   R'   R(   R   Rw   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR   w  s   	t   ZipPathModificationTestc           B   s   e  Z d    Z RS(   c         C   s_   |  j  s t  t |  j |  j d  |  j d 7_ t j |  j  sN t  t j |   d  S(   Ns   .zip(   Rt   Rv   R   Ro   t   zipfilet
   is_zipfileRn   Rw   (   R
   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyRw     s
    (   R'   R(   Rw   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR     s   t   PythonPathTestCasec           B   s    e  Z d  Z d   Z d   Z RS(   s   
    Tests for the class which provides the implementation for all of the
    public API of L{twisted.python.modules}, L{PythonPath}.
    c            s   d t  f d     Y    f d   } d g } i  } | g } i  } d   } t j | | | | |  } t | j     |  j t   d  |  j t  f d    d S(	   s}   
        Make sure that the behavior when encountering an unknown importer
        type is not catastrophic failure.
        t   SecretImporterc           B   s   e  Z RS(    (   R'   R(   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR     s   c            s       S(   N(    (   R   (   R   (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   hook  s    s   example/pathc         S   s   d  S(   N(   R1   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt	   sysloader  s    i   c              s     d d S(   Ni    RK   (    (    (   t   entries(    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   <lambda>  s    N(	   t   objectR   R/   R6   t   iterEntriesR2   Rf   t   assertRaisest   KeyError(   R
   R   t   syspatht
   sysmodulest   syshookst   syscacheR   t   space(    (   R   R   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   test_unhandledImporter  s    			c         C   s   t  j g  t j  g  i   } | t } |  j |  j g  } |  j | d d t  |  j | d d t t	 j
  j   j   d t d  |  j t |  d  |  j | j t  d S(   s   
        If the path a module loaded with L{PythonPath.__getitem__} is not
        present in the path importer cache, a warning is emitted, but the
        L{PythonModule} is returned as usual.
        i    t   categoryt   messages    (for module sR   ) not in path importer cache (PEP 302 violation - check your local configuration).i   N(   R   R/   R   R'   t   flushWarningst   test_inconsistentImporterCacheR2   t   UserWarningR   RE   t   __file__t   parentt   dirnameRf   R   (   R
   R   t
   thisModulet   warnings(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR     s    
((   R'   R(   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyR     s   	(   R   R   R   R   Rc   RE   t   twisted.trial.unittestR    t   twisted.pythonR   t   twisted.python.filepathR   t   twisted.python.reflectR   t   twisted.test.test_pathsR   R   R*   Rn   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_modules.pyt   <module>   s    3f	