-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Hi,
I'm not highly fluent with Python streams, but I've noticed a few ResourceWarnings while using this library: cmudict/__init__.py:155: ResourceWarning: unclosed file <_io.BufferedReader name='<snip>/cmudict/data/cmudict.dict'>
It seems to me there are two plausible ways to resolve this issue:
1. Close exhausted streams explicitly in entries
_entries() consumes the full stream, so it should be safe to call close() on the generated dict_stream().
def entries():
- cmu_entries = _entries(dict_stream(), "#")
+ with dict_stream() as stream:
+ cmu_entries = _entries(stream, "#")
return cmu_entriesCould do the same in vp().
2. Register newly-opened streams with the file_manager
Cmudict already creates a contextlib.ExitStack on import; we could add newly-opened streams to the ExitStack. That has the downside of preventing open file streams from being garbage-collected before termination, but the upside of avoiding the eventual ResourceWarning.
def _stream(resource_name):
stream = resources.files(__name__).joinpath(resource_name).open("rb")
+ stream = file_manager.enter_context(stream)
return streamPull request?
There could very well be something I'm missing that makes one or both of these options unappealing. Otherwise, option 1 is my recommendation, as it's cleaner to handle the stream life-cycle explicitly here. I'd be happy to cut a PR with this small patch if that would be useful.