scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
412 stars 68 forks source link

Possible resource leakage #186

Closed pepeperezperez closed 5 years ago

pepeperezperez commented 6 years ago

Hi!

if I call: s = bitstring.ConstBitStream(filename='file.txt')

And I return after doing a s.find(), it seems like the file.txt is not getting closed. And I'm getting a ResourceWarning for unclosed file.

Maybe a call to source.close() in the _setfile() method of Bits.

scott-griffiths commented 6 years ago

Hi, thanks for the bug report.

I think that the file does get closed when s goes out of scope. Not sure to be honest without writing some code. So I believe the warning is just that and won't adversely affect things.

The real solution might be to implement __enter__ and __exit__ and allow the user to write with ConstBitStream(filename='file.txt') as s: to guarantee clean-up at a predictable time...

pepeperezperez commented 6 years ago

Great, thanks! Using it with context manager would be great!

scott-griffiths commented 5 years ago

I believe this is now fixed in the 3.1 update branch (and shortly master too).