Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65091
| From | Kushal Kumaran <kushal.kumaran@gmail.com> |
|---|---|
| Subject | Re: buggy python interpretter or am I missing something here? |
| References | (7 earlier) <mailman.6048.1390838027.18130.python-list@python.org> <0c9b382d-e6e5-490f-93e4-2837b3187cf7@googlegroups.com> <52e6dce7$0$29999$c3e8da3$5496439d@news.astraweb.com> <bku5ckFdbb1U1@mid.individual.net> <52eb2162$0$29972$c3e8da3$5496439d@news.astraweb.com> |
| Date | 2014-01-31 10:37 +0530 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6192.1391144884.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes: > On Thu, 30 Jan 2014 18:13:54 +1300, Gregory Ewing wrote: > >> Steven D'Aprano wrote: >>> On Mon, 27 Jan 2014 12:22:22 -0800, Rick Johnson wrote: >>> >>>>Why do we even need an "input" function anyway if all it is going to do >>>>is read from stdin? >>> >>> That's not all it does. >>> >>> For example, it handles backspacing, so that typing H E L O O BACKSPACE >>> BACKSPACE L O gives "HELLO" rather than "HELOO\x7f\x7fO". >> >> No, it doesn't -- that's handled at a lower level. Any other method of >> reading from stdin, as long as it hasn't been redirected away from the >> console, has the same behaviour. >> >> I typed some backspaces in the input to each of the following >> experiments, and they didn't end up in the data: >> >> >>> import sys >> >>> x = sys.stdin.readline() >> HELLO >> >>> x >> 'HELLO\n' >> >>> import os >> >>> f = os.fdopen(0) >> >>> y = f.readline() >> adsxx >> >>> y >> 'adsxx\n' > > > Very interesting. I admit I don't actually understand the way stdin > works. Can you explain what's going on here then? > > import sys, os > import tty, termios, fcntl > > def getch(): > """Get a single character from standard input. > > Does not echo to the screen. This will block waiting for a keypress. > """ > fd = sys.stdin.fileno() > old_settings = termios.tcgetattr(fd) > try: > tty.setraw(fd) > ch = sys.stdin.read(1) > finally: > termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) > return ch > > > And in use: > >>>> [getch() for i in range(14)] > ['H', 'e', 'l', 'l', 'l', '\x7f', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'] > > > where I type "Helll BACKSPACE o SPACE World!" > > > At what point do the arrow keys and other readline or readline-like > features get handled? > They can be handled by the in-kernel tty driver, when you're using a tty set in "cooked" mode. This driver can be configured by the termios functions, which invoke various ioctls on the terminal devices. Or you can set the tty to "raw" mode, and the keystrokes are passed on to the application, which can do all sorts of interpretation (for example if you're using the readline library, or a curses app). W. Richard Stevens covers this in much detail in APUE, but you can preserve your sanity by being ignorant of the details of tty handling. -- regards, kushal
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 03:42 +0000
buggy python interpretter or am I missing something here? - "TCdaemon.py" yEnc me <noone@all.net> - 2014-01-27 03:42 +0000
Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 21:04 -0800
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:17 +0000
Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 23:03 -0800
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:20 +0000
Re:buggy python interpretter or am I missing something here? Dave Angel <davea@davea.name> - 2014-01-27 00:36 -0500
Re:buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:02 +0000
Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 00:47 -0600
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:17 +0000
Re: buggy python interpretter or am I missing something here? Alister <alister.ware@ntlworld.com> - 2014-01-27 12:19 +0000
Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 13:48 +0000
Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 10:23 -0600
Re: buggy python interpretter or am I missing something here? Dan Sommers <dan@tombstonezero.net> - 2014-01-27 16:38 +0000
Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 16:45 +0000
Re: buggy python interpretter or am I missing something here? Terry Reedy <tjreedy@udel.edu> - 2014-01-27 01:21 -0500
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:42 +0000
Re: buggy python interpretter or am I missing something here? Ethan Furman <ethan@stoneleaf.us> - 2014-01-26 23:08 -0800
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:46 +0000
Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 00:55 -0600
Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 23:12 -0800
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:30 +0000
Re: buggy python interpretter or am I missing something here? Peter Otten <__peter__@web.de> - 2014-01-27 09:45 +0100
Re: buggy python interpretter or am I missing something here? Ethan Furman <ethan@stoneleaf.us> - 2014-01-26 23:17 -0800
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:44 +0000
Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-27 20:01 +1100
Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 09:32 +0000
Re: buggy python interpretter or am I missing something here? Neil Cerutti <neilc@norwich.edu> - 2014-01-27 12:56 +0000
Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-27 13:56 +0000
Re: buggy python interpretter or am I missing something here? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-01-27 07:33 -0800
Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-28 02:53 +1100
Re: buggy python interpretter or am I missing something here? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-01-27 12:22 -0800
Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-28 07:29 +1100
Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-27 22:25 +0000
Re: buggy python interpretter or am I missing something here? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-30 18:13 +1300
Re: buggy python interpretter or am I missing something here? Terry Reedy <tjreedy@udel.edu> - 2014-01-30 04:44 -0500
Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-31 04:06 +0000
Re: buggy python interpretter or am I missing something here? Kushal Kumaran <kushal.kumaran@gmail.com> - 2014-01-31 10:37 +0530
Re: buggy python interpretter or am I missing something here? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-31 19:59 +1300
Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 16:22 +0000
Re: buggy python interpretter or am I missing something here? Michael Torrie <torriem@gmail.com> - 2014-01-27 10:52 -0700
csiph-web