Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'finally:': 0.07; 'received:198': 0.07; 'redirected': 0.07; 'subject:missing': 0.07; 'sys': 0.07; 'data:': 0.09; 'driver,': 0.09; 'sanity': 0.09; 'screen.': 0.09; 'sys,': 0.09; 'try:': 0.09; 'typed': 0.09; 'works.': 0.09; 'def': 0.12; 'jan': 0.12; 'anyway': 0.14; '"""get': 0.16; "'d',": 0.16; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; "'w',": 0.16; 'backspace': 0.16; 'behaviour.': 0.16; 'fcntl': 0.16; 'mode,': 0.16; 'preserve': 0.16; 'sorts': 0.16; 'then?': 0.16; 'tty': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; "hasn't": 0.19; 'thu,': 0.19; 'typing': 0.19; '>>>': 0.22; 'input': 0.22; 'example': 0.22; 'import': 0.22; 'handles': 0.22; 'header:User-Agent:1': 0.23; 'driver': 0.24; 'library,': 0.24; 'mon,': 0.24; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'character': 0.29; "doesn't": 0.30; 'gives': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'invoke': 0.31; 'keys': 0.31; 'steven': 0.31; 'writes:': 0.31; 'handled': 0.32; 'level.': 0.33; 'johnson': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'method': 0.36; 'subject:?': 0.36; 'detail': 0.37; 'example,': 0.37; 'being': 0.38; 'configured': 0.38; 'richard': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; 'even': 0.60; 'read': 0.60; 'lower': 0.61; "you're": 0.61; 'details': 0.65; 'here': 0.66; 'covers': 0.68; 'message-id:@mx.google.com': 0.80; 'console,': 0.84; 'keystrokes': 0.84; 'subject:here': 0.84; 'world!"': 0.84; 'rick': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:in-reply-to:references:user-agent:date:message-id :mime-version:content-type; bh=5CfO+0NICl8yxjjVCNr/dA3jVjcT4mMUi4ybYSABDEc=; b=fEBqrFwToIeR/cgelqjnOshsid/zjUkUwSJuUi45JDxVJzp0i1TKEe30yhKmVCmWlp M2u/mcWFie3IxHbQf2gvUOzSIZ2uiSY8WuekfbWHjdW51G639ufCp1dAhCEOKmb2yG3o icMuejnjbjPM4hhwq5qA6Ev+ShKQhTAPvt1G2u5QmVbLiGnXiShSjLYzG0qfcj8V4H/l hLKKkYNJ4i5TfmCVKRHvpVXeLeA6DAZXeI2IkzoYmh5bXxXs8xgmQhk2bbjZSYHWnvv9 7Pi9NUteQMBCIfNeWEZOkpzyRlWeJvLCh7hnZ1SK/ZKS5sMYTWhxpDg3w0IEtg0v9iU+ cSbg== X-Received: by 10.42.144.201 with SMTP id c9mr13482879icv.16.1391144875605; Thu, 30 Jan 2014 21:07:55 -0800 (PST) From: Kushal Kumaran To: python-list@python.org Subject: Re: buggy python interpretter or am I missing something here? In-Reply-To: <52eb2162$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <52e600ab$0$29542$862e30e2@ngroups.net> <52e60e69$0$29774$862e30e2@ngroups.net> <52e627bb$0$29811$862e30e2@ngroups.net> <25077ddb-e33d-4c5e-93c8-91d5d079ee8b@googlegroups.com> <0c9b382d-e6e5-490f-93e4-2837b3187cf7@googlegroups.com> <52e6dce7$0$29999$c3e8da3$5496439d@news.astraweb.com> <52eb2162$0$29972$c3e8da3$5496439d@news.astraweb.com> User-Agent: Notmuch/0.17+35~g3b36898 (http://notmuchmail.org) Emacs/24.3.1 (x86_64-pc-linux-gnu) Date: Fri, 31 Jan 2014 10:37:41 +0530 MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391144884 news.xs4all.nl 2876 [2001:888:2000:d::a6]:47984 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65091 --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Steven D'Aprano 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: >>>=20 >>>>Why do we even need an "input" function anyway if all it is going to do >>>>is read from stdin? >>>=20 >>> That's not all it does. >>>=20 >>> For example, it handles backspacing, so that typing H E L O O BACKSPACE >>> BACKSPACE L O gives "HELLO" rather than "HELOO\x7f\x7fO". >>=20 >> 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. >>=20 >> I typed some backspaces in the input to each of the following >> experiments, and they didn't end up in the data: >>=20 >> >>> import sys >> >>> x =3D sys.stdin.readline() >> HELLO >> >>> x >> 'HELLO\n' >> >>> import os >> >>> f =3D os.fdopen(0) >> >>> y =3D f.readline() >> adsxx >> >>> y >> 'adsxx\n' > > > Very interesting. I admit I don't actually understand the way stdin=20 > 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 =3D sys.stdin.fileno() > old_settings =3D termios.tcgetattr(fd) > try: > tty.setraw(fd) > ch =3D 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=20 > 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. =2D-=20 regards, kushal --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.14 (GNU/Linux) iQEcBAEBAgAGBQJS6y+dAAoJEKolkdITDqLt6fQH/1xxAdiAJAWaq/zupTmGCSNu n/vnexajRihcK2OUaF/aWwD8hdTdg2zeh+z+4uN9KE3l7Er+RoN1YLD747NWPS1p R6n7ucrbR9dEGdR0OQY9+Ggqk8M46fqkC4A0Rj9YzY8SHafy176a2UUPGnoHhTH0 CCNEtuvE541WjjM4oHZOtNeFfqbphfNH4rpmjEA3eOWg/4IA7HZXCbT6yS5onRNW pi6p6apOctxmIdKLfqNP9rVQ01sp0y8iXqfHHujKUBvUAmTktjn0UNf6kf5gbjwF iQJahEtPGtEEBeVRZ4fAQN+7uagrCF+ryIfk9On7eASp0DtOgrTp4gsu9UXV5/E= =mt2y -----END PGP SIGNATURE----- --=-=-=--