<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import sys <EOL> import twisted <EOL> from types import ModuleType , FunctionType <EOL> from twisted import _checkRequirements <EOL> from twisted . python . compat import _PY3 <EOL> from twisted . python import reflect <EOL> from twisted . trial . unittest import TestCase , SkipTest <EOL> class SetAsideModule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def _unimport ( self , name ) : <EOL> """<STR_LIT>""" <EOL> modules = dict ( [ <EOL> ( moduleName , module ) <EOL> for ( moduleName , module ) <EOL> in list ( sys . modules . items ( ) ) <EOL> if ( moduleName == self . name or <EOL> moduleName . startswith ( self . name + "<STR_LIT:.>" ) ) ] ) <EOL> for name in modules : <EOL> del sys . modules [ name ] <EOL> return modules <EOL> def __enter__ ( self ) : <EOL> self . modules = self . _unimport ( self . name ) <EOL> def __exit__ ( self , excType , excValue , traceback ) : <EOL> self . _unimport ( self . name ) <EOL> sys . modules . update ( self . modules ) <EOL> def _install ( modules ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> _makePackages ( None , modules , result ) <EOL> sys . modules . update ( result ) <EOL> def _makePackages ( parent , attributes , result ) : <EOL> """<STR_LIT>""" <EOL> attrs = { } <EOL> for ( name , value ) in list ( attributes . items ( ) ) : <EOL> if parent is None : <EOL> if isinstance ( value , dict ) : <EOL> module = ModuleType ( name ) <EOL> module . __dict__ . update ( _makePackages ( module , value , result ) ) <EOL> result [ name ] = module <EOL> else : <EOL> result [ name ] = value <EOL> else : <EOL> if isinstance ( value , dict ) : <EOL> module = ModuleType ( parent . __name__ + '<STR_LIT:.>' + name ) <EOL> module . __dict__ . update ( _makePackages ( module , value , result ) ) <EOL> result [ parent . __name__ + '<STR_LIT:.>' + name ] = module <EOL> attrs [ name ] = module <EOL> else : <EOL> attrs [ name ] = value <EOL> return attrs <EOL> class RequirementsTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> unsupportedPythonVersion = ( <NUM_LIT:2> , <NUM_LIT:6> ) <EOL> supportedPythonVersion = ( <NUM_LIT:2> , <NUM_LIT:7> ) <EOL> Py3unsupportedPythonVersion = ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> Py3supportedPythonVersion = ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> if _PY3 : <EOL> supportedZopeInterfaceVersion = ( <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> supportedZopeInterfaceVersion = ( <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:0> ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . version = sys . version_info <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . version_info = self . version <EOL> def test_oldPython ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . version_info = self . unsupportedPythonVersion <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( "<STR_LIT>" <EOL> % self . supportedPythonVersion , <EOL> str ( raised . exception ) ) <EOL> def test_newPython ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . version_info = self . supportedPythonVersion <EOL> self . assertEqual ( None , _checkRequirements ( ) ) <EOL> def test_oldPythonPy3 ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . version_info = self . Py3unsupportedPythonVersion <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( "<STR_LIT>" <EOL> % self . Py3supportedPythonVersion , <EOL> str ( raised . exception ) ) <EOL> def test_newPythonPy3 ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . version_info = self . Py3supportedPythonVersion <EOL> self . assertEqual ( None , _checkRequirements ( ) ) <EOL> def test_missingZopeNamespace ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = None <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . supportedZopeInterfaceVersion , <EOL> str ( raised . exception ) ) <EOL> def test_missingZopeInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = ModuleType ( "<STR_LIT>" ) <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . supportedZopeInterfaceVersion , <EOL> str ( raised . exception ) ) <EOL> def test_setupNoCheckRequirements ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> setuptools = ModuleType ( "<STR_LIT>" ) <EOL> setuptools . _TWISTED_NO_CHECK_REQUIREMENTS = True <EOL> sys . modules [ "<STR_LIT>" ] = setuptools <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = None <EOL> _checkRequirements ( ) <EOL> def test_setupCheckRequirements ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = ModuleType ( "<STR_LIT>" ) <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = None <EOL> self . assertRaises ( ImportError , _checkRequirements ) <EOL> def test_noSetupCheckRequirements ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = None <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> sys . modules [ "<STR_LIT>" ] = None <EOL> self . assertRaises ( ImportError , _checkRequirements ) <EOL> if _PY3 : <EOL> def test_oldZopeInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> _install ( _zope38 ) <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" , <EOL> str ( raised . exception ) ) <EOL> def test_newZopeInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> _install ( _zope40 ) <EOL> self . assertEqual ( None , _checkRequirements ( ) ) <EOL> else : <EOL> def test_oldZopeInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> _install ( _zope35 ) <EOL> with self . assertRaises ( ImportError ) as raised : <EOL> _checkRequirements ( ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" , <EOL> str ( raised . exception ) ) <EOL> def test_newZopeInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> _install ( _zope36 ) <EOL> self . assertEqual ( None , _checkRequirements ( ) ) <EOL> class MakePackagesTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_nonModule ( self ) : <EOL> """<STR_LIT>""" <EOL> modules = { } <EOL> _makePackages ( None , dict ( reactor = '<STR_LIT>' ) , modules ) <EOL> self . assertEqual ( modules , dict ( reactor = '<STR_LIT>' ) ) <EOL> def test_moduleWithAttribute ( self ) : <EOL> """<STR_LIT>""" <EOL> modules = { } <EOL> _makePackages ( None , dict ( twisted = dict ( version = '<STR_LIT>' ) ) , modules ) <EOL> self . assertTrue ( isinstance ( modules , dict ) ) <EOL> self . assertTrue ( isinstance ( modules [ '<STR_LIT>' ] , ModuleType ) ) <EOL> self . assertEqual ( '<STR_LIT>' , modules [ '<STR_LIT>' ] . __name__ ) <EOL> self . assertEqual ( '<STR_LIT>' , modules [ '<STR_LIT>' ] . version ) <EOL> def test_packageWithModule ( self ) : <EOL> """<STR_LIT>""" <EOL> modules = { } <EOL> _makePackages ( None , dict ( twisted = dict ( web = dict ( version = '<STR_LIT>' ) ) ) , modules ) <EOL> self . assertTrue ( isinstance ( modules , dict ) ) <EOL> self . assertTrue ( isinstance ( modules [ '<STR_LIT>' ] , ModuleType ) ) <EOL> self . assertEqual ( '<STR_LIT>' , modules [ '<STR_LIT>' ] . __name__ ) <EOL> self . assertTrue ( isinstance ( modules [ '<STR_LIT>' ] . web , ModuleType ) ) <EOL> self . assertEqual ( '<STR_LIT>' , modules [ '<STR_LIT>' ] . web . __name__ ) <EOL> self . assertEqual ( '<STR_LIT>' , modules [ '<STR_LIT>' ] . web . version ) <EOL> def _functionOnlyImplementer ( * interfaces ) : <EOL> """<STR_LIT>""" <EOL> def check ( obj ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , FunctionType ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return check <EOL> def _classSupportingImplementer ( * interfaces ) : <EOL> """<STR_LIT>""" <EOL> def check ( obj ) : <EOL> """<STR_LIT>""" <EOL> return check <EOL> class _SuccessInterface ( object ) : <EOL> """<STR_LIT>""" <EOL> _zope35 = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : _SuccessInterface , <EOL> '<STR_LIT>' : _functionOnlyImplementer , <EOL> } , <EOL> } , <EOL> } <EOL> _zope36 = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : _SuccessInterface , <EOL> '<STR_LIT>' : _classSupportingImplementer , <EOL> } , <EOL> } , <EOL> } <EOL> class _Zope38OnPython3Module ( object ) : <EOL> """<STR_LIT>""" <EOL> __path__ = [ ] <EOL> __name__ = '<STR_LIT>' <EOL> @ property <EOL> def interface ( self ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> _zope38 = { <EOL> '<STR_LIT>' : _Zope38OnPython3Module ( ) , <EOL> } <EOL> _zope40 = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : _SuccessInterface , <EOL> '<STR_LIT>' : _classSupportingImplementer , <EOL> } , <EOL> } , <EOL> } <EOL> class ZopeInterfaceTestsMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def test_zope35 ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> self . install ( ( <NUM_LIT:3> , <NUM_LIT:5> ) ) <EOL> from zope . interface import Interface , implementer <EOL> class IDummy ( Interface ) : <EOL> pass <EOL> try : <EOL> @ implementer ( IDummy ) <EOL> class Dummy ( object ) : <EOL> pass <EOL> except TypeError as exc : <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> str ( exc ) ) <EOL> def test_zope36 ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> self . install ( ( <NUM_LIT:3> , <NUM_LIT:6> ) ) <EOL> from zope . interface import Interface , implementer <EOL> class IDummy ( Interface ) : <EOL> pass <EOL> @ implementer ( IDummy ) <EOL> class Dummy ( object ) : <EOL> pass <EOL> if _PY3 : <EOL> def test_zope38 ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> self . install ( ( <NUM_LIT:3> , <NUM_LIT:8> ) ) <EOL> try : <EOL> from zope import interface <EOL> interface <EOL> except Exception as exc : <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> str ( exc ) ) <EOL> else : <EOL> self . fail ( <EOL> "<STR_LIT>" ) <EOL> def test_zope40 ( self ) : <EOL> """<STR_LIT>""" <EOL> with SetAsideModule ( "<STR_LIT>" ) : <EOL> self . install ( ( <NUM_LIT:4> , <NUM_LIT:0> ) ) <EOL> from zope . interface import Interface <EOL> class IDummy ( Interface ) : <EOL> pass <EOL> class FakeZopeInterfaceTests ( TestCase , ZopeInterfaceTestsMixin ) : <EOL> """<STR_LIT>""" <EOL> versions = { <EOL> ( <NUM_LIT:3> , <NUM_LIT:5> ) : _zope35 , <EOL> ( <NUM_LIT:3> , <NUM_LIT:6> ) : _zope36 , <EOL> ( <NUM_LIT:3> , <NUM_LIT:8> ) : _zope38 , <EOL> ( <NUM_LIT:4> , <NUM_LIT:0> ) : _zope40 , <EOL> } <EOL> def install ( self , version ) : <EOL> """<STR_LIT>""" <EOL> _install ( self . versions [ version ] ) <EOL> class RealZopeInterfaceTests ( TestCase , ZopeInterfaceTestsMixin ) : <EOL> """<STR_LIT>""" <EOL> def install ( self , version ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import pkg_resources <EOL> except ImportError as e : <EOL> raise SkipTest ( <EOL> "<STR_LIT>" % ( e , ) ) <EOL> else : <EOL> try : <EOL> pkg = pkg_resources . get_distribution ( "<STR_LIT>" ) <EOL> except pkg_resources . DistributionNotFound as e : <EOL> raise SkipTest ( <EOL> "<STR_LIT>" % ( <EOL> e , ) ) <EOL> installed = pkg . version <EOL> versionTuple = tuple ( <EOL> int ( part ) for part in installed . split ( '<STR_LIT:.>' ) [ : len ( version ) ] ) <EOL> if versionTuple == version : <EOL> pass <EOL> else : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> class OldSubprojectDeprecationBase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> subproject = None <EOL> def test_deprecated ( self ) : <EOL> """<STR_LIT>""" <EOL> module = reflect . namedAny ( "<STR_LIT>" . format ( self . subproject ) ) <EOL> self . assertEqual ( module . __version__ , twisted . __version__ ) <EOL> warningsShown = self . flushWarnings ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( warningsShown ) ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . subproject ) , <EOL> warningsShown [ <NUM_LIT:0> ] [ '<STR_LIT:message>' ] ) <EOL> def test_noversionpy ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( AttributeError ) : <EOL> reflect . namedAny ( <EOL> "<STR_LIT>" . format ( self . subproject ) ) <EOL> if _PY3 : <EOL> subprojects = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> subprojects = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> for subproject in subprojects : <EOL> class SubprojectTestCase ( OldSubprojectDeprecationBase ) : <EOL> """<STR_LIT>""" <EOL> subproject = subproject <EOL> newName = subproject . title ( ) + "<STR_LIT>" <EOL> SubprojectTestCase . __name__ = newName <EOL> if _PY3 : <EOL> SubprojectTestCase . __qualname__ = "<STR_LIT:.>" . join ( <EOL> OldSubprojectDeprecationBase . __qualname__ . split ( ) [ <NUM_LIT:0> : - <NUM_LIT:1> ] + <EOL> [ newName ] ) <EOL> globals ( ) . update ( { subproject . title ( ) + <EOL> "<STR_LIT>" : SubprojectTestCase } ) <EOL> del SubprojectTestCase <EOL> del newName <EOL> del OldSubprojectDeprecationBase </s>
