Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r48244:2a1eb9241e77 Date: 2011-10-19 23:26 +0200 http://bitbucket.org/pypy/pypy/changeset/2a1eb9241e77/
Log: Test and fix for issue917. 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,7 @@ co = compile(source.rstrip()+"\n", filename, 'exec') exec co in glob, loc -def raw_input(prompt=None): +def raw_input(prompt=''): """raw_input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. @@ -51,14 +51,17 @@ prompt = '' return sys.__raw_input__(prompt) - if prompt is not None: - stdout.write(prompt) - try: - flush = stdout.flush - except AttributeError: - pass - else: - flush() + print >> stdout, prompt, + try: + flush = stdout.flush + except AttributeError: + pass + else: + flush() + try: + stdout.softspace = 0 + except (AttributeError, TypeError): + pass line = stdin.readline() if not line: # inputting an empty line gives line == '\n' raise EOFError diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py --- a/pypy/module/__builtin__/test/test_builtin.py +++ b/pypy/module/__builtin__/test/test_builtin.py @@ -631,6 +631,30 @@ raises(TypeError, pr, end=3) raises(TypeError, pr, sep=42) + 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_round(self): assert round(11.234) == 11.0 assert round(11.234, -1) == 10.0 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit