[Issue 10467] readln problem with CTRL-Z

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 25 00:12:43 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10467



--- Comment #4 from monarchdodra at gmail.com 2013-06-25 00:12:41 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> 
> > Just for the record, what *would* the correct behavior be?
> 
> An equivalent Python2.6 program:
> 
> 
> while True:
>     guess = raw_input("Input: ").strip()
>     if guess == "1":
>         break
> 
> 
> 
> 
> This is how Python behaves here (inputs are 1, CTRL-C and CTRL-Z):
> 
> >temp.py
> Input: 1
> 
> >temp.py
> Input: Traceback (most recent call last):
>   File "\temp.py", line 2, in <module>
>     guess = raw_input("Input: ").strip()
> KeyboardInterrupt
> 
> >temp.py
> Input: ^Z
> Traceback (most recent call last):
>   File "\temp.py", line 2, in <module>
>     guess = raw_input("Input: ").strip()
> EOFError

I'm noticing D's readln doesn't quite have the same semantics as C++'s:
In C++, getline will keep reading until it *can* read at least a single non
empty line, trying again if it reads an empty line. This means that you can't
know if you'll succeed unless you try. getline explicitly returns the state of
the stream, so you can test the return status of getline.

D's getline, on the other hand, returns empty lines. This means there is no
real way to check for success, unless an exception was thrown, or the stream is
in an error state. This means the interface is kind of crummy, since it claims:
"returns: 0 for end of file, otherwise number of characters read": which sucks,
since it can succeed if 0 characters were read...

Also, D's readln says "StdioException on I/O error".

So after a second though, I think you are right, and Python's "throw for reads
of EOF" is not only safer and better, but also works, and is the *documented
behavior*... too bad we didn't have this for C++ :'(

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list