[pypy-commit] pypy default: merged upstream
alex_gaynor
noreply at buildbot.pypy.org
Thu Oct 20 03:20:31 CEST 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r48252:63d990952c7a
Date: 2011-10-19 21:20 -0400
http://bitbucket.org/pypy/pypy/changeset/63d990952c7a/
Log: merged upstream
diff --git a/pypy/module/__builtin__/app_io.py b/pypy/module/__builtin__/app_io.py
--- a/pypy/module/__builtin__/app_io.py
+++ b/pypy/module/__builtin__/app_io.py
@@ -27,7 +27,20 @@
co = compile(source.rstrip()+"\n", filename, 'exec')
exec co in glob, loc
-def raw_input(prompt=None):
+def _write_prompt(stdout, prompt):
+ print >> stdout, prompt,
+ try:
+ flush = stdout.flush
+ except AttributeError:
+ pass
+ else:
+ flush()
+ try:
+ stdout.softspace = 0
+ except (AttributeError, TypeError):
+ pass
+
+def raw_input(prompt=''):
"""raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
@@ -47,18 +60,10 @@
if (hasattr(sys, '__raw_input__') and
isinstance(stdin, file) and stdin.fileno() == 0 and stdin.isatty() and
isinstance(stdout, file) and stdout.fileno() == 1):
- if prompt is None:
- prompt = ''
- return sys.__raw_input__(prompt)
+ _write_prompt(stdout, '')
+ return sys.__raw_input__(str(prompt))
- if prompt is not None:
- stdout.write(prompt)
- try:
- flush = stdout.flush
- except AttributeError:
- pass
- else:
- flush()
+ _write_prompt(stdout, prompt)
line = stdin.readline()
if not line: # inputting an empty line gives line == '\n'
raise EOFError
diff --git a/pypy/module/__builtin__/test/test_rawinput.py b/pypy/module/__builtin__/test/test_rawinput.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__builtin__/test/test_rawinput.py
@@ -0,0 +1,77 @@
+import autopath
+
+
+class AppTestRawInput():
+
+ def test_raw_input(self):
+ import sys, StringIO
+ for prompt, expected in [("def:", "abc/ def:/ghi\n"),
+ ("", "abc/ /ghi\n"),
+ (42, "abc/ 42/ghi\n"),
+ (None, "abc/ None/ghi\n"),
+ (Ellipsis, "abc/ /ghi\n")]:
+ save = sys.stdin, sys.stdout
+ try:
+ sys.stdin = StringIO.StringIO("foo\nbar\n")
+ out = sys.stdout = StringIO.StringIO()
+ print "abc", # softspace = 1
+ out.write('/')
+ if prompt is Ellipsis:
+ got = raw_input()
+ else:
+ got = raw_input(prompt)
+ out.write('/')
+ print "ghi"
+ finally:
+ sys.stdin, sys.stdout = save
+ assert out.getvalue() == expected
+ assert got == "foo"
+
+ def test_softspace(self):
+ import sys
+ import StringIO
+ fin = StringIO.StringIO()
+ fout = StringIO.StringIO()
+
+ fin.write("Coconuts\n")
+ fin.seek(0)
+
+ sys_stdin_orig = sys.stdin
+ sys_stdout_orig = sys.stdout
+
+ sys.stdin = fin
+ sys.stdout = fout
+
+ print "test",
+ raw_input("test")
+
+ sys.stdin = sys_stdin_orig
+ sys.stdout = sys_stdout_orig
+
+ fout.seek(0)
+ assert fout.read() == "test test"
+
+ def test_softspace_carryover(self):
+ import sys
+ import StringIO
+ fin = StringIO.StringIO()
+ fout = StringIO.StringIO()
+
+ fin.write("Coconuts\n")
+ fin.seek(0)
+
+ sys_stdin_orig = sys.stdin
+ sys_stdout_orig = sys.stdout
+
+ sys.stdin = fin
+ sys.stdout = fout
+
+ print "test",
+ raw_input("test")
+ print "test",
+
+ sys.stdin = sys_stdin_orig
+ sys.stdout = sys_stdout_orig
+
+ fout.seek(0)
+ assert fout.read() == "test testtest"
More information about the pypy-commit
mailing list