Changaco / python-libarchive-c

Python interface to libarchive
Other
70 stars 37 forks source link

Attempting to use ArchiveEntry outside of for loop doesn't work #104

Closed JonathonReinhart closed 2 years ago

JonathonReinhart commented 3 years ago

Test code:

import libarchive
import sys

def test1(path):
    """Normal iteration"""
    print("Test 1");
    with libarchive.file_reader(path) as ar:
        for e in ar:
            print("    ", e)

def test2(path):
    """Try saving an entry and using ouside of for loop"""
    print("Test 2");
    with libarchive.file_reader(path) as ar:
        for e in ar:
            e2 = e
            print("    First entry:", e2)
            break
        print("    First entry (outside):", e2)

def test3(path):
    """Try making a list of ArchiveEntry objects"""
    print("Test 3");
    with libarchive.file_reader(path) as ar:
        for e in list(ar):
            print("    ", e)

path = sys.argv[1]
test1(path)
test2(path)
#test3(path)

Output:

Test 1
     test.txt
Test 2
    First entry: test.txt
    First entry (outside): Traceback (most recent call last):
  File "bug.py", line 30, in <module>
    test2(path)
  File "bug.py", line 19, in test2
    print("    First entry (outside):", e2)
TypeError: __str__ returned non-string (type NoneType)

This error happens because ._getpathname() returns None.