Python Forum
impossible to exit from the file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: impossible to exit from the file (/thread-12757.html)



impossible to exit from the file - sylas - Sep-11-2018

Hi all ! This file works well, unless I try to exit. Look please at lines 17,18, 19. I am on Linux. Python 3.7.
import tty, sys, termios

class ReadChar():
    def __enter__(self):
        self.fd = sys.stdin.fileno()
        self.old_settings = termios.tcgetattr(self.fd)
        tty.setraw(sys.stdin.fileno())
        return sys.stdin.read(1)
    def __exit__(self, type, value, traceback):
        termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old_settings)

def test():
    while True:
        with ReadChar() as rc:
            char = rc
        #if ord(char) == 3:
		#	sys.exit()   
        #elif ord(char) <= 32:
        if ord(char) <= 32:
            print("You entered character with ordinal {}."\
                        .format(ord(char)))
        else:
            print("You entered character '{}'."\
                        .format(char))
        #if char in "^C^D":
         #   sys.exit()

if __name__ == "__main__":
    test()
If I uncomment lines 17, 18, I have the following error:
Error:
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python get2.py File "get2.py", line 18 sys.exit() ^



RE: impossible to exit from the file - sylas - Sep-11-2018

Error message was incomplete:
Error:
File "get2.py", line 18 sys.exit() ^ TabError: inconsistent use of tabs and spaces in indentation



RE: impossible to exit from the file - volcano63 - Sep-12-2018

(Sep-11-2018, 09:14 AM)sylas Wrote:
Error:
File "get2.py", line 18 sys.exit() ^ TabError: inconsistent use of tabs and spaces in indentation

The message is clear - you mixed tabs and spaces in your code. You should never do that


RE: impossible to exit from the file - sylas - Sep-13-2018

@volcano It is not first time I notice interpreter tells nonsense. Please try yourself, and you will see that it is impossible to exit. Thanks for your reply.


RE: impossible to exit from the file - buran - Sep-13-2018

(Sep-13-2018, 06:38 AM)sylas Wrote: It is not first time I notice interpreter tells nonsense.
Please, let be serious... message is clear - you mix tabs and space for indentation. fix that and it will work if there is no other problems.


RE: impossible to exit from the file - volcano63 - Sep-13-2018

(Sep-13-2018, 06:38 AM)sylas Wrote: @volcano It is not first time I notice interpreter tells nonsense. Please try yourself, and you will see that it is impossible to exit. Thanks for your reply.

Unclear - maybe; nonsense - in my 6 years in Python I have never noticed Doh (from interpreter Naughty )


RE: impossible to exit from the file - DeaD_EyE - Sep-13-2018

It was introduced with Python 3.
If you are mixing tabs with white space, your program won't run.
They changed it, because before it was a bit confusing.

We don't need to try it out, because we know this. I run often into this issue until I changed my tool set.
Just use an IDE/text editor, which converts Tab-Stops into 4 white spaces.


RE: impossible to exit from the file - volcano63 - Sep-13-2018

(Sep-13-2018, 10:39 AM)DeaD_EyE Wrote: It was introduced with Python 3.
If you are mixing tabs with white space, your program won't run.

Good call on the part of Python development community - was not aware of that. (Well, I never use tabs)

I had my share of inherited code with a mixture of tabs and spaces - that was major PITA.


RE: impossible to exit from the file - sylas - Sep-13-2018

Instead of tabs I used spaces. At the second trial it works well. Surprising !!