Skip to content

Initialising Archive with a pathlib.Path fails. #3126

Closed
@GPRicci

Description

@GPRicci

Creating an instance of an Archive with a pathlib.Path fails with a ValueError "bad archive content".
Debugging the code, I suspect the following lines are responsible:

PyMuPDF/src/__init__.py

Lines 1749 to 1752 in 4d604d6

if hasattr(content, "name"):
content = content.name
elif isinstance(content, pathlib.Path):
content = str(content)

Since a Path object has an attribute name (and given that's checked first), the content variable receives the final path component.
Therefore, when the code checks if content is either a directory or file (lines 1754 and 1762), it will only be true if the final path element is either a subdirectory or file in the directory where the __init__.py file resides.

I tried inverting the conditions of the if and elif statements on the cited snippet, i.e check if content is an instance of Path before checking it has the attribute name, and it worked. However, given my limited knowledge of the library, I'm not sure if this would break in other scenarios.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions