Skip to content

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

@opk12

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions