Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-31062: Allow socket.makefile to handle line buffering properly #12370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,12 @@ def makefile(self, mode="r", buffering=None, *,
rawmode += "w"
raw = SocketIO(self, rawmode)
self._io_refs += 1
line_buffering = False
if buffering is None:
buffering = -1
if buffering == 1:
buffering = -1
line_buffering = True
if buffering < 0:
buffering = io.DEFAULT_BUFFER_SIZE
if buffering == 0:
Expand All @@ -338,7 +342,8 @@ def makefile(self, mode="r", buffering=None, *,
if binary:
return buffer
encoding = io.text_encoding(encoding)
text = io.TextIOWrapper(buffer, encoding, errors, newline)
text = io.TextIOWrapper(
buffer, encoding, errors, newline, line_buffering)
text.mode = mode
return text

Expand Down
29 changes: 22 additions & 7 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4913,10 +4913,20 @@ def testReadline(self):
# Performing file readline test
line = self.read_file.readline()
self.assertEqual(line, self.read_msg)
# Readline mode
if self.bufsize == 1 and self.read_mode == "r":
self.assertTrue(self.read_file.line_buffering)

def _testReadline(self):
self.write_file.write(self.write_msg)
self.write_file.flush()
# Readline mode: no need to flush
if self.bufsize == 1 and self.write_mode == "w":
self.assertTrue(self.write_file.line_buffering)
else:
self.write_file.flush()
# Prevent garbage collection from flushing
# until the server has finished
self.assertTrue(self.serv_finished.wait(5.0))

def testCloseAfterMakefile(self):
# The file returned by makefile should keep the socket open.
Expand Down Expand Up @@ -5072,11 +5082,6 @@ def _testWriteNonBlocking(self):
self.serv_skipped = "failed to saturate the socket buffer"


class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):

bufsize = 1 # Default-buffered for reading; line-buffered for writing


class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):

bufsize = 2 # Exercise the buffering code
Expand Down Expand Up @@ -5112,6 +5117,16 @@ class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase):
newline = ''


class UnicodeLineBufferedFileObjectClassTestCase(FileObjectClassTestCase):

bufsize = 1 # Default-buffered for reading; line-buffered for writing
read_mode = 'r'
read_msg = MSG.decode('utf-8')
write_mode = 'w'
write_msg = MSG.decode('utf-8')
newline = ''


class NetworkConnectionTest(object):
"""Prove network connection."""

Expand Down Expand Up @@ -6649,11 +6664,11 @@ def test_main():
NonBlockingTCPTests,
FileObjectClassTestCase,
UnbufferedFileObjectClassTestCase,
LineBufferedFileObjectClassTestCase,
SmallBufferedFileObjectClassTestCase,
UnicodeReadFileObjectClassTestCase,
UnicodeWriteFileObjectClassTestCase,
UnicodeReadWriteFileObjectClassTestCase,
UnicodeLineBufferedFileObjectClassTestCase,
NetworkConnectionNoServer,
NetworkConnectionAttributesTest,
NetworkConnectionBehaviourTest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow :meth:`socket.socket.makefile` to handle line buffering properly
(``buffering=1``). This is done by mimicking the behavior of :func:`open`.