This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ValueError on repr(closed_socket_file)
Type: Stage:
Components: IO Versions: Python 3.2
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: nirs, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2011-01-03 23:51 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
issue10819.patch vstinner, 2011-01-03 23:52
socketio_name.patch vstinner, 2011-01-04 00:17
Messages (5)
msg125253 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-03 23:51
The following code raise a ValueError('I/O operation on closed file.'):
import socket
socket.socket(socket.SOCK_STREAM, socket.AF_INET)
s=socket.socket(socket.SOCK_STREAM, socket.AF_INET)

io.BufferedReader.__repr__() reads (, but self.buffer is closed.

io.BufferedReader.__repr__() catchs AttributeError when reading It should also maybe catch ValueError: attached patch does that.

socket.repr(x) returns a string with "[closed]". BufferedReader.repr() should maybe do the same.

Note: TextIOWrapper has the same issue.
msg125256 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-04 00:17
Antoine suggested me to patch property instead of the BufferedReader.__repr__() method: socketio_name.patch implements this idea.
msg125301 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-04 11:00
Fixed by r87730.
msg348390 - (view) Author: Nir Soffer (nirs) * Date: 2019-07-24 15:16
I find this new behavior a usability regression. Before this change, code
(e.g python 2 code ported to python 3) could do:

   fd = sock.fileno()

Without handling errors, since closed socket would raise (good). Now such code need to check the return value (bad):

   fd = sock.fileno()
   if fd == -1:

This is also not consistent with other objects:

>>> f = open("Makefile")
>>> f.fileno()
>>> f.close()
>>> f.fileno()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> repr(f)
"<_io.TextIOWrapper name='Makefile' mode='r' encoding='UTF-8'>"

The issue with repr() on closed socket can be mitigated easily inside __repr__, handling closed sockets without affecting code using file descriptors.

Can we return the old safe behavior?
msg348672 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-07-29 15:53
This issue is closed. Please open a new issue.
Date User Action Args
2022-04-11 14:57:10adminsetgithub: 55028
2019-07-29 15:53:31vstinnersetmessages: + msg348672
2019-07-24 15:16:38nirssetnosy: + nirs
messages: + msg348390
2011-01-04 11:00:59vstinnersetstatus: open -> closed

messages: + msg125301
resolution: fixed
nosy: pitrou, vstinner
2011-01-04 00:17:39vstinnersetfiles: + socketio_name.patch
nosy: pitrou, vstinner
messages: + msg125256
2011-01-03 23:52:10vstinnersetfiles: + issue10819.patch
nosy: pitrou, vstinner
keywords: + patch
2011-01-03 23:51:31vstinnercreate