File tree 2 files changed +13
-3
lines changed 2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,8 @@ def S_ISWHT(mode):
126
126
127
127
128
128
_filemode_table = (
129
+ # File type chars according to:
130
+ # http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h
129
131
((S_IFLNK , "l" ),
130
132
(S_IFSOCK , "s" ), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR
131
133
(S_IFREG , "-" ),
@@ -156,13 +158,17 @@ def S_ISWHT(mode):
156
158
def filemode (mode ):
157
159
"""Convert a file's mode to a string of the form '-rwxrwxrwx'."""
158
160
perm = []
159
- for table in _filemode_table :
161
+ for index , table in enumerate ( _filemode_table ) :
160
162
for bit , char in table :
161
163
if mode & bit == bit :
162
164
perm .append (char )
163
165
break
164
166
else :
165
- perm .append ("-" )
167
+ if index == 0 :
168
+ # Unknown filetype
169
+ perm .append ("?" )
170
+ else :
171
+ perm .append ("-" )
166
172
return "" .join (perm )
167
173
168
174
Original file line number Diff line number Diff line change @@ -122,8 +122,11 @@ def test_mode(self):
122
122
st_mode , modestr = self .get_mode ()
123
123
self .assertEqual (modestr , '-rwx------' )
124
124
self .assertS_IS ("REG" , st_mode )
125
- self .assertEqual (self .statmod .S_IMODE (st_mode ),
125
+ imode = self .statmod .S_IMODE (st_mode )
126
+ self .assertEqual (imode ,
126
127
self .statmod .S_IRWXU )
128
+ self .assertEqual (self .statmod .filemode (imode ),
129
+ '?rwx------' )
127
130
128
131
os .chmod (TESTFN , 0o070 )
129
132
st_mode , modestr = self .get_mode ()
@@ -238,6 +241,7 @@ def test_file_attribute_constants(self):
238
241
self .assertEqual (value , modvalue , key )
239
242
240
243
244
+ @unittest .skipIf (c_stat is None , 'need _stat extension' )
241
245
class TestFilemodeCStat (TestFilemode , unittest .TestCase ):
242
246
statmod = c_stat
243
247
You can’t perform that action at this time.
0 commit comments