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.

classification
Title: socket.makefile does not handle line buffering
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: giampaolo.rodola, kchen, vxgmichel
Priority: normal Keywords: patch

Created on 2017-07-27 21:11 by kchen, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 12370 open vxgmichel, 2019-03-16 17:20
Messages (2)
msg299351 - (view) Author: Kevin Chen (kchen) Date: 2017-07-27 21:11
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
msg338088 - (view) Author: Vincent Michel (vxgmichel) * Date: 2019-03-16 17:21
I ran into this issue too so I went ahead and created a pull request (https://github.com/python/cpython/pull/12370).
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75245
2019-03-17 15:51:57cheryl.sabellasetnosy: + giampaolo.rodola

versions: - Python 3.6
2019-03-16 17:21:51vxgmichelsetnosy: + vxgmichel

messages: + msg338088
versions: + Python 3.7, Python 3.8
2019-03-16 17:20:24vxgmichelsetkeywords: + patch
stage: patch review
pull_requests: + pull_request12333
2017-07-27 21:11:45kchencreate