Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal in broken state (invisible text / no echo) after exit() during input() / raw_input()

Tags:

I've been writing a small utility application using Python 3 (the below testcase also works in Python 2, however) and PyQt 4 that uses the code module to spawn a REPL prompt allowing interaction with a Qt window.

Unfortunately I've hit a problem I've been unable to solve: When I exit() the app while code is inside input() (known as raw_input() in Python 2.x), my Linux terminal subsequently no longer echoes typed characters. I.e. the terminal appears to be left in a broken state, presumably due to some escape sequence issued by input().

I've tried a variety of approaches to fix this, from using the curses module and other means to reset the terminal prior to running exit, to trying to emulate the stdin stream to exit by actually handing exit() to input() (unfornunately code.InteractiveConsole.push() does not work that way, as one might think it would), to trying to write my own non-blocking input() using threading, but I've been unable to pull together something working.

Here, here, here and here are discussions of similar problems.

Finally, here is a reduced testcase to demonstrate the problem:

#!/usr/bin/env python3  import code import sys from PyQt4.QtGui import QApplication, QWidget  app = QApplication(sys.argv)  app.lastWindowClosed.connect(exit)  widget = QWidget() widget.show()  code.interact() 

For those unfamiliar with (Py)Qt, this will open a blank window, and when it is closed, the connection from app's lastWindowClosed signal will cause a call to the built-in exit() function to happen. This occurs while the code module is executing a call to input() to read from sys.stdin. And here, when I close the window, typing into the terminal afterwards doesn't show any of the types characters.

I'm mainly using Python 3, and the actual app uses Python 3-specific code, but I've tried the testcase in Python 2.7 as well and it shows the same problem.

like image 808
Sho Avatar asked Oct 29 '11 10:10

Sho


2 Answers

Try os.system('stty sane'). The stty sane is supposed to reset echo, and some other things apparently.

like image 57
Quentin Engles Avatar answered Sep 27 '22 23:09

Quentin Engles


This is no real solution to the problem, but if you type "reset" in the terminal after you've closed the app, it goes back to normal.

I had similar issues once when developing a c application that didn't close a pipe correctly. Maybe something similar is happening here aswell.

like image 28
Jakob Avatar answered Sep 27 '22 21:09

Jakob