Skip to content

docs: add an explicit close() call to urlopen examples without with #130132

Closed

Description

@opk12

Documentation

The documentation for urllib.request uses urlopen() 3 times without either with or manual closing. Although technically possible, this is not recommended.

It is also possible to achieve the same result without using the context manager approach.

>>> import urllib.request
>>> f = urllib.request.urlopen('http://www.python.org/')
>>> print(f.read(100).decode('utf-8'))

...
Use of Basic HTTP Authentication:

urllib.request.urlopen('http://www.example.com/login.html')

...
Adding HTTP headers:

r = urllib.request.urlopen(req)

This is a follow-up from ruff #13683.

Linked PRs

Activity

  1. added
    docsDocumentation in the Doc dir
    on Feb 14, 2025
  2. rruuaanng commented on Feb 17, 2025

    @rruuaanng
    Contributor

    Image

    Are you referring to this?

  3. picnixz commented on Feb 17, 2025

    @picnixz
    Member

    Although technically possible, this is not recommended.

    It's not that it's not recommended, it's just that it would only free resources upon object deletion and not upon request termination. But we should mention that the resources are indeed cleaned up but much later.

    EDIT: actually, I've checked and it's not even freed upon object deletion! I couldn't find any __del__ implementation which calls .close() so it's indeed better not to recommend it, or add an explicit close() call.

  4. changed the title [-][documentation] `urlopen` examples without `with`[/-] [+]docs: add an explicit `close()` call to `urlopen` examples without `with`[/+] on Feb 17, 2025
  5. added a commit that references this issue on Feb 18, 2025
  6. opk12 commented on Feb 18, 2025

    @opk12
    Author

    @Mr-Sunglasses @rruuaanng Proper closing should be added to all 3 quoted examples. As you may know, the audience includes learners and people from other fields than programming.

  7. opk12 commented on Feb 18, 2025

    @opk12
    Author

    To be honest, I'm not sure that the examples are worth keeping? Using a context manager without with deserves a bit of explanation and IMO the examples make a point that feels out of place in the docs for urllib.

  8. added 2 commits that reference this issue on Feb 18, 2025
  9. Mr-Sunglasses commented on Feb 18, 2025

    @Mr-Sunglasses
    Contributor

    @Mr-Sunglasses @rruuaanng Proper closing should be added to all 3 quoted examples. As you may know, the audience includes learners and people from other fields than programming.

    Thanks a lot @opk12 for suggestion, I've added explicit close() in all the remaining examples.

  10. Mr-Sunglasses commented on Feb 18, 2025

    @Mr-Sunglasses
    Contributor

    To be honest, I'm not sure that the examples are worth keeping? Using a context manager without with deserves a bit of explanation and IMO the examples make a point that feels out of place in the docs for urllib.

    I think the examples are pretty outdated, probably from the time when with does not exists ( completely agree on this

    Using a context manager without with deserves a bit of explanation

    and Yeah I agree as it feel very outdated, we need to work on them, as most of the examples does not work or the information is outdates.

    let's wait for the opinion of core dev's, what they think about this issue.

  11. added a commit that references this issue on Mar 18, 2025
  12. added a commit that references this issue on Mar 18, 2025
  13. added a commit that references this issue on Mar 18, 2025
  14. picnixz commented on Mar 18, 2025

    @picnixz
    Member

    Thank you for the report and the fix!

  15. added 2 commits that reference this issue on Mar 18, 2025
  16. added a commit that references this issue on Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions