<s> import unittest <EOL> import os <EOL> from test . support import TESTFN , run_unittest , import_fresh_module <EOL> import stat <EOL> class TestFilemode ( unittest . TestCase ) : <EOL> file_flags = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> formats = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' } <EOL> format_funcs = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' } <EOL> stat_struct = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , <EOL> '<STR_LIT>' : <NUM_LIT:9> } <EOL> permission_bits = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> } <EOL> def setUp ( self ) : <EOL> try : <EOL> os . remove ( TESTFN ) <EOL> except OSError : <EOL> try : <EOL> os . rmdir ( TESTFN ) <EOL> except OSError : <EOL> pass <EOL> tearDown = setUp <EOL> def get_mode ( self , fname = TESTFN , lstat = True ) : <EOL> if lstat : <EOL> st_mode = os . lstat ( fname ) . st_mode <EOL> else : <EOL> st_mode = os . stat ( fname ) . st_mode <EOL> modestr = stat . filemode ( st_mode ) <EOL> return st_mode , modestr <EOL> def assertS_IS ( self , name , mode ) : <EOL> fmt = getattr ( stat , "<STR_LIT>" + name . lstrip ( "<STR_LIT:F>" ) ) <EOL> self . assertEqual ( stat . S_IFMT ( mode ) , fmt ) <EOL> testname = "<STR_LIT>" + name <EOL> for funcname in self . format_funcs : <EOL> func = getattr ( stat , funcname , None ) <EOL> if func is None : <EOL> if funcname == testname : <EOL> raise ValueError ( funcname ) <EOL> continue <EOL> if funcname == testname : <EOL> self . assertTrue ( func ( mode ) ) <EOL> else : <EOL> self . assertFalse ( func ( mode ) ) <EOL> def test_mode ( self ) : <EOL> with open ( TESTFN , '<STR_LIT:w>' ) : <EOL> pass <EOL> if os . name == '<STR_LIT>' : <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> self . assertEqual ( stat . S_IMODE ( st_mode ) , <EOL> stat . S_IRWXU ) <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> self . assertEqual ( stat . S_IMODE ( st_mode ) , <EOL> stat . S_IRWXG ) <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> self . assertEqual ( stat . S_IMODE ( st_mode ) , <EOL> stat . S_IRWXO ) <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> self . assertEqual ( stat . S_IMODE ( st_mode ) , <NUM_LIT> ) <EOL> else : <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr [ : <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> self . assertEqual ( stat . S_IFMT ( st_mode ) , <EOL> stat . S_IFREG ) <EOL> def test_directory ( self ) : <EOL> os . mkdir ( TESTFN ) <EOL> os . chmod ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> if os . name == '<STR_LIT>' : <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> else : <EOL> self . assertEqual ( modestr [ <NUM_LIT:0> ] , '<STR_LIT:d>' ) <EOL> @ unittest . skipUnless ( hasattr ( os , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_link ( self ) : <EOL> try : <EOL> os . symlink ( os . getcwd ( ) , TESTFN ) <EOL> except ( OSError , NotImplementedError ) as err : <EOL> raise unittest . SkipTest ( str ( err ) ) <EOL> else : <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr [ <NUM_LIT:0> ] , '<STR_LIT:l>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> @ unittest . skipUnless ( hasattr ( os , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_fifo ( self ) : <EOL> os . mkfifo ( TESTFN , <NUM_LIT> ) <EOL> st_mode , modestr = self . get_mode ( ) <EOL> self . assertEqual ( modestr , '<STR_LIT>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> @ unittest . skipUnless ( os . name == '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_devices ( self ) : <EOL> if os . path . exists ( os . devnull ) : <EOL> st_mode , modestr = self . get_mode ( os . devnull , lstat = False ) <EOL> self . assertEqual ( modestr [ <NUM_LIT:0> ] , '<STR_LIT:c>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> for blockdev in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if os . path . exists ( blockdev ) : <EOL> st_mode , modestr = self . get_mode ( blockdev , lstat = False ) <EOL> self . assertEqual ( modestr [ <NUM_LIT:0> ] , '<STR_LIT:b>' ) <EOL> self . assertS_IS ( "<STR_LIT>" , st_mode ) <EOL> break <EOL> def test_module_attributes ( self ) : <EOL> for key , value in self . stat_struct . items ( ) : <EOL> modvalue = getattr ( stat , key ) <EOL> self . assertEqual ( value , modvalue , key ) <EOL> for key , value in self . permission_bits . items ( ) : <EOL> modvalue = getattr ( stat , key ) <EOL> self . assertEqual ( value , modvalue , key ) <EOL> for key in self . file_flags : <EOL> modvalue = getattr ( stat , key ) <EOL> self . assertIsInstance ( modvalue , int ) <EOL> for key in self . formats : <EOL> modvalue = getattr ( stat , key ) <EOL> self . assertIsInstance ( modvalue , int ) <EOL> for key in self . format_funcs : <EOL> func = getattr ( stat , key ) <EOL> self . assertTrue ( callable ( func ) ) <EOL> self . assertEqual ( func ( <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> def test_main ( ) : <EOL> run_unittest ( TestFilemode ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
