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
socket.makefile does not handle line buffering #75245
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2017-07-27.21:11:45.908>labels= ['3.7', '3.8', 'type-bug', 'library']
title='socket.makefile does not handle line buffering'updated_at=<Date2019-03-17.15:51:57.119>user='https://bugs.python.org/kchen'
File objects generated with socket.makefile and that attempt to use line buffering appear to not actually use line buffering, at least for writing. In this example, the string does not appear to be written until the flush call.
First, set up a socket:
$ nc -l -U /tmp/foo
Then:
Python 3.6.2 (default, Jul 26 2017, 01:41:27)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
>>> s.connect("/tmp/foo")
>>> f = s.makefile("rw", buffering=1)
>>> f.write("asdf\n")
5
>>> f.flush()
The following patch appears to fix the problem:
--- socket.py.orig 2017-07-25 21:41:39.974554944 -0400+++ socket.py 2017-07-27 17:02:58.223353418 -0400@@ -253,7 +253,11 @@
buffer = io.BufferedWriter(raw, buffering)
if binary:
return buffer
- text = io.TextIOWrapper(buffer, encoding, errors, newline)+ line_buffering = False+ if buffering == 1:+ line_buffering = True+ text = io.TextIOWrapper(buffer, encoding, errors, newline,+ line_buffering)
text.mode = mode
return text
This worked in Python 2, where makefile() returned socket._fileobject, which implemented buffering itself: bufsize=1 meant line buffering for writing and default buffering for reading, in any mode, because there was no text and binary distinction at that level. It was lost when makefile() was reimplemented on top of the io module, where line buffering only exists in TextIOWrapper.
#12370 passes line_buffering to TextIOWrapper, so buffering=1 works again in text mode. In binary mode it now emits a RuntimeWarning and uses the default buffer size, as open() does; previously it silently created a 1-byte buffer.
LineBufferedFileObjectClassTestCase is a leftover from Python 2: it sets bufsize = 1, but uses binary mode, where line buffering has never worked in Python 3. It is replaced with a test in text mode.
Activity
kchen commentedon Jul 27, 2017
File objects generated with socket.makefile and that attempt to use line buffering appear to not actually use line buffering, at least for writing. In this example, the string does not appear to be written until the flush call.
First, set up a socket:
$ nc -l -U /tmp/foo
Then:
The following patch appears to fix the problem:
vxgmichel commentedon Mar 16, 2019
I ran into this issue too so I went ahead and created a pull request (#12370).
serhiy-storchaka commentedon Aug 12, 2026
This worked in Python 2, where
makefile()returnedsocket._fileobject, which implemented buffering itself:bufsize=1meant line buffering for writing and default buffering for reading, in any mode, because there was no text and binary distinction at that level. It was lost whenmakefile()was reimplemented on top of theiomodule, where line buffering only exists inTextIOWrapper.#12370 passes
line_bufferingtoTextIOWrapper, sobuffering=1works again in text mode. In binary mode it now emits aRuntimeWarningand uses the default buffer size, asopen()does; previously it silently created a 1-byte buffer.LineBufferedFileObjectClassTestCaseis a leftover from Python 2: it setsbufsize = 1, but uses binary mode, where line buffering has never worked in Python 3. It is replaced with a test in text mode.gh-75245: Support line buffering in socket.makefile() (GH-12370)
pythongh-75245: Support line buffering in socket.makefile() (pythonGH…