<s> import sys <EOL> import os <EOL> import marshal <EOL> import imp <EOL> import struct <EOL> import time <EOL> import unittest <EOL> import zlib <EOL> from zipfile import ZipFile , ZipInfo , ZIP_STORED , ZIP_DEFLATED <EOL> from test import test_support <EOL> from test . test_importhooks import ImportHooksBaseTestCase , test_src , test_co <EOL> import zipimport <EOL> import linecache <EOL> import doctest <EOL> import inspect <EOL> import StringIO <EOL> from traceback import extract_tb , extract_stack , print_tb <EOL> raise_src = '<STR_LIT>' <EOL> test_imported = False <EOL> def make_pyc ( co , mtime ) : <EOL> data = marshal . dumps ( co ) <EOL> if type ( mtime ) is type ( <NUM_LIT:0.0> ) : <EOL> if mtime < <NUM_LIT> : <EOL> mtime = int ( mtime ) <EOL> else : <EOL> mtime = int ( - <NUM_LIT> L + long ( mtime ) ) <EOL> pyc = imp . get_magic ( ) + struct . pack ( "<STR_LIT>" , int ( mtime ) ) + data <EOL> return pyc <EOL> def module_path_to_dotted_name ( path ) : <EOL> return path . replace ( os . sep , '<STR_LIT:.>' ) <EOL> NOW = time . time ( ) <EOL> test_pyc = make_pyc ( test_co , NOW ) <EOL> if __debug__ : <EOL> pyc_ext = "<STR_LIT>" <EOL> else : <EOL> pyc_ext = "<STR_LIT>" <EOL> TESTMOD = "<STR_LIT>" <EOL> TESTPACK = "<STR_LIT>" <EOL> TESTPACK2 = "<STR_LIT>" <EOL> TEMP_ZIP = os . path . abspath ( "<STR_LIT>" + os . extsep + "<STR_LIT>" ) <EOL> class UncompressedZipImportTestCase ( ImportHooksBaseTestCase ) : <EOL> compression = ZIP_STORED <EOL> def setUp ( self ) : <EOL> linecache . clearcache ( ) <EOL> zipimport . _zip_directory_cache . clear ( ) <EOL> ImportHooksBaseTestCase . setUp ( self ) <EOL> def doTest ( self , expected_ext , files , * modules , ** kw ) : <EOL> z = ZipFile ( TEMP_ZIP , "<STR_LIT:w>" ) <EOL> try : <EOL> for name , ( mtime , data ) in files . items ( ) : <EOL> zinfo = ZipInfo ( name , time . localtime ( mtime ) ) <EOL> zinfo . compress_type = self . compression <EOL> z . writestr ( zinfo , data ) <EOL> z . close ( ) <EOL> stuff = kw . get ( "<STR_LIT>" , None ) <EOL> if stuff is not None : <EOL> f = open ( TEMP_ZIP , "<STR_LIT:rb>" ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> f = open ( TEMP_ZIP , "<STR_LIT:wb>" ) <EOL> f . write ( stuff ) <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> sys . path . insert ( <NUM_LIT:0> , TEMP_ZIP ) <EOL> mod = __import__ ( "<STR_LIT:.>" . join ( modules ) , globals ( ) , locals ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> call = kw . get ( '<STR_LIT>' ) <EOL> if call is not None : <EOL> call ( mod ) <EOL> if expected_ext : <EOL> file = mod . get_file ( ) <EOL> self . assertEquals ( file , os . path . join ( TEMP_ZIP , <EOL> * modules ) + expected_ext ) <EOL> finally : <EOL> z . close ( ) <EOL> os . remove ( TEMP_ZIP ) <EOL> def testAFakeZlib ( self ) : <EOL> if "<STR_LIT>" in sys . builtin_module_names : <EOL> return <EOL> if "<STR_LIT>" in sys . modules : <EOL> del sys . modules [ "<STR_LIT>" ] <EOL> files = { "<STR_LIT>" : ( NOW , test_src ) } <EOL> try : <EOL> self . doTest ( "<STR_LIT>" , files , "<STR_LIT>" ) <EOL> except ImportError : <EOL> if self . compression != ZIP_DEFLATED : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> if self . compression != ZIP_STORED : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testPy ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD ) <EOL> def testPyc ( self ) : <EOL> files = { TESTMOD + pyc_ext : ( NOW , test_pyc ) } <EOL> self . doTest ( pyc_ext , files , TESTMOD ) <EOL> def testBoth ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) , <EOL> TESTMOD + pyc_ext : ( NOW , test_pyc ) } <EOL> self . doTest ( pyc_ext , files , TESTMOD ) <EOL> def testEmptyPy ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , "<STR_LIT>" ) } <EOL> self . doTest ( None , files , TESTMOD ) <EOL> def testBadMagic ( self ) : <EOL> m0 = ord ( test_pyc [ <NUM_LIT:0> ] ) <EOL> m0 ^= <NUM_LIT> <EOL> badmagic_pyc = chr ( m0 ) + test_pyc [ <NUM_LIT:1> : ] <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) , <EOL> TESTMOD + pyc_ext : ( NOW , badmagic_pyc ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD ) <EOL> def testBadMagic2 ( self ) : <EOL> m0 = ord ( test_pyc [ <NUM_LIT:0> ] ) <EOL> m0 ^= <NUM_LIT> <EOL> badmagic_pyc = chr ( m0 ) + test_pyc [ <NUM_LIT:1> : ] <EOL> files = { TESTMOD + pyc_ext : ( NOW , badmagic_pyc ) } <EOL> try : <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD ) <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testBadMTime ( self ) : <EOL> t3 = ord ( test_pyc [ <NUM_LIT:7> ] ) <EOL> t3 ^= <NUM_LIT> <EOL> badtime_pyc = test_pyc [ : <NUM_LIT:7> ] + chr ( t3 ) + test_pyc [ <NUM_LIT:8> : ] <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) , <EOL> TESTMOD + pyc_ext : ( NOW , badtime_pyc ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD ) <EOL> def testPackage ( self ) : <EOL> packdir = TESTPACK + os . sep <EOL> files = { packdir + "<STR_LIT>" + pyc_ext : ( NOW , test_pyc ) , <EOL> packdir + TESTMOD + pyc_ext : ( NOW , test_pyc ) } <EOL> self . doTest ( pyc_ext , files , TESTPACK , TESTMOD ) <EOL> def testDeepPackage ( self ) : <EOL> packdir = TESTPACK + os . sep <EOL> packdir2 = packdir + TESTPACK2 + os . sep <EOL> files = { packdir + "<STR_LIT>" + pyc_ext : ( NOW , test_pyc ) , <EOL> packdir2 + "<STR_LIT>" + pyc_ext : ( NOW , test_pyc ) , <EOL> packdir2 + TESTMOD + pyc_ext : ( NOW , test_pyc ) } <EOL> self . doTest ( pyc_ext , files , TESTPACK , TESTPACK2 , TESTMOD ) <EOL> def testZipImporterMethods ( self ) : <EOL> packdir = TESTPACK + os . sep <EOL> packdir2 = packdir + TESTPACK2 + os . sep <EOL> files = { packdir + "<STR_LIT>" + pyc_ext : ( NOW , test_pyc ) , <EOL> packdir2 + "<STR_LIT>" + pyc_ext : ( NOW , test_pyc ) , <EOL> packdir2 + TESTMOD + pyc_ext : ( NOW , test_pyc ) } <EOL> z = ZipFile ( TEMP_ZIP , "<STR_LIT:w>" ) <EOL> try : <EOL> for name , ( mtime , data ) in files . items ( ) : <EOL> zinfo = ZipInfo ( name , time . localtime ( mtime ) ) <EOL> zinfo . compress_type = self . compression <EOL> z . writestr ( zinfo , data ) <EOL> z . close ( ) <EOL> zi = zipimport . zipimporter ( TEMP_ZIP ) <EOL> self . assertEquals ( zi . is_package ( TESTPACK ) , True ) <EOL> zi . load_module ( TESTPACK ) <EOL> self . assertEquals ( zi . is_package ( packdir + '<STR_LIT>' ) , False ) <EOL> self . assertEquals ( zi . is_package ( packdir + TESTPACK2 ) , True ) <EOL> self . assertEquals ( zi . is_package ( packdir2 + TESTMOD ) , False ) <EOL> mod_name = packdir2 + TESTMOD <EOL> mod = __import__ ( module_path_to_dotted_name ( mod_name ) ) <EOL> self . assertEquals ( zi . get_source ( TESTPACK ) , None ) <EOL> self . assertEquals ( zi . get_source ( mod_name ) , None ) <EOL> finally : <EOL> z . close ( ) <EOL> os . remove ( TEMP_ZIP ) <EOL> def testGetData ( self ) : <EOL> z = ZipFile ( TEMP_ZIP , "<STR_LIT:w>" ) <EOL> z . compression = self . compression <EOL> try : <EOL> name = "<STR_LIT>" <EOL> data = "<STR_LIT>" . join ( [ chr ( x ) for x in range ( <NUM_LIT> ) ] ) * <NUM_LIT> <EOL> z . writestr ( name , data ) <EOL> z . close ( ) <EOL> zi = zipimport . zipimporter ( TEMP_ZIP ) <EOL> self . assertEquals ( data , zi . get_data ( name ) ) <EOL> self . assert_ ( '<STR_LIT>' in repr ( zi ) ) <EOL> finally : <EOL> z . close ( ) <EOL> os . remove ( TEMP_ZIP ) <EOL> def testImporterAttr ( self ) : <EOL> src = """<STR_LIT>""" <EOL> pyc = make_pyc ( compile ( src , "<STR_LIT>" , "<STR_LIT>" ) , NOW ) <EOL> files = { TESTMOD + pyc_ext : ( NOW , pyc ) , <EOL> "<STR_LIT>" : ( NOW , "<STR_LIT>" ) } <EOL> self . doTest ( pyc_ext , files , TESTMOD ) <EOL> def testImport_WithStuff ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD , <EOL> stuff = "<STR_LIT>" * <NUM_LIT> ) <EOL> def assertModuleSource ( self , module ) : <EOL> self . assertEqual ( inspect . getsource ( module ) , test_src ) <EOL> def testGetSource ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD , call = self . assertModuleSource ) <EOL> def testGetCompiledSource ( self ) : <EOL> pyc = make_pyc ( compile ( test_src , "<STR_LIT>" , "<STR_LIT>" ) , NOW ) <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) , <EOL> TESTMOD + pyc_ext : ( NOW , pyc ) } <EOL> self . doTest ( pyc_ext , files , TESTMOD , call = self . assertModuleSource ) <EOL> def runDoctest ( self , callback ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , test_src ) , <EOL> "<STR_LIT>" : ( NOW , "<STR_LIT>" ) } <EOL> self . doTest ( "<STR_LIT>" , files , TESTMOD , call = callback ) <EOL> def doDoctestFile ( self , module ) : <EOL> log = [ ] <EOL> old_master , doctest . master = doctest . master , None <EOL> try : <EOL> doctest . testfile ( <EOL> '<STR_LIT>' , package = module , module_relative = True , <EOL> globs = locals ( ) <EOL> ) <EOL> finally : <EOL> doctest . master = old_master <EOL> self . assertEqual ( log , [ True ] ) <EOL> def testDoctestFile ( self ) : <EOL> self . runDoctest ( self . doDoctestFile ) <EOL> def doDoctestSuite ( self , module ) : <EOL> log = [ ] <EOL> doctest . DocFileTest ( <EOL> '<STR_LIT>' , package = module , module_relative = True , <EOL> globs = locals ( ) <EOL> ) . run ( ) <EOL> self . assertEqual ( log , [ True ] ) <EOL> def testDoctestSuite ( self ) : <EOL> self . runDoctest ( self . doDoctestSuite ) <EOL> def doTraceback ( self , module ) : <EOL> try : <EOL> module . do_raise ( ) <EOL> except : <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] . tb_next <EOL> f , lno , n , line = extract_tb ( tb , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( line , raise_src . strip ( ) ) <EOL> f , lno , n , line = extract_stack ( tb . tb_frame , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( line , raise_src . strip ( ) ) <EOL> s = StringIO . StringIO ( ) <EOL> print_tb ( tb , <NUM_LIT:1> , s ) <EOL> self . failUnless ( s . getvalue ( ) . endswith ( raise_src ) ) <EOL> else : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> def testTraceback ( self ) : <EOL> files = { TESTMOD + "<STR_LIT>" : ( NOW , raise_src ) } <EOL> self . doTest ( None , files , TESTMOD , call = self . doTraceback ) <EOL> class CompressedZipImportTestCase ( UncompressedZipImportTestCase ) : <EOL> compression = ZIP_DEFLATED <EOL> class BadFileZipImportTestCase ( unittest . TestCase ) : <EOL> def assertZipFailure ( self , filename ) : <EOL> self . assertRaises ( zipimport . ZipImportError , <EOL> zipimport . zipimporter , filename ) <EOL> def testNoFile ( self ) : <EOL> self . assertZipFailure ( '<STR_LIT>' ) <EOL> def testEmptyFilename ( self ) : <EOL> self . assertZipFailure ( '<STR_LIT>' ) <EOL> def testBadArgs ( self ) : <EOL> self . assertRaises ( TypeError , zipimport . zipimporter , None ) <EOL> self . assertRaises ( TypeError , zipimport . zipimporter , TESTMOD , kwd = None ) <EOL> def testFilenameTooLong ( self ) : <EOL> self . assertZipFailure ( '<STR_LIT:A>' * <NUM_LIT> ) <EOL> def testEmptyFile ( self ) : <EOL> test_support . unlink ( TESTMOD ) <EOL> open ( TESTMOD , '<STR_LIT>' ) . close ( ) <EOL> self . assertZipFailure ( TESTMOD ) <EOL> def testFileUnreadable ( self ) : <EOL> test_support . unlink ( TESTMOD ) <EOL> fd = os . open ( TESTMOD , os . O_CREAT , <NUM_LIT> ) <EOL> try : <EOL> os . close ( fd ) <EOL> self . assertZipFailure ( TESTMOD ) <EOL> finally : <EOL> os . chmod ( TESTMOD , <NUM_LIT:0> <NUM_LIT> ) <EOL> test_support . unlink ( TESTMOD ) <EOL> def testNotZipFile ( self ) : <EOL> test_support . unlink ( TESTMOD ) <EOL> fp = open ( TESTMOD , '<STR_LIT>' ) <EOL> fp . write ( '<STR_LIT:a>' * <NUM_LIT> ) <EOL> fp . close ( ) <EOL> self . assertZipFailure ( TESTMOD ) <EOL> def _testBogusZipFile ( self ) : <EOL> test_support . unlink ( TESTMOD ) <EOL> fp = open ( TESTMOD , '<STR_LIT>' ) <EOL> fp . write ( struct . pack ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> fp . write ( '<STR_LIT:a>' * <NUM_LIT> ) <EOL> fp . close ( ) <EOL> z = zipimport . zipimporter ( TESTMOD ) <EOL> try : <EOL> self . assertRaises ( TypeError , z . find_module , None ) <EOL> self . assertRaises ( TypeError , z . load_module , None ) <EOL> self . assertRaises ( TypeError , z . is_package , None ) <EOL> self . assertRaises ( TypeError , z . get_code , None ) <EOL> self . assertRaises ( TypeError , z . get_data , None ) <EOL> self . assertRaises ( TypeError , z . get_source , None ) <EOL> error = zipimport . ZipImportError <EOL> self . assertEqual ( z . find_module ( '<STR_LIT:abc>' ) , None ) <EOL> self . assertRaises ( error , z . load_module , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( error , z . get_code , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( IOError , z . get_data , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( error , z . get_source , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( error , z . is_package , '<STR_LIT:abc>' ) <EOL> finally : <EOL> zipimport . _zip_directory_cache . clear ( ) <EOL> def cleanup ( ) : <EOL> global test_imported <EOL> if test_imported : <EOL> zipimport . _zip_directory_cache . clear ( ) <EOL> if hasattr ( UncompressedZipImportTestCase , '<STR_LIT>' ) : <EOL> delattr ( UncompressedZipImportTestCase , '<STR_LIT>' ) <EOL> if hasattr ( CompressedZipImportTestCase , '<STR_LIT>' ) : <EOL> delattr ( CompressedZipImportTestCase , '<STR_LIT>' ) <EOL> test_imported = True <EOL> def test_main ( ) : <EOL> cleanup ( ) <EOL> try : <EOL> test_support . run_unittest ( <EOL> UncompressedZipImportTestCase , <EOL> CompressedZipImportTestCase , <EOL> BadFileZipImportTestCase , <EOL> ) <EOL> finally : <EOL> test_support . unlink ( TESTMOD ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
