Gripe: Use Ctrl-D (i.e. EOF) to exit

M

Mike Maxwell

<vent> I see there has been traffic over the years on this gripe, so just
let me vent my frustration, and add another reason why 'exit' (or possibly
exit() or quit() or halt() or some such) should exit Python, rather than
giving the ridiculous msg

Use Ctrl-D (i.e. EOF) to exit.

The additional reason is that God intended Ctrl-D to be "scroll down", as in
vi. I have my keyboard translator set up to do something very much like
that (actually, I translate ^D into a sequence of "cursor downs"), and
hitting the Ctrl-D key therefore does not drop me out of Python. More
generally, it is probably not uncommon for someone to translate ^D into
something else, using xmodmap or some such. I need to kill the entire
X-terminal to stop Python.

Now, if you're using a gui Python, there's probably a menu command you can
use to drop out. But if you're using the command-line version (e.g. the one
in CygWin) from a shell prompt, then you're out of luck.

Why isn't there a command or function to drop out of Python??!! Obviously
Python is clever enough to know that you want to exit when you type 'exit';
why does it make you stand on your head to do so?

Aaargh!

</vent>

Mike Maxwell
 
P

Peter Hansen

Mike said:
<vent> I see there has been traffic over the years on this gripe, so just
let me vent my frustration, and add another reason why 'exit' (or possibly
exit() or quit() or halt() or some such) should exit Python, rather than
giving the ridiculous msg

Use Ctrl-D (i.e. EOF) to exit.

The additional reason is that God intended Ctrl-D to be "scroll down", as in
vi.

Interesting... so God didn't write the ASCII standard, obviously:

ctrl-D 4 EOT END OF TRANSMISSION
A transmission control character used to indicate the conclusion of the transmission of one or more texts..

(From http://www.cs.tut.fi/~jkorpela/chars/c0.html, for example.)


Why anyone would think that ought to be translated as "down" rather than
"I'm done, bye" is unclear to me.

-Peter
 
I

Irmen de Jong

Mike said:
that (actually, I translate ^D into a sequence of "cursor downs"), and
hitting the Ctrl-D key therefore does not drop me out of Python. More
generally, it is probably not uncommon for someone to translate ^D into
something else, using xmodmap or some such. I need to kill the entire
X-terminal to stop Python.

How the hell do you send a EOF then, to the many other applications that
read from stdin until EOF? (such as most standard unix tools)

(Also see Peter's reply.)

--Irmen
 
A

Alan Gauld

The additional reason is that God intended Ctrl-D to be "scroll down", as in
vi.

Sorry God had decided that Ctrl-D was EOF long before vi was even
a bit pattern in Bill Joy's eye.

And of course if you are on the spawn of Satan, EOF is CTRL-Z and
thats how you exit Python...
use to drop out. But if you're using the command-line version (e.g. the one
in CygWin) from a shell prompt, then you're out of luck.

raise SystemExit

A wee bit longwinded I'll grant you, but it does the trick...

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Mark Jackson]
from sys import exit
exit()

Or maybe, a bit simpler:

raise SystemExit

One may also manage to merely fall to the end of your __main__ module.
 
E

Erno Kuusela

Mike Maxwell said:
<vent> I see there has been traffic over the years on this gripe, so just
let me vent my frustration, and add another reason why 'exit' (or possibly
exit() or quit() or halt() or some such) should exit Python, rather than
giving the ridiculous msg

Use Ctrl-D (i.e. EOF) to exit.

of course typing EOF is the normal way to tell any unix stdio app to
exit. but the EOF key depends on your tty settings and is set with
stty. hardcoding ctrl-d is strictly speaking broken ;)

-- erno
 
M

Mike Maxwell

Peter said:
ctrl-D 4 EOT END OF TRANSMISSION
A transmission control character used to indicate the conclusion of
the transmission of one or more texts..

Why anyone would think that ought to be translated as "down" rather
than "I'm done, bye" is unclear to me.

To this and other comments along this line, I'll just add my own (not trying
to start a flame war, just explaining what may seem an odd choice on my
part):

IMHO, there's an unfortunate juxtaposition (I won't say confusion) of two
meanings of Ctrl-D: one is the ASCII char (as above), the other is whatever
is produced by pressing the 'd' key while the key labeled 'Ctrl' is pressed.

On Lisp Machines (and if you're too young to know what those were, you're
too young), there was a whole set of special purpose keys, including a Meta
key and a Hyper key. Their effect was similar to the Ctrl or Shift keys, in
that they modified the output of an "ordinary" key. Most keyboards nowadays
come with an Alt key, which could be used for what I want (move the cursor
down 7 rows, or scroll half a screen, or whatever), except that it has a
different function. And since there is no Meta or Hyper key on my keyboard,
I use the key to the left of the 'A' key (easy to reach both that and all
the alphabetic keys at the same time) as a key modifier, to do cursor
movement (^A = beginning of line, ^H = left one char, etc.). It happens
that that modifier key is labeled "Caps Lock" on most modern keyboards.
Under Linux, I can re-map it to a special modifier key, which does not then
interfere with using the Ctrl key to produce the Ascii control chars, so I
can use ^D to kill Python. But it's not so easy to re-map it under Windows,
and the only alternative seems to be to re-map it to be the Ctrl key. (And
don't say that I should just use Linux--the range of standardized cursor
movement keystrokes is _far_ greater under Windows than it is under Linux
environments that I've seen.)

So the upshot of it is, I want to have a keystroke to move down a half dozen
lines _much_ more often than I want one to exit a program. And my fingers
know Ctrl-d.

I might also point out that one can use other modifiers at the same time as
the Ctrl, e.g. Ctrl-Shift-D on my keyboard selects text for seven lines
down. There is obviously no Ascii equivalent of this, but it's a useful
keystroke.
Interesting... so God didn't write the ASCII standard, obviously

No, He used Hebrew. Duh. (I might also point out that the Hebrew alphabet,
lacking a case distinction, would fit nicely into a 6-bit encoding, as was
used on the CDC Cyber 170 that I wrote my dissertation on. You don't know
what a Cyber 170 is? You _are_ too young!)

Mike McSwell
 
J

John Hazen

* Erno Kuusela said:
of course typing EOF is the normal way to tell any unix stdio app to
exit. but the EOF key depends on your tty settings and is set with
stty. hardcoding ctrl-d is strictly speaking broken ;)

I'm not sure if you were implying that python does this...

I just tested it, and it's not hardcoded (2.3a2 darwin).

lap:john$ stty eof ^A
lap:john$ cat
nthoeu
nthoeu
< exited by typing ^A >
lap:john$ python
Python 2.3a2+ (#4, Mar 2 2003, 17:13:46)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information. < exited by typing ^A >
lap:john$

So, another solution to the OP's problem is to change the EOF character
to something else.

-John
< my_first_name AT my_last_name DOT net >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top