Skip to content

Unable to read archive info if self.header.main_streams is None #665

@aborel

Description

@aborel

Describe the bug

SevenZipFile.archiveinfo() crashes if self.header.main_streams is None:

  File "/Users/XXXXXl/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/py7zr.py", line 982, in archiveinfo
    self._get_method_names(),
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/py7zr.py", line 788, in _get_method_names
    return get_methods_names([folder.coders for folder in self.header.main_streams.unpackinfo.folders])
                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'unpackinfo'

py7zr t also fails in that case:

py7zr t 7z.7z 
Testing archive: 7z.7z
--
Path = 7z.7z
Type = 7z
Physical Size = 143
Headers Size = 143
Traceback (most recent call last):
  File "/Users/XXXXX/virtualenv/acouachecksum/bin/py7zr", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/__main__.py", line 25, in main
    return cli.Cli().run()
           ~~~~~~~~~~~~~^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/cli.py", line 101, in run
    return args.func(args)
           ~~~~~~~~~^^^^^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/cli.py", line 307, in run_test
    self.print_archiveinfo(archive=a, file=file)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/cli.py", line 290, in print_archiveinfo
    file.write("Method = {}\n".format(", ".join(archive._get_method_names())))
                                                ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/XXXXX/virtualenv/acouachecksum/lib/python3.13/site-packages/py7zr/py7zr.py", line 788, in _get_method_names
    return get_methods_names([folder.coders for folder in self.header.main_streams.unpackinfo.folders])

This is probably a rare problem, my test file contains only a folder, containing a zero-byte file. However, other functions such as SevenZipFile._real_get_contents() can apparently tolerate the case so it would be elegant to deal with it more gracefully.

To Reproduce

Actual content:

7-Zip [64] 17.05 : Copyright (c) 1999-2021 Igor Pavlov : 2017-08-28
p7zip Version 17.05 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE)

Scanning the drive for archives:
1 file, 143 bytes (1 KiB)                      

Listing archive: /7z.7z

--
Path = 7z.7z
Type = 7z
Physical Size = 143
Headers Size = 143
Solid = -
Blocks = 0

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2025-06-16 13:02:54 D....            0            0  7z
2025-06-16 13:02:46 ....A            0            0  7z/Fichier_7z.txt
------------------- ----- ------------ ------------  ------------------------
2025-06-16 13:02:54                  0            0  1 files, 1 folders

Expected behavior

Not sure exactly but a more user-friendly error would at least be nice.

Environment (please complete the following information):

  • OS: MacOS 15.5
  • Python: 3.13
  • py7zr version: 1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions