Skip to content

gh-87264: tarfile list() method does not show file type. #29974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1930,13 +1930,18 @@ def list(self, verbose=True, *, members=None):
output is produced. `members' is optional and must be a subset of the
list returned by getmembers().
"""
# Convert tarinfo type to stat type.
type2mode = {REGTYPE: stat.S_IFREG, SYMTYPE: stat.S_IFLNK,
FIFOTYPE: stat.S_IFIFO, CHRTYPE: stat.S_IFCHR,
DIRTYPE: stat.S_IFDIR, BLKTYPE: stat.S_IFBLK}
self._check()

if members is None:
members = self
for tarinfo in members:
if verbose:
_safe_print(stat.filemode(tarinfo.mode))
modetype = type2mode.get(tarinfo.type, 0)
_safe_print(stat.filemode(modetype | tarinfo.mode))
_safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
tarinfo.gname or tarinfo.gid))
if tarinfo.ischr() or tarinfo.isblk():
Expand Down
20 changes: 16 additions & 4 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,23 @@ def test_list_verbose(self):
# accessories if verbose flag is being used
# ...
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
# -rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
# drwxr-xr-x tarfile/tarfile 0 2003-01-05 15:19:43 ustar/dirtype/
# ...
self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 '
br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
br'ustar/\w+type ?\r?\n') * 2)
#
# Array of values to modify the regex below:
# ((file_type, file_permissions, file_length), ...)
type_perm_lengths = (
(r'\?', 'rw-r--r--', '7011'), ('-', 'rw-r--r--', '7011'),
('d', 'rwxr-xr-x', '0'), ('d', 'rwxr-xr-x', '255'),
(r'\?', 'rw-r--r--', '0'), ('l', 'rwxrwxrwx', '0'),
('b', 'rw-rw----', '3,0'), ('c', 'rw-rw-rw-', '1,3'),
('p', 'rw-r--r--', '0'))
self.assertRegex(out.decode(), ''.join(
[(tp + (r'%s tarfile/tarfile\s+%s ' % (perm, ln) +
r'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
r'ustar/\w+type[/>\sa-z-]*\n')) for tp, perm, ln
in type_perm_lengths]))
# Make sure it prints the source of link with verbose flag
self.assertIn(b'ustar/symtype -> regtype', out)
self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tarfile list() method shows file type correctly instead of as question marks.