<s> import tempfile <EOL> import errno <EOL> import io <EOL> import os <EOL> import signal <EOL> import sys <EOL> import re <EOL> import warnings <EOL> import contextlib <EOL> import unittest <EOL> from test import support <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> import stat <EOL> has_stat = <NUM_LIT:1> <EOL> else : <EOL> has_stat = <NUM_LIT:0> <EOL> has_textmode = ( tempfile . _text_openflags != tempfile . _bin_openflags ) <EOL> has_spawnl = hasattr ( os , '<STR_LIT>' ) <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> TEST_FILES = <NUM_LIT> <EOL> else : <EOL> TEST_FILES = <NUM_LIT:100> <EOL> class BaseTestCase ( unittest . TestCase ) : <EOL> str_check = re . compile ( r"<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> self . _warnings_manager = support . check_warnings ( ) <EOL> self . _warnings_manager . __enter__ ( ) <EOL> warnings . filterwarnings ( "<STR_LIT:ignore>" , category = RuntimeWarning , <EOL> message = "<STR_LIT>" , module = __name__ ) <EOL> def tearDown ( self ) : <EOL> self . _warnings_manager . __exit__ ( None , None , None ) <EOL> def nameCheck ( self , name , dir , pre , suf ) : <EOL> ( ndir , nbase ) = os . path . split ( name ) <EOL> npre = nbase [ : len ( pre ) ] <EOL> nsuf = nbase [ len ( nbase ) - len ( suf ) : ] <EOL> self . assertEqual ( os . path . abspath ( ndir ) , os . path . abspath ( dir ) , <EOL> "<STR_LIT>" % ( name , dir ) ) <EOL> self . assertEqual ( npre , pre , <EOL> "<STR_LIT>" % ( nbase , pre ) ) <EOL> self . assertEqual ( nsuf , suf , <EOL> "<STR_LIT>" % ( nbase , suf ) ) <EOL> nbase = nbase [ len ( pre ) : len ( nbase ) - len ( suf ) ] <EOL> self . assertTrue ( self . str_check . match ( nbase ) , <EOL> "<STR_LIT>" <EOL> % nbase ) <EOL> class TestExports ( BaseTestCase ) : <EOL> def test_exports ( self ) : <EOL> dict = tempfile . __dict__ <EOL> expected = { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> unexp = [ ] <EOL> for key in dict : <EOL> if key [ <NUM_LIT:0> ] != '<STR_LIT:_>' and key not in expected : <EOL> unexp . append ( key ) <EOL> self . assertTrue ( len ( unexp ) == <NUM_LIT:0> , <EOL> "<STR_LIT>" % unexp ) <EOL> class TestRandomNameSequence ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . r = tempfile . _RandomNameSequence ( ) <EOL> super ( ) . setUp ( ) <EOL> def test_get_six_char_str ( self ) : <EOL> s = next ( self . r ) <EOL> self . nameCheck ( s , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_many ( self ) : <EOL> dict = { } <EOL> r = self . r <EOL> for i in range ( TEST_FILES ) : <EOL> s = next ( r ) <EOL> self . nameCheck ( s , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertNotIn ( s , dict ) <EOL> dict [ s ] = <NUM_LIT:1> <EOL> def supports_iter ( self ) : <EOL> i = <NUM_LIT:0> <EOL> r = self . r <EOL> for s in r : <EOL> i += <NUM_LIT:1> <EOL> if i == <NUM_LIT:20> : <EOL> break <EOL> @ unittest . skipUnless ( hasattr ( os , '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) <EOL> def test_process_awareness ( self ) : <EOL> read_fd , write_fd = os . pipe ( ) <EOL> pid = None <EOL> try : <EOL> pid = os . fork ( ) <EOL> if not pid : <EOL> os . close ( read_fd ) <EOL> os . write ( write_fd , next ( self . r ) . encode ( "<STR_LIT:ascii>" ) ) <EOL> os . close ( write_fd ) <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> parent_value = next ( self . r ) <EOL> child_value = os . read ( read_fd , len ( parent_value ) ) . decode ( "<STR_LIT:ascii>" ) <EOL> finally : <EOL> if pid : <EOL> try : <EOL> os . kill ( pid , signal . SIGKILL ) <EOL> except EnvironmentError : <EOL> pass <EOL> os . close ( read_fd ) <EOL> os . close ( write_fd ) <EOL> self . assertNotEqual ( child_value , parent_value ) <EOL> class TestCandidateTempdirList ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_nonempty_list ( self ) : <EOL> cand = tempfile . _candidate_tempdir_list ( ) <EOL> self . assertFalse ( len ( cand ) == <NUM_LIT:0> ) <EOL> for c in cand : <EOL> self . assertIsInstance ( c , str ) <EOL> def test_wanted_dirs ( self ) : <EOL> with support . EnvironmentVarGuard ( ) as env : <EOL> for envname in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> dirname = os . getenv ( envname ) <EOL> if not dirname : <EOL> env [ envname ] = os . path . abspath ( envname ) <EOL> cand = tempfile . _candidate_tempdir_list ( ) <EOL> for envname in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> dirname = os . getenv ( envname ) <EOL> if not dirname : raise ValueError <EOL> self . assertIn ( dirname , cand ) <EOL> try : <EOL> dirname = os . getcwd ( ) <EOL> except ( AttributeError , os . error ) : <EOL> dirname = os . curdir <EOL> self . assertIn ( dirname , cand ) <EOL> class TestGetDefaultTempdir ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_no_files_left_behind ( self ) : <EOL> with tempfile . TemporaryDirectory ( ) as our_temp_directory : <EOL> def our_candidate_list ( ) : <EOL> return [ our_temp_directory ] <EOL> with support . swap_attr ( tempfile , "<STR_LIT>" , <EOL> our_candidate_list ) : <EOL> tempfile . _get_default_tempdir ( ) <EOL> self . assertEqual ( os . listdir ( our_temp_directory ) , [ ] ) <EOL> def raise_OSError ( * args , ** kwargs ) : <EOL> raise OSError ( ) <EOL> with support . swap_attr ( io , "<STR_LIT>" , raise_OSError ) : <EOL> with self . assertRaises ( FileNotFoundError ) : <EOL> tempfile . _get_default_tempdir ( ) <EOL> self . assertEqual ( os . listdir ( our_temp_directory ) , [ ] ) <EOL> open = io . open <EOL> def bad_writer ( * args , ** kwargs ) : <EOL> fp = open ( * args , ** kwargs ) <EOL> fp . write = raise_OSError <EOL> return fp <EOL> with support . swap_attr ( io , "<STR_LIT>" , bad_writer ) : <EOL> with self . assertRaises ( FileNotFoundError ) : <EOL> tempfile . _get_default_tempdir ( ) <EOL> self . assertEqual ( os . listdir ( our_temp_directory ) , [ ] ) <EOL> class TestGetCandidateNames ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_retval ( self ) : <EOL> obj = tempfile . _get_candidate_names ( ) <EOL> self . assertIsInstance ( obj , tempfile . _RandomNameSequence ) <EOL> def test_same_thing ( self ) : <EOL> a = tempfile . _get_candidate_names ( ) <EOL> b = tempfile . _get_candidate_names ( ) <EOL> self . assertTrue ( a is b ) <EOL> @ contextlib . contextmanager <EOL> def _inside_empty_temp_dir ( ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> with support . swap_attr ( tempfile , '<STR_LIT>' , dir ) : <EOL> yield <EOL> finally : <EOL> support . rmtree ( dir ) <EOL> def _mock_candidate_names ( * names ) : <EOL> return support . swap_attr ( tempfile , <EOL> '<STR_LIT>' , <EOL> lambda : iter ( names ) ) <EOL> class TestMkstempInner ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> class mkstemped : <EOL> _bflags = tempfile . _bin_openflags <EOL> _tflags = tempfile . _text_openflags <EOL> _close = os . close <EOL> _unlink = os . unlink <EOL> def __init__ ( self , dir , pre , suf , bin ) : <EOL> if bin : flags = self . _bflags <EOL> else : flags = self . _tflags <EOL> ( self . fd , self . name ) = tempfile . _mkstemp_inner ( dir , pre , suf , flags ) <EOL> def write ( self , str ) : <EOL> os . write ( self . fd , str ) <EOL> def __del__ ( self ) : <EOL> self . _close ( self . fd ) <EOL> self . _unlink ( self . name ) <EOL> def do_create ( self , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" , bin = <NUM_LIT:1> ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> file = self . mkstemped ( dir , pre , suf , bin ) <EOL> self . nameCheck ( file . name , dir , pre , suf ) <EOL> return file <EOL> def test_basic ( self ) : <EOL> self . do_create ( ) . write ( b"<STR_LIT>" ) <EOL> self . do_create ( pre = "<STR_LIT:a>" ) . write ( b"<STR_LIT>" ) <EOL> self . do_create ( suf = "<STR_LIT:b>" ) . write ( b"<STR_LIT>" ) <EOL> self . do_create ( pre = "<STR_LIT:a>" , suf = "<STR_LIT:b>" ) . write ( b"<STR_LIT>" ) <EOL> self . do_create ( pre = "<STR_LIT>" , suf = "<STR_LIT>" ) . write ( b"<STR_LIT>" ) <EOL> def test_basic_many ( self ) : <EOL> extant = list ( range ( TEST_FILES ) ) <EOL> for i in extant : <EOL> extant [ i ] = self . do_create ( pre = "<STR_LIT>" ) <EOL> def test_choose_directory ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> self . do_create ( dir = dir ) . write ( b"<STR_LIT>" ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> def test_file_mode ( self ) : <EOL> if not has_stat : <EOL> return <EOL> file = self . do_create ( ) <EOL> mode = stat . S_IMODE ( os . stat ( file . name ) . st_mode ) <EOL> expected = <NUM_LIT> <EOL> if sys . platform in ( '<STR_LIT:win32>' , '<STR_LIT>' ) : <EOL> user = expected >> <NUM_LIT:6> <EOL> expected = user * ( <NUM_LIT:1> + <NUM_LIT:8> + <NUM_LIT:64> ) <EOL> self . assertEqual ( mode , expected ) <EOL> def test_noinherit ( self ) : <EOL> if not has_spawnl : <EOL> return <EOL> if support . verbose : <EOL> v = "<STR_LIT:v>" <EOL> else : <EOL> v = "<STR_LIT:q>" <EOL> file = self . do_create ( ) <EOL> fd = "<STR_LIT>" % file . fd <EOL> try : <EOL> me = __file__ <EOL> except NameError : <EOL> me = sys . argv [ <NUM_LIT:0> ] <EOL> tester = os . path . join ( os . path . dirname ( os . path . abspath ( me ) ) , <EOL> "<STR_LIT>" ) <EOL> if sys . platform in ( '<STR_LIT:win32>' , ) : <EOL> decorated = '<STR_LIT>' % sys . executable <EOL> tester = '<STR_LIT>' % tester <EOL> else : <EOL> decorated = sys . executable <EOL> retval = os . spawnl ( os . P_WAIT , sys . executable , decorated , tester , v , fd ) <EOL> self . assertFalse ( retval < <NUM_LIT:0> , <EOL> "<STR_LIT>" % - retval ) <EOL> self . assertFalse ( retval > <NUM_LIT:0> , "<STR_LIT>" % retval ) <EOL> def test_textmode ( self ) : <EOL> if not has_textmode : <EOL> return <EOL> f = self . do_create ( bin = <NUM_LIT:0> ) <EOL> f . write ( b"<STR_LIT>" ) <EOL> f . write ( b"<STR_LIT>" ) <EOL> os . lseek ( f . fd , <NUM_LIT:0> , os . SEEK_SET ) <EOL> self . assertEqual ( os . read ( f . fd , <NUM_LIT:20> ) , b"<STR_LIT>" ) <EOL> def default_mkstemp_inner ( self ) : <EOL> return tempfile . _mkstemp_inner ( tempfile . gettempdir ( ) , <EOL> tempfile . template , <EOL> '<STR_LIT>' , <EOL> tempfile . _bin_openflags ) <EOL> def test_collision_with_existing_file ( self ) : <EOL> with _inside_empty_temp_dir ( ) , _mock_candidate_names ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ( fd1 , name1 ) = self . default_mkstemp_inner ( ) <EOL> os . close ( fd1 ) <EOL> self . assertTrue ( name1 . endswith ( '<STR_LIT>' ) ) <EOL> ( fd2 , name2 ) = self . default_mkstemp_inner ( ) <EOL> os . close ( fd2 ) <EOL> self . assertTrue ( name2 . endswith ( '<STR_LIT>' ) ) <EOL> def test_collision_with_existing_directory ( self ) : <EOL> with _inside_empty_temp_dir ( ) , _mock_candidate_names ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> self . assertTrue ( dir . endswith ( '<STR_LIT>' ) ) <EOL> ( fd , name ) = self . default_mkstemp_inner ( ) <EOL> os . close ( fd ) <EOL> self . assertTrue ( name . endswith ( '<STR_LIT>' ) ) <EOL> class TestGetTempPrefix ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_sane_template ( self ) : <EOL> p = tempfile . gettempprefix ( ) <EOL> self . assertIsInstance ( p , str ) <EOL> self . assertTrue ( len ( p ) > <NUM_LIT:0> ) <EOL> def test_usable_template ( self ) : <EOL> p = tempfile . gettempprefix ( ) + "<STR_LIT>" <EOL> d = tempfile . mkdtemp ( prefix = "<STR_LIT>" ) <EOL> try : <EOL> p = os . path . join ( d , p ) <EOL> fd = os . open ( p , os . O_RDWR | os . O_CREAT ) <EOL> os . close ( fd ) <EOL> os . unlink ( p ) <EOL> finally : <EOL> os . rmdir ( d ) <EOL> class TestGetTempDir ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_directory_exists ( self ) : <EOL> dir = tempfile . gettempdir ( ) <EOL> self . assertTrue ( os . path . isabs ( dir ) or dir == os . curdir , <EOL> "<STR_LIT>" % dir ) <EOL> self . assertTrue ( os . path . isdir ( dir ) , <EOL> "<STR_LIT>" % dir ) <EOL> def test_directory_writable ( self ) : <EOL> file = tempfile . NamedTemporaryFile ( ) <EOL> file . write ( b"<STR_LIT>" ) <EOL> file . close ( ) <EOL> def test_same_thing ( self ) : <EOL> a = tempfile . gettempdir ( ) <EOL> b = tempfile . gettempdir ( ) <EOL> self . assertTrue ( a is b ) <EOL> class TestMkstemp ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def do_create ( self , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> ( fd , name ) = tempfile . mkstemp ( dir = dir , prefix = pre , suffix = suf ) <EOL> ( ndir , nbase ) = os . path . split ( name ) <EOL> adir = os . path . abspath ( dir ) <EOL> self . assertEqual ( adir , ndir , <EOL> "<STR_LIT>" % ( adir , ndir ) ) <EOL> try : <EOL> self . nameCheck ( name , dir , pre , suf ) <EOL> finally : <EOL> os . close ( fd ) <EOL> os . unlink ( name ) <EOL> def test_basic ( self ) : <EOL> self . do_create ( ) <EOL> self . do_create ( pre = "<STR_LIT:a>" ) <EOL> self . do_create ( suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT:a>" , suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT>" , suf = "<STR_LIT>" ) <EOL> self . do_create ( dir = "<STR_LIT:.>" ) <EOL> def test_choose_directory ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> self . do_create ( dir = dir ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> class TestMkdtemp ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def do_create ( self , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> name = tempfile . mkdtemp ( dir = dir , prefix = pre , suffix = suf ) <EOL> try : <EOL> self . nameCheck ( name , dir , pre , suf ) <EOL> return name <EOL> except : <EOL> os . rmdir ( name ) <EOL> raise <EOL> def test_basic ( self ) : <EOL> os . rmdir ( self . do_create ( ) ) <EOL> os . rmdir ( self . do_create ( pre = "<STR_LIT:a>" ) ) <EOL> os . rmdir ( self . do_create ( suf = "<STR_LIT:b>" ) ) <EOL> os . rmdir ( self . do_create ( pre = "<STR_LIT:a>" , suf = "<STR_LIT:b>" ) ) <EOL> os . rmdir ( self . do_create ( pre = "<STR_LIT>" , suf = "<STR_LIT>" ) ) <EOL> def test_basic_many ( self ) : <EOL> extant = list ( range ( TEST_FILES ) ) <EOL> try : <EOL> for i in extant : <EOL> extant [ i ] = self . do_create ( pre = "<STR_LIT>" ) <EOL> finally : <EOL> for i in extant : <EOL> if ( isinstance ( i , str ) ) : <EOL> os . rmdir ( i ) <EOL> def test_choose_directory ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> os . rmdir ( self . do_create ( dir = dir ) ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> def test_mode ( self ) : <EOL> if not has_stat : <EOL> return <EOL> dir = self . do_create ( ) <EOL> try : <EOL> mode = stat . S_IMODE ( os . stat ( dir ) . st_mode ) <EOL> mode &= <NUM_LIT> <EOL> expected = <NUM_LIT> <EOL> if sys . platform in ( '<STR_LIT:win32>' , '<STR_LIT>' ) : <EOL> user = expected >> <NUM_LIT:6> <EOL> expected = user * ( <NUM_LIT:1> + <NUM_LIT:8> + <NUM_LIT:64> ) <EOL> self . assertEqual ( mode , expected ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> def test_collision_with_existing_file ( self ) : <EOL> with _inside_empty_temp_dir ( ) , _mock_candidate_names ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> file = tempfile . NamedTemporaryFile ( delete = False ) <EOL> file . close ( ) <EOL> self . assertTrue ( file . name . endswith ( '<STR_LIT>' ) ) <EOL> dir = tempfile . mkdtemp ( ) <EOL> self . assertTrue ( dir . endswith ( '<STR_LIT>' ) ) <EOL> def test_collision_with_existing_directory ( self ) : <EOL> with _inside_empty_temp_dir ( ) , _mock_candidate_names ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> dir1 = tempfile . mkdtemp ( ) <EOL> self . assertTrue ( dir1 . endswith ( '<STR_LIT>' ) ) <EOL> dir2 = tempfile . mkdtemp ( ) <EOL> self . assertTrue ( dir2 . endswith ( '<STR_LIT>' ) ) <EOL> class TestMktemp ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . dir = tempfile . mkdtemp ( ) <EOL> super ( ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> if self . dir : <EOL> os . rmdir ( self . dir ) <EOL> self . dir = None <EOL> super ( ) . tearDown ( ) <EOL> class mktemped : <EOL> _unlink = os . unlink <EOL> _bflags = tempfile . _bin_openflags <EOL> def __init__ ( self , dir , pre , suf ) : <EOL> self . name = tempfile . mktemp ( dir = dir , prefix = pre , suffix = suf ) <EOL> os . close ( os . open ( self . name , self . _bflags , <NUM_LIT> ) ) <EOL> def __del__ ( self ) : <EOL> self . _unlink ( self . name ) <EOL> def do_create ( self , pre = "<STR_LIT>" , suf = "<STR_LIT>" ) : <EOL> file = self . mktemped ( self . dir , pre , suf ) <EOL> self . nameCheck ( file . name , self . dir , pre , suf ) <EOL> return file <EOL> def test_basic ( self ) : <EOL> self . do_create ( ) <EOL> self . do_create ( pre = "<STR_LIT:a>" ) <EOL> self . do_create ( suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT:a>" , suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT>" , suf = "<STR_LIT>" ) <EOL> def test_many ( self ) : <EOL> extant = list ( range ( TEST_FILES ) ) <EOL> for i in extant : <EOL> extant [ i ] = self . do_create ( pre = "<STR_LIT>" ) <EOL> class TestNamedTemporaryFile ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def do_create ( self , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" , delete = True ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> file = tempfile . NamedTemporaryFile ( dir = dir , prefix = pre , suffix = suf , <EOL> delete = delete ) <EOL> self . nameCheck ( file . name , dir , pre , suf ) <EOL> return file <EOL> def test_basic ( self ) : <EOL> self . do_create ( ) <EOL> self . do_create ( pre = "<STR_LIT:a>" ) <EOL> self . do_create ( suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT:a>" , suf = "<STR_LIT:b>" ) <EOL> self . do_create ( pre = "<STR_LIT>" , suf = "<STR_LIT>" ) <EOL> def test_creates_named ( self ) : <EOL> f = tempfile . NamedTemporaryFile ( ) <EOL> self . assertTrue ( os . path . exists ( f . name ) , <EOL> "<STR_LIT>" % f . name ) <EOL> def test_del_on_close ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> f = tempfile . NamedTemporaryFile ( dir = dir ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> self . assertFalse ( os . path . exists ( f . name ) , <EOL> "<STR_LIT>" % f . name ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> def test_dis_del_on_close ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> tmp = None <EOL> try : <EOL> f = tempfile . NamedTemporaryFile ( dir = dir , delete = False ) <EOL> tmp = f . name <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> self . assertTrue ( os . path . exists ( f . name ) , <EOL> "<STR_LIT>" % f . name ) <EOL> finally : <EOL> if tmp is not None : <EOL> os . unlink ( tmp ) <EOL> os . rmdir ( dir ) <EOL> def test_multiple_close ( self ) : <EOL> f = tempfile . NamedTemporaryFile ( ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> def test_context_manager ( self ) : <EOL> with tempfile . NamedTemporaryFile ( ) as f : <EOL> self . assertTrue ( os . path . exists ( f . name ) ) <EOL> self . assertFalse ( os . path . exists ( f . name ) ) <EOL> def use_closed ( ) : <EOL> with f : <EOL> pass <EOL> self . assertRaises ( ValueError , use_closed ) <EOL> class TestSpooledTemporaryFile ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def do_create ( self , max_size = <NUM_LIT:0> , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> file = tempfile . SpooledTemporaryFile ( max_size = max_size , dir = dir , prefix = pre , suffix = suf ) <EOL> return file <EOL> def test_basic ( self ) : <EOL> f = self . do_create ( ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f = self . do_create ( max_size = <NUM_LIT:100> , pre = "<STR_LIT:a>" , suf = "<STR_LIT>" ) <EOL> self . assertFalse ( f . _rolled ) <EOL> def test_del_on_close ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:10> , dir = dir ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT>' * <NUM_LIT:5> ) <EOL> self . assertTrue ( f . _rolled ) <EOL> filename = f . name <EOL> f . close ( ) <EOL> self . assertFalse ( isinstance ( filename , str ) and os . path . exists ( filename ) , <EOL> "<STR_LIT>" % filename ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> def test_rewrite_small ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT:30> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> f . write ( b'<STR_LIT:x>' * <NUM_LIT:20> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> def test_write_sequential ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT:30> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT:x>' * <NUM_LIT:20> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT:x>' ) <EOL> self . assertTrue ( f . _rolled ) <EOL> def test_writelines ( self ) : <EOL> f = self . do_create ( ) <EOL> f . writelines ( ( b'<STR_LIT:x>' , b'<STR_LIT:y>' , b'<STR_LIT:z>' ) ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> buf = f . read ( ) <EOL> self . assertEqual ( buf , b'<STR_LIT>' ) <EOL> def test_writelines_sequential ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT> ) <EOL> f . writelines ( ( b'<STR_LIT:x>' * <NUM_LIT:20> , b'<STR_LIT:x>' * <NUM_LIT:10> , b'<STR_LIT:x>' * <NUM_LIT:5> ) ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT:x>' ) <EOL> self . assertTrue ( f . _rolled ) <EOL> def test_sparse ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT:30> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . seek ( <NUM_LIT:100> , <NUM_LIT:0> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT:x>' ) <EOL> self . assertTrue ( f . _rolled ) <EOL> def test_fileno ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT:30> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertTrue ( f . fileno ( ) > <NUM_LIT:0> ) <EOL> self . assertTrue ( f . _rolled ) <EOL> def test_multiple_close_before_rollover ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> self . assertFalse ( f . _rolled ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> def test_multiple_close_after_rollover ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:1> ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> self . assertTrue ( f . _rolled ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> def test_bound_methods ( self ) : <EOL> f = self . do_create ( max_size = <NUM_LIT:30> ) <EOL> read = f . read <EOL> write = f . write <EOL> seek = f . seek <EOL> write ( b"<STR_LIT:a>" * <NUM_LIT> ) <EOL> write ( b"<STR_LIT:b>" * <NUM_LIT> ) <EOL> seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . assertEqual ( read ( <NUM_LIT> ) , b'<STR_LIT:a>' * <NUM_LIT> + b'<STR_LIT:b>' * <NUM_LIT> ) <EOL> def test_properties ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:10> ) <EOL> f . write ( b'<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNone ( f . name ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> f . newlines <EOL> with self . assertRaises ( AttributeError ) : <EOL> f . encoding <EOL> f . write ( b'<STR_LIT:x>' ) <EOL> self . assertTrue ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNotNone ( f . name ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> f . newlines <EOL> with self . assertRaises ( AttributeError ) : <EOL> f . encoding <EOL> def test_text_mode ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( mode = '<STR_LIT>' , max_size = <NUM_LIT:10> ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNone ( f . name ) <EOL> self . assertIsNone ( f . newlines ) <EOL> self . assertIsNone ( f . encoding ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" ) <EOL> self . assertTrue ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNotNone ( f . name ) <EOL> self . assertEqual ( f . newlines , os . linesep ) <EOL> self . assertIsNotNone ( f . encoding ) <EOL> def test_text_newline_and_encoding ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( mode = '<STR_LIT>' , max_size = <NUM_LIT:10> , <EOL> newline = '<STR_LIT>' , encoding = '<STR_LIT:utf-8>' ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNone ( f . name ) <EOL> self . assertIsNone ( f . newlines ) <EOL> self . assertIsNone ( f . encoding ) <EOL> f . write ( "<STR_LIT>" * <NUM_LIT:20> + "<STR_LIT:\r\n>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . read ( ) , "<STR_LIT>" + ( "<STR_LIT>" * <NUM_LIT:20> ) + "<STR_LIT:\r\n>" ) <EOL> self . assertTrue ( f . _rolled ) <EOL> self . assertEqual ( f . mode , '<STR_LIT>' ) <EOL> self . assertIsNotNone ( f . name ) <EOL> self . assertIsNotNone ( f . newlines ) <EOL> self . assertEqual ( f . encoding , '<STR_LIT:utf-8>' ) <EOL> def test_context_manager_before_rollover ( self ) : <EOL> with tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:1> ) as f : <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertFalse ( f . closed ) <EOL> self . assertTrue ( f . closed ) <EOL> def use_closed ( ) : <EOL> with f : <EOL> pass <EOL> self . assertRaises ( ValueError , use_closed ) <EOL> def test_context_manager_during_rollover ( self ) : <EOL> with tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:1> ) as f : <EOL> self . assertFalse ( f . _rolled ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . flush ( ) <EOL> self . assertTrue ( f . _rolled ) <EOL> self . assertFalse ( f . closed ) <EOL> self . assertTrue ( f . closed ) <EOL> def use_closed ( ) : <EOL> with f : <EOL> pass <EOL> self . assertRaises ( ValueError , use_closed ) <EOL> def test_context_manager_after_rollover ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:1> ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . flush ( ) <EOL> self . assertTrue ( f . _rolled ) <EOL> with f : <EOL> self . assertFalse ( f . closed ) <EOL> self . assertTrue ( f . closed ) <EOL> def use_closed ( ) : <EOL> with f : <EOL> pass <EOL> self . assertRaises ( ValueError , use_closed ) <EOL> def test_truncate_with_size_parameter ( self ) : <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:10> ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> f . truncate ( ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertEqual ( f . _file . getvalue ( ) , b'<STR_LIT>' ) <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:10> ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . truncate ( <NUM_LIT:4> ) <EOL> self . assertFalse ( f . _rolled ) <EOL> self . assertEqual ( f . _file . getvalue ( ) , b'<STR_LIT>' ) <EOL> f = tempfile . SpooledTemporaryFile ( max_size = <NUM_LIT:10> ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . truncate ( <NUM_LIT:20> ) <EOL> self . assertTrue ( f . _rolled ) <EOL> if has_stat : <EOL> self . assertEqual ( os . fstat ( f . fileno ( ) ) . st_size , <NUM_LIT:20> ) <EOL> if tempfile . NamedTemporaryFile is not tempfile . TemporaryFile : <EOL> class TestTemporaryFile ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_basic ( self ) : <EOL> tempfile . TemporaryFile ( ) <EOL> def test_has_no_name ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> f = tempfile . TemporaryFile ( dir = dir ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> try : <EOL> os . rmdir ( dir ) <EOL> except : <EOL> f . close ( ) <EOL> os . rmdir ( dir ) <EOL> raise <EOL> def test_multiple_close ( self ) : <EOL> f = tempfile . TemporaryFile ( ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> def test_mode_and_encoding ( self ) : <EOL> def roundtrip ( input , * args , ** kwargs ) : <EOL> with tempfile . TemporaryFile ( * args , ** kwargs ) as fileobj : <EOL> fileobj . write ( input ) <EOL> fileobj . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( input , fileobj . read ( ) ) <EOL> roundtrip ( b"<STR_LIT>" , "<STR_LIT>" ) <EOL> roundtrip ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> roundtrip ( "<STR_LIT>" , "<STR_LIT>" , encoding = "<STR_LIT>" ) <EOL> roundtrip ( "<STR_LIT>" , "<STR_LIT>" , newline = "<STR_LIT>" ) <EOL> class NulledModules : <EOL> def __init__ ( self , * modules ) : <EOL> self . refs = [ mod . __dict__ for mod in modules ] <EOL> self . contents = [ ref . copy ( ) for ref in self . refs ] <EOL> def __enter__ ( self ) : <EOL> for d in self . refs : <EOL> for key in d : <EOL> d [ key ] = None <EOL> def __exit__ ( self , * exc_info ) : <EOL> for d , c in zip ( self . refs , self . contents ) : <EOL> d . clear ( ) <EOL> d . update ( c ) <EOL> class TestTemporaryDirectory ( BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def do_create ( self , dir = None , pre = "<STR_LIT>" , suf = "<STR_LIT>" , recurse = <NUM_LIT:1> ) : <EOL> if dir is None : <EOL> dir = tempfile . gettempdir ( ) <EOL> tmp = tempfile . TemporaryDirectory ( dir = dir , prefix = pre , suffix = suf ) <EOL> self . nameCheck ( tmp . name , dir , pre , suf ) <EOL> if recurse : <EOL> self . do_create ( tmp . name , pre , suf , recurse - <NUM_LIT:1> ) <EOL> with open ( os . path . join ( tmp . name , "<STR_LIT>" ) , "<STR_LIT:wb>" ) as f : <EOL> f . write ( b"<STR_LIT>" ) <EOL> return tmp <EOL> def test_mkdtemp_failure ( self ) : <EOL> with tempfile . TemporaryDirectory ( ) as nonexistent : <EOL> pass <EOL> with self . assertRaises ( FileNotFoundError ) as cm : <EOL> tempfile . TemporaryDirectory ( dir = nonexistent ) <EOL> self . assertEqual ( cm . exception . errno , errno . ENOENT ) <EOL> def test_explicit_cleanup ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> d = self . do_create ( dir = dir ) <EOL> self . assertTrue ( os . path . exists ( d . name ) , <EOL> "<STR_LIT>" % d . name ) <EOL> d . cleanup ( ) <EOL> self . assertFalse ( os . path . exists ( d . name ) , <EOL> "<STR_LIT>" % d . name ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> @ support . skip_unless_symlink <EOL> def test_cleanup_with_symlink_to_a_directory ( self ) : <EOL> d1 = self . do_create ( ) <EOL> d2 = self . do_create ( ) <EOL> os . symlink ( d2 . name , os . path . join ( d1 . name , "<STR_LIT:foo>" ) ) <EOL> d1 . cleanup ( ) <EOL> self . assertFalse ( os . path . exists ( d1 . name ) , <EOL> "<STR_LIT>" % d1 . name ) <EOL> self . assertTrue ( os . path . exists ( d2 . name ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( os . listdir ( d2 . name ) , [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> d2 . cleanup ( ) <EOL> @ support . cpython_only <EOL> def test_del_on_collection ( self ) : <EOL> dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> d = self . do_create ( dir = dir ) <EOL> name = d . name <EOL> del d <EOL> self . assertFalse ( os . path . exists ( name ) , <EOL> "<STR_LIT>" % name ) <EOL> finally : <EOL> os . rmdir ( dir ) <EOL> @ unittest . expectedFailure <EOL> def test_del_on_shutdown ( self ) : <EOL> with self . do_create ( ) as dir : <EOL> d = self . do_create ( dir = dir ) <EOL> modules = [ os , os . path ] <EOL> if has_stat : <EOL> modules . append ( stat ) <EOL> with support . captured_stderr ( ) as err : <EOL> with NulledModules ( * modules ) : <EOL> d . cleanup ( ) <EOL> d . _closed = True <EOL> self . assertFalse ( os . path . exists ( d . name ) , <EOL> "<STR_LIT>" % d . name ) <EOL> def test_warnings_on_cleanup ( self ) : <EOL> with self . do_create ( ) as dir : <EOL> if os . sep != '<STR_LIT:\\>' : <EOL> suffix = '<STR_LIT>' <EOL> else : <EOL> suffix = '<STR_LIT>' <EOL> d = self . do_create ( dir = dir , suf = suffix ) <EOL> modules = [ os , os . path ] <EOL> if has_stat : <EOL> modules . append ( stat ) <EOL> with support . captured_stderr ( ) as err : <EOL> with NulledModules ( * modules ) : <EOL> d . cleanup ( ) <EOL> message = err . getvalue ( ) . replace ( '<STR_LIT>' , '<STR_LIT:\\>' ) <EOL> self . assertIn ( "<STR_LIT>" , message ) <EOL> self . assertIn ( d . name , message ) <EOL> with support . check_warnings ( ( '<STR_LIT>' , ResourceWarning ) , quiet = False ) : <EOL> warnings . filterwarnings ( "<STR_LIT>" , category = ResourceWarning ) <EOL> d . __del__ ( ) <EOL> self . assertFalse ( os . path . exists ( d . name ) , <EOL> "<STR_LIT>" % d . name ) <EOL> def test_multiple_close ( self ) : <EOL> d = self . do_create ( ) <EOL> d . cleanup ( ) <EOL> d . cleanup ( ) <EOL> d . cleanup ( ) <EOL> def test_context_manager ( self ) : <EOL> d = self . do_create ( ) <EOL> with d as name : <EOL> self . assertTrue ( os . path . exists ( name ) ) <EOL> self . assertEqual ( name , d . name ) <EOL> self . assertFalse ( os . path . exists ( name ) ) <EOL> def test_main ( ) : <EOL> support . run_unittest ( __name__ ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
