summaryrefslogtreecommitdiffstats
path: root/Lib/io.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-12-06 01:04:26 (GMT)
committerGuido van Rossum <guido@python.org>2007-12-06 01:04:26 (GMT)
commitf64db9f6db6dc631368faaeaa5547769a01682cc (patch)
treefe6ff3ed1fb0ed0d3edfe193f303a31a3d93a003 /Lib/io.py
parent786720876c4a473284a0324c8e3b43ed87e8f42c (diff)
downloadcpython-f64db9f6db6dc631368faaeaa5547769a01682cc.zip
cpython-f64db9f6db6dc631368faaeaa5547769a01682cc.tar.gz
cpython-f64db9f6db6dc631368faaeaa5547769a01682cc.tar.bz2
Fix the rest of issue 1400, by introducing a proper implementation of
line buffering. The TextIOWrapper class no longer calls isatty() on every write() call.
Diffstat (limited to 'Lib/io.py')
-rw-r--r--Lib/io.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/Lib/io.py b/Lib/io.py
index a72e3fd..5066c49 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -151,8 +151,10 @@ def open(file, mode="r", buffering=None, encoding=None, errors=None,
closefd)
if buffering is None:
buffering = -1
- if buffering < 0 and raw.isatty():
- buffering = 1
+ line_buffering = False
+ if buffering == 1 or buffering < 0 and raw.isatty():
+ buffering = -1
+ line_buffering = True
if buffering < 0:
buffering = DEFAULT_BUFFER_SIZE
try:
@@ -182,7 +184,7 @@ def open(file, mode="r", buffering=None, encoding=None, errors=None,
buffer.name = file
buffer.mode = mode
return buffer
- text = TextIOWrapper(buffer, encoding, errors, newline)
+ text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
text.name = file
text.mode = mode
return text
@@ -1133,7 +1135,8 @@ class TextIOWrapper(TextIOBase):
_CHUNK_SIZE = 128
- def __init__(self, buffer, encoding=None, errors=None, newline=None):
+ def __init__(self, buffer, encoding=None, errors=None, newline=None,
+ line_buffering=False):
if newline not in (None, "", "\n", "\r", "\r\n"):
raise ValueError("illegal newline value: %r" % (newline,))
if encoding is None:
@@ -1160,6 +1163,7 @@ class TextIOWrapper(TextIOBase):
raise ValueError("invalid errors: %r" % errors)
self.buffer = buffer
+ self._line_buffering = line_buffering
self._encoding = encoding
self._errors = errors
self._readuniversal = not newline
@@ -1180,6 +1184,10 @@ class TextIOWrapper(TextIOBase):
def errors(self):
return self._errors
+ @property
+ def line_buffering(self):
+ return self._line_buffering
+
# A word about _snapshot. This attribute is either None, or a
# tuple (decoder_state, readahead, pending) where decoder_state is
# the second (integer) item of the decoder state, readahead is the
@@ -1218,13 +1226,13 @@ class TextIOWrapper(TextIOBase):
raise TypeError("can't write %s to text stream" %
s.__class__.__name__)
length = len(s)
- haslf = "\n" in s
+ haslf = (self._writetranslate or self._line_buffering) and "\n" in s
if haslf and self._writetranslate and self._writenl != "\n":
s = s.replace("\n", self._writenl)
# XXX What if we were just reading?
b = s.encode(self._encoding, self._errors)
self.buffer.write(b)
- if haslf and self.isatty():
+ if self._line_buffering and (haslf or "\r" in s):
self.flush()
self._snapshot = None
if self._decoder: