Skip to content

CompoundFileMiniStream.close being called twice leading to "Exception ignored" warnings in recent Python versions #14

Description

@vrzh

Hi, thank you for the awesome project, it has recently been very useful to me and still works great at reading these old files. I think projects like these are very important.

There is one small fix I would like to ask for.

Running the most basic example:

import compoundfiles

with compoundfiles.CompoundFileReader("foo.txm") as doc:
    entry = doc.root[0]
    if entry.isfile:
        with doc.open(entry) as stream:
            print(len(stream.read()))

with a recent Python (I have 3.13), will read the data correctly but a warning will be printed:

Exception ignored in: <compoundfiles.streams.CompoundFileMiniStream object at 0x719e7de27280>
Traceback (most recent call last):
  File "/proj/.venv/lib/python3.13/site-packages/compoundfiles/streams.py", line 245, in close
AttributeError: 'NoneType' object has no attribute 'close'

I believe this is because the CompoundFileMiniStream.close function is being called twice

def close(self):

Here is a minimal example which reproduces the issue:

import io

class MyTestClass(io.RawIOBase):
    def __init__(self):
        print("MyTestClass.__init__")

    def close(self):
        print("MyTestClass.close")


with MyTestClass() as d:
    print("inside")

which will print out:

MyTestClass.__init__
inside
MyTestClass.close
MyTestClass.close

I believe the smallest fix it to also call the close() method in the base implementation, i.e.

import io

class MyTestClass(io.RawIOBase):
    def __init__(self):
        print("MyTestClass.__init__")

    def close(self):
        super().close() # this is the fix
        print("MyTestClass.close")


with MyTestClass() as d:
    print("inside")

So, my suggestion would be to change the CompoundFileMiniStream.close function to:

def close(self):
    super().close() # add this line
    try:
        self._file.close()
    finally:
        self._file = None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions