You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
docs: add an explicit close() call to urlopen examples without with #130132
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'))
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.
changed the title [-][documentation] `urlopen` examples without `with`[/-][+]docs: add an explicit `close()` call to `urlopen` examples without `with`[/+]on Feb 17, 2025
@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.
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.
@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.
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.
Activity
rruuaanng commentedon Feb 17, 2025
Are you referring to this?
picnixz commentedon Feb 17, 2025
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.[-][documentation] `urlopen` examples without `with`[/-][+]docs: add an explicit `close()` call to `urlopen` examples without `with`[/+]pythongh-130132: add an explicit close() call to urlopen examples wit…
opk12 commentedon Feb 18, 2025
@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.
opk12 commentedon Feb 18, 2025
To be honest, I'm not sure that the examples are worth keeping? Using a context manager without
withdeserves a bit of explanation and IMO the examples make a point that feels out of place in the docs for urllib.pythongh-130132: add an explicit close() in Use of Basic HTTP Authent…
pythongh-130132: add an explicit close() in Adding HTTP headers
Mr-Sunglasses commentedon Feb 18, 2025
Thanks a lot @opk12 for suggestion, I've added explicit
close()in all the remaining examples.Mr-Sunglasses commentedon Feb 18, 2025
I think the examples are pretty outdated, probably from the time when
withdoes not exists ( completely agree on thisand 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.
gh-130132: properly free resources in `urrlib.urlopen` examples (#130280
pythongh-130132: properly free resources in `urrlib.urlopen` examples (…
pythongh-130132: properly free resources in `urrlib.urlopen` examples (…
picnixz commentedon Mar 18, 2025
Thank you for the report and the fix!
[3.13] gh-130132: properly free resources in `urrlib.urlopen` examples (
[3.12] gh-130132: properly free resources in `urrlib.urlopen` examples (
pythongh-130132: properly free resources in `urrlib.urlopen` examples (…