[Python-ideas] Move tarfile.filemode() into stat module

Terry Reedy tjreedy at udel.edu
Sat May 12 19:16:05 CEST 2012


On 5/12/2012 10:41 AM, Antoine Pitrou wrote:
> On Sat, 12 May 2012 16:29:35 +0200
> Giampaolo Rodolà<g.rodola at gmail.com>
> wrote:
>> http://hg.python.org/cpython/file/9d9495fabeb9/Lib/tarfile.py#l304
>> I discovered this undocumented function by accident different years
>> ago and reused it a couple of times since then.
>> I think that leaving it hidden inside tarfile module is unfortunate.
>> What about moving it into stat module and document it?
>
> I don't know which of stat or shutil would be the better recipient, but
> it's a good idea anyway.

I think I would more likely look in stat, and as noted below, the 
constants used for the table used in the function are already in stat.py.

I checked, and

# Bits used in the mode field, values in octal.
#---------------------------------------------------------
S_IFLNK = 0o120000        # symbolic link
...

are only used in

filemode_table = (
     ((S_IFLNK,      "l"),
     ...

which is only used in

def filemode(mode): ...

So all three can be cleanly extracted into another module.

However 1) the bit definitions themselves should just be deleted as they 
*duplicate* those in stat.py. The S_Ixxx names are the same, the other 
names are variations of the other stat.S_Ixxxx names. So filemode_table 
(with '_' added?) could/should be re-written in stat.py to use the 
public, documented constants already defined there.

However 2) stat.py lacks the nice comments explaining the constants in 
the file itself, so I *would* copy the comments to the appropriate lines.


There only seems to be one use of the function in tarfile.py:
Line 1998:                 print(filemode(tarinfo.mode), end=' ')

All the other uses of 'filemode' are as a local name inside the open 
method, derived from its mode parameter:
             filemode, comptype = mode.split(":", 1)

+1 on moving the table (probably with private name, and using the 
existing, documented stat S_Ixxxx constants) and function (public) to 
stat.py.

-- 
Terry Jan Reedy





More information about the Python-ideas mailing list