Skip to content

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

Open

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

  1. vrzh commented on Sep 1, 2026

    @vrzh
    Author

    For now I just monkey patch the close function as follows:

    import io
    import compoundfiles
    
    
    def patched_close(self):
        io.RawIOBase.close(self)
        try:
            self._file.close()
        finally:
            self._file = None
    
    
    compoundfiles.streams.CompoundFileMiniStream.close = patched_close
    
    with compoundfiles.CompoundFileReader("foo.txm") as doc:
        entry = doc.root[0]
        if entry.isfile:
            with doc.open(entry) as stream:
                print(len(stream.read()))
  2. waveform80 commented on Sep 17, 2026

    @waveform80
    Member

    Huh, I'd almost forgotten about this ol' project! The suggestion sounds reasonable -- could you stick that in a PR? I should really bump the packaging on this up to something vaguely modern at the same time...

  3. added a commit that references this issue on Sep 28, 2026
    21135ae
  4. vrzh commented on Sep 28, 2026

    @vrzh
    Author

    Thanks! Created pull request #15. Sorry for the delay.

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