Issue #33 new

open file handles never closed

damonlynch
created an issue

When metadata is extracted, e.g. self.parser = createParser(self.u_filename, self.filename) self.metadata = extractMetadata(self.parser) The file handle used by hachoir is never closed. Instructing python to forcefully delete the parser and metadata objects using the del statement does not even close the file handle. It is only closed when the process exits. This is most unfortunate if for instance the user wants to unmount a device and the process is still running.

Comments (9)

  1. miigotu

    Victor Stinner So in order to make my project support both python3 and python2.7 using hachoir, I need to selectively import either hachoir or hachoir3? I wish it were not so =P.

    Still have to look into how the parser and metadata stuff works, to try and fix this issue.

  2. Kuang-che Wu

    I think switching between hachoir and hachoir3 is infeasible because the api is not compatible. For example, some module paths and names are changed due to refactoring.

    Maybe you can try 3to2 on hachoir3. I never tried it and don't know if it works.

  3. miigotu

    Mark this solved.

    >>> parser = createParser(u'/home/dusted/Videos/True Detective/Season 02/True Detective - S02E02 - Night Finds You.mp4')
    >>> parser.stream._input
    <open file u'/home/dusted/Videos/True Detective/Season 02/True Detective - S02E02 - Night Finds You.mp4', mode 'rb' at 0x7f57f6a711e0>
    >>> parser.stream._input.close()
    >>> parser.stream._input
    <closed file u'/home/dusted/Videos/True Detective/Season 02/True Detective - S02E02 - Night Finds You.mp4', mode 'rb' at 0x7f57f6a711e0>
    
  4. Victor Stinner repo owner

    I fixed this issue in Hachoir3 by adding a close() method and support for the context manager ("with obj: ...") to parser and streams (input and output streams). I fixed examples and programs to use close() or the context manager to close input/ouput files as soon as possible.

    Hachoir3: https://github.com/haypo/hachoir3

  5. Log in to comment