[Tutor] puzzling EOFError when mocking raw_input in doctest

Kent Johnson kent37 at tds.net
Mon Aug 11 00:18:40 CEST 2008


On Sun, Aug 10, 2008 at 4:03 PM,  <broek at cc.umanitoba.ca> wrote:
>
> Hi all,
>
> I've continued playing about with mocking raw_input in doctests in light of
> Kent's reply.
>
> I've hit an odd thing---if my substitute for raw_input returns '', I get an
> EOFError. The code below shows the problem. It arises equally if myrawalt is
> used in the doctest in place of myraw, so I am sure the problem has nothing
> to do with the use of iter. I can work around the problem in my actual
> target code---that code, like the toy code below, calls strip() on the
> results of raw_input, so I can get away with having my raw_input substitute
> return ' ' in place of ''. But, I cannot figure out why the EOFError comes
> up. Any thoughts?

My guess, to verify look at the source for raw_input() or experiment...

readline() normally includes the \n in the line it returns. It will
only return an empty string on EOF. So the raw_input() is seeing the
empty line, interpreting it as EOF, and raising EOFError.

My suggestion - in your mock stdin, append a \n to the provided data.

Also you might want to change the name, you are replacing sys.stdin,
not raw_input().

Kent


More information about the Tutor mailing list