python / python/cpython

urllib.request.urlopen(data_uri).file are closed automatically when the response is not hold by a variable

Open
#92,713 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Opening a data uri with urllib.request.urlopen and read it directly will result in I/O operation on closed file..

Errored example:

from urllib.request import urlopen

url = 'data:text/plain,hello world'
print(urlopen(url).file.read())

"""
Output:

Traceback (most recent call last):
  File "/home/maple3142/tmp/xa.py", line 4, in <module>
    print(urlopen(url).file.read())
ValueError: I/O operation on closed file.
"""

Normal example:

from urllib.request import urlopen

url = 'data:text/plain,hello world'
resp = urlopen(url)
print(resp.file.read())

"""
Output:

b'hello world'
"""

Your environment

  • CPython versions tested on: 3.8.11, 3.10.4
  • Operating system and architecture: Arch Linux on WSL2 on Windows 10 x64

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two urllib.request.urlopen examples, then inspect the data-URI handling behind that entry point. Done means the direct chained .file.read() call works without a closed-file error while the existing stored-response example remains correct, with a regression test for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.