Author: Brian Kearns <[email protected]>
Branch: use-file-star-for-file
Changeset: r73440:6a7cc1c84664
Date: 2014-09-10 22:50 -0400
http://bitbucket.org/pypy/pypy/changeset/6a7cc1c84664/
Log: use f._setbufsize in interpreter init
diff --git a/pypy/interpreter/app_main.py b/pypy/interpreter/app_main.py
--- a/pypy/interpreter/app_main.py
+++ b/pypy/interpreter/app_main.py
@@ -222,20 +222,24 @@
print >> sys.stderr, 'usage: %s [options]' % (sys.executable,)
print >> sys.stderr, 'Try `%s -h` for more information.' %
(sys.executable,)
-def fdopen(fd, mode, bufsize=-1):
+def set_unbuffered_io():
try:
- fdopen = file.fdopen
- except AttributeError: # only on top of CPython, running tests
+ for f in [sys.__stdin__, sys.__stdout__, sys.__stderr__]:
+ f._setbufsize(0)
+ except AttributeError:
from os import fdopen
- return fdopen(fd, mode, bufsize)
+ sys.stdin = sys.__stdin__ = fdopen(0, 'rb', 0)
+ sys.stdout = sys.__stdout__ = fdopen(1, 'wb', 0)
+ sys.stderr = sys.__stderr__ = fdopen(2, 'wb', 0)
-def set_unbuffered_io():
- sys.stdin = sys.__stdin__ = fdopen(0, 'rb', 0)
- sys.stdout = sys.__stdout__ = fdopen(1, 'wb', 0)
- sys.stderr = sys.__stderr__ = fdopen(2, 'wb', 0)
-
-def set_fully_buffered_io():
- sys.stdout = sys.__stdout__ = fdopen(1, 'w')
+def set_line_buffered_io():
+ try:
+ for f in [sys.__stdin__, sys.__stdout__]:
+ f._setbufsize(1)
+ except AttributeError:
+ from os import fdopen
+ sys.stdin = sys.__stdin__ = fdopen(0, 'rb', 1)
+ sys.stdout = sys.__stdout__ = fdopen(1, 'wb', 1)
# ____________________________________________________________
# Main entry point
@@ -511,8 +515,8 @@
if unbuffered:
set_unbuffered_io()
- elif not sys.stdout.isatty():
- set_fully_buffered_io()
+ elif interactive:
+ set_line_buffered_io()
mainmodule = type(sys)('__main__')
sys.modules['__main__'] = mainmodule
diff --git a/pypy/interpreter/test/test_app_main.py
b/pypy/interpreter/test/test_app_main.py
--- a/pypy/interpreter/test/test_app_main.py
+++ b/pypy/interpreter/test/test_app_main.py
@@ -755,7 +755,7 @@
time.sleep(1)
# stdout flushed automatically here
""")
- cmdline = '%s -u "%s" %s' % (sys.executable, app_main, path)
+ cmdline = '%s "%s" %s' % (sys.executable, app_main, path)
print 'POPEN:', cmdline
child_in, child_out_err = os.popen4(cmdline)
data = child_out_err.read(11)
@@ -765,6 +765,32 @@
assert data == '\x00(STDOUT)\n\x00' # from stdout
child_out_err.close()
+ def test_non_interactive_stdout_line_buffered(self):
+ if os.name == 'nt':
+ try:
+ import __pypy__
+ except:
+ py.test.skip('app_main cannot run on non-pypy for windows')
+ path = getscript(r"""
+ import sys, time
+ sys.stdout.write('\x00(STDOUT)\n\x00')
+ time.sleep(1)
+ sys.stderr.write('\x00[STDERR]\n\x00')
+ time.sleep(1)
+ # stdout flushed automatically here
+ """)
+ cmdline = '%s "%s" -i %s' % (sys.executable, app_main, path)
+ print 'POPEN:', cmdline
+ child_in, child_out_err = os.popen4(cmdline)
+ data = child_out_err.read(10)
+ assert data == '\x00(STDOUT)\n' # from stdout
+ data = child_out_err.read(11)
+ assert data == '\x00[STDERR]\n\x00' # from stderr
+ child_in.close()
+ data = child_out_err.read(2)
+ assert data == '\n\x00'
+ child_out_err.close()
+
def test_non_interactive_stdout_unbuffered(self, monkeypatch):
monkeypatch.setenv('PYTHONUNBUFFERED', '1')
if os.name == 'nt':
@@ -787,8 +813,8 @@
assert data == '\x00(STDOUT)\n\x00' # from stderr
data = child_out_err.read(11)
assert data == '\x00[STDERR]\n\x00' # from stdout
+ child_in.close()
child_out_err.close()
- child_in.close()
def test_proper_sys_path(self, tmpdir):
data = self.run('-c "import _ctypes"', python_flags='-S')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit