Skip to content

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

Open
@maple3142

Description

@maple3142

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions