Backspace does not erase in stdout

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I have a function which reads characters from stdin and writes stars to
stdout, but backspacing does not erase the stars as I expected.

Tested in Python 2.6 on Linux. This will almost certainly not work on
Windows.

import sys, tty, termios
def getpass():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
chars = []
try:
tty.setraw(sys.stdin.fileno())
while 1:
c = sys.stdin.read(1)
if c in '\n\r': # Enter or Return key.
break
elif c == '\x7f': # Backspace key.
if chars:
# Rubout previous character.
sys.stdout.write('\b')
sys.stdout.flush()
del chars[-1]
else:
# Obfuscate the letter typed.
sys.stdout.write('*')
chars.append(c)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
sys.stdout.write('\n')
return ''.join(chars)


When I call this function and then type, I get a serious of asterisks as
expected. Each time I press the backspace key, the cursor moves one
character to the left, but the asterisks remain visible.

Is there a way to erase the character other than backspacing, writing a
space, then backspacing again? That feels inelegant.
 
C

Chris Angelico

Is there a way to erase the character other than backspacing, writing a
space, then backspacing again? That feels inelegant.

Emitting "\b \b" is one very common way to do a destructive backspace.
Inelegant? Perhaps, but a common inelegance.

ChrisA
 
G

Grant Edwards

Emitting "\b \b" is one very common way to do a destructive backspace.
Inelegant? Perhaps, but a common inelegance.

That's pretty much the only way I've seen it done for the past 25
years.

What would be more elegant?
 
S

Steven D'Aprano

That's pretty much the only way I've seen it done for the past 25 years.

What would be more elegant?

For backspace to actually backspace, and not just move the cursor.

Thanks for those who answered, I guess I'll just do the backspace, space,
backspace dance.
 
G

Grant Edwards

For backspace to actually backspace, and not just move the cursor.

Ah, but "just move the cursor" is what backspace has always meant.
It's been that way for 100 years -- since the days of typewriters and
teletypes. ;)
Thanks for those who answered, I guess I'll just do the backspace,
space, backspace dance.

After thinking a while, I do remember one program I ran across
recently that when you hit backspace would erase the entire line, then
rewrite the entire line stopping one character short of where it was
before. Even at network speeds it was noticable when the link was
encrypted.
 
C

Chris Angelico

After thinking a while, I do remember one program I ran across
recently that when you hit backspace would erase the entire line, then
rewrite the entire line stopping one character short of where it was
before.  Even at network speeds it was noticable when the link was
encrypted.

I've seen a few programs that do that. Benefit is that it works even
when (a) you're doing more than just "backspace one letter" (eg
deleting an entire word), and (b) when your logical line wraps over
multiple physical lines. It gets ugly fast, though.

ChrisA
 
N

Nobody

That's pretty much the only way I've seen it done for the past 25
years.

.... before which, it was BS-DEL-BS.

DEL being 127 means that it punches through all of the holes on the paper
tape, causing whatever was there previously to be replaced with a DEL
character. BS-SPC-BS would only punch through one of the holes.
 
R

Rick Johnson

... before which, it was BS-DEL-BS.

DEL being 127 means that it punches through all of the holes on the paper
tape, causing whatever was there previously to be replaced with a DEL
character. BS-SPC-BS would only punch through one of the holes.

*Wise Observer Speculates:* Why on earth are we "21st century slaves"
to an archaic mid 20th century technology that punches holes in paper
tape? Anyone?
True
 
C

Chris Angelico

*Wise Observer Speculates:* Why on earth are we "21st century slaves"
to an archaic mid 20th century technology that punches holes in paper
tape? Anyone?

True

I'm not sure. Let's see...

* Manned, powered, heavier-than-air flight: A hundred years.
* TCP/IP: Thirty years.
* Sliced bread: Eighty years. (What was the greatest thing BEFORE sliced bread?)
* The English language: A few hundred, depending on how you count

Why are we still eating sliced bread on board aeroplanes while posting
in English on internet forums? Clearly we've made no progress since
then. What language and communication protocol do you propose for the
new python-troll-list?

Getting back to the point though...

The use of 127 for DEL is a consequence of the exact technology of
paper tape. However, the question of whether BS is destructive or not
is separate. Should it parallel other characters (draw character at
cursor then advance cursor, vs retard cursor then erase character at
cursor), or should it parallel cursor movement (eg carriage return,
line feed, vertical tab)? If your BS is defined nondestructively, you
can implement the destructive version with three characters; if it's
defined destructively, you can't implement a nondestructive without
some fiddliness (some systems use characters 28-31 for cursor
movement).

ChrisA
 
R

Roy Smith

Nobody said:
... before which, it was BS-DEL-BS.

DEL being 127 means that it punches through all of the holes on the paper
tape, causing whatever was there previously to be replaced with a DEL
character. BS-SPC-BS would only punch through one of the holes.

I don't remember having a DEL key on the ASR-33. It did have a RUBOUT
key (http://regmedia.co.uk/2010/06/21/teletype_hereis.jpg) which
performed the function you describe. I don't remember if that actually
sent a 177 code over the wire or if it was a purely local function.

Also, the BS code only caused the printhead to back up. If you wanted
to back up the tape, you had to press the "B. SP." button the the punch
(http://pichotjm.free.fr/Multi8/TTY/ASR33-3.html).

Of course, sending a BS-SPS-BS sequence on a printing terminal didn't do
anything about erasing what had previously been printed. To do that,
you needed one of those fancy vee-de-oh things. On the ASR-33, to
prevent passwords from being readable, you either disabled echo, or sent
something like BS-*-BS-X-BS-$-BS-%-BS-#-BS-@-BS-&-BS-H-BS-Z-BS-M in the
hopes that nothing would be readable under all that garbage.
 
R

Roy Smith

Rick Johnson said:
*Wise Observer Speculates:* Why on earth are we "21st century slaves"
to an archaic mid 20th century technology that punches holes in paper
tape? Anyone?

Or to an archaic mid 20th century technology that limited lines to 80
characters?
 
G

Grant Edwards

... before which, it was BS-DEL-BS.

DEL being 127 means that it punches through all of the holes on the
paper tape, causing whatever was there previously to be replaced with
a DEL character.

BS-SPC-BS would only punch through one of the holes.

Ah yes. And when you read a tape, you silently ignored DEL
characters. I had completely fogotten about that. I only used paper
tape very briefly, and only for storage, never as a "live" medium for
editing.
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top