pypa / readme_renderer

Safely render long_description/README files in Warehouse
Apache License 2.0
158 stars 89 forks source link

Lazy open output files, and always close them #314

Closed kurtmckee closed 2 months ago

kurtmckee commented 3 months ago

This PR modifies the test suite to show all warnings, including ResourceWarning. By doing so, this revealed that the CLI code never closes output files opened by argparse during argument parsing.

Therefore, this PR additionally modifies the CLI code to open output files only when they will be written to, and ensures that they are always closed after writing.

kurtmckee commented 3 months ago

Rebased on main

kurtmckee commented 3 months ago

Great questions.

miketheman commented 2 months ago

... we needed to import, customize, and call the cli.main() function ourselves ...

Thanks for the context, and that makes sense.

The library was never fully intended as a CLI, so please be mindful of the cli namespace, as it is entire possible to change (as we ware changing it here). I'd be open to understanding the utility performed in the CLI that should be part of the general API, and thus decrease usage on cli.*

This PR changes that behavior so an output file is only created when rendered output is ready to be written, and ensures that resources are cleaned up afterward.

This makes most sense here.

Want me to switch to pytest -Wall instead of python -Wall -m pytest?

Yes please! If we have a specific reason to move it to the "outer" behavior you've described, then we can do that at a later time, but we should use the built-in if possible.

kurtmckee commented 2 months ago

Oh no, my commit signatures appear to have been broken by using GitHub's web UI to rebase on main. Let me circle back to this when I'm back at my PC tomorrow.

Regarding the cli.main() discussion -- I wanted to be clear that this was not something involving this project, it was just an example from my recent experience that "people use stuff in unexpected ways". 😀

miketheman commented 2 months ago

Thanks! Merging now.

If you'd like to craft a CHANGES.rst entry for 44.0, we can get these released. Here's an example of a previous PR for releases: https://github.com/pypa/readme_renderer/pull/303/