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

Discussion in 'Python' started by Mike Maxwell, Feb 23, 2004.

  1. Mike Maxwell

    Mike Maxwell Guest

    <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
     
    Mike Maxwell, Feb 23, 2004
    #1
    1. Advertising

  2. Mike Maxwell

    Peter Hansen Guest

    Mike Maxwell wrote:
    >
    > <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
     
    Peter Hansen, Feb 23, 2004
    #2
    1. Advertising

  3. Mike Maxwell wrote:

    > 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
     
    Irmen de Jong, Feb 23, 2004
    #3
  4. Mike Maxwell

    Mark Jackson Guest

    "Mike Maxwell" <> writes:

    > Why isn't there a command or function to drop out of Python??!!


    from sys import exit
    exit()

    --
    Mark Jackson - http://www.alumni.caltech.edu/~mjackson
    The *world* is arbitrary. *Individuals* are either fair
    or unfair. Which are you? - Caulfield (Jef Mallett)
     
    Mark Jackson, Feb 23, 2004
    #4
  5. Mike Maxwell

    Alan Gauld Guest

    On Mon, 23 Feb 2004 15:12:59 -0500, "Mike Maxwell"
    <> wrote:

    > 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
     
    Alan Gauld, Feb 23, 2004
    #5
  6. [Mark Jackson]
    > "Mike Maxwell" <> writes:


    > > Why isn't there a command or function to drop out of Python??!!


    > 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.

    --
    Fran├žois Pinard http://www.iro.umontreal.ca/~pinard
     
    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Feb 24, 2004
    #6
  7. Mike Maxwell

    Erno Kuusela Guest

    "Mike Maxwell" <> writes:

    > <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
     
    Erno Kuusela, Feb 24, 2004
    #7
  8. Mike Maxwell

    Mike Maxwell Guest

    Peter Hansen wrote:
    > 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.

    Peter Hansen also wrote:
    > 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
     
    Mike Maxwell, Feb 24, 2004
    #8
  9. Mike Maxwell

    John Hazen Guest

    * Erno Kuusela <> [2004-02-23 17:38]:
    >
    > 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 >
     
    John Hazen, Feb 25, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C++
    Replies:
    5
    Views:
    653
    CBFalconer
    Aug 15, 2003
  2. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C Programming
    Replies:
    5
    Views:
    668
    CBFalconer
    Aug 15, 2003
  3. Replies:
    1
    Views:
    399
    Dan Bishop
    Feb 26, 2004
  4. Angus
    Replies:
    5
    Views:
    2,117
    Michael Rauscher
    Nov 18, 2006
  5. Replies:
    2
    Views:
    1,420
    Munna
    Jul 10, 2008
Loading...

Share This Page