Skip to content

Unclosed resource files: two potential fixes #120

@levon003

Description

@levon003

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_entries

Could 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 stream

Pull 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions