Changaco / python-libarchive-c

Python interface to libarchive
Other
70 stars 37 forks source link

AttributeError: 'ArchiveEntry' object has no attribute '_pointer' when getting the entry format_name #126

Closed mxmlnkn closed 4 months ago

mxmlnkn commented 4 months ago

Reproducer:

import io
import os
import tarfile
import libarchive

file_name = "tar-with-single-file.tar.bz2"
if os.path.exists(file_name):
    raise RuntimeError("Will not overwrite existing file!")

with tarfile.open(file_name, "w:bz2") as archive:
    archive.addfile(tarfile.TarInfo("foo"), fileobj=io.BytesIO(b"bar"))

try:
    with libarchive.file_reader(file_name) as archive:
        entry = next(iter(archive))
        print("format name:", entry.format_name)
    print("Done")
finally:
    os.remove(file_name)

Error:

Traceback (most recent call last):
  File "test_format_name.py", line 16, in <module>
    print("format name:", entry.format_name)
  File "~/.local/lib/python3.10/site-packages/libarchive/entry.py", line 438, in format_name
    return ffi.format_name(self._pointer)
AttributeError: 'ArchiveEntry' object has no attribute '_pointer'

It might be a good idea to add static analysis such as pylint, which would have noticed this kind of error. Unfortunately, there are a lot of false positives because of the ffi module, which I had to filter:

> pylint libarchive/*py | grep ': E' | grep -v "[Mm]odule 'libarchive.ffi"
libarchive/entry.py:374:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
libarchive/entry.py:438:31: E1101: Instance of 'ArchiveEntry' has no '_pointer' member (no-member)
libarchive/ffi.py:70:0: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)