From 8ed4afec85f56de5080603c0e6e8a3e19f063d28 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 11 Oct 2012 18:42:53 -0400 Subject: [PATCH] Update for Python 3 compatibility. --- curses_check_for_keypress.py | 29 ++++++++++++++++------------- setup.py | 6 +++++- 2 files changed, 21 insertions(+), 14 deletions(-) mode change 100644 => 100755 curses_check_for_keypress.py diff --git a/curses_check_for_keypress.py b/curses_check_for_keypress.py old mode 100644 new mode 100755 index af84489..3d762c7 --- a/curses_check_for_keypress.py +++ b/curses_check_for_keypress.py @@ -56,14 +56,14 @@ testing error catching, wait for the error... >>> try: ... while c.input() == None: ... if i > 4: -... print >> _sys.stderr, 'testing error output' -... raise TestException, 'testing error exception' -... c.output('sleeping %d\\n' % i) +... size = _sys.stderr.write('testing error output\\n') +... raise TestException('testing error exception') +... c.output('sleeping {}\\n'.format(i)) ... _test_sleep() ... i += 1 -... raise Exception, '_test_error_catching() failed!' -... except TestException, e: -... print 'caught exception:', e +... raise Exception('_test_error_catching() failed!') +... except TestException as e: +... print('caught exception: {}'.format(e)) ... finally: ... c.cleanup() sleeping 0 @@ -78,7 +78,10 @@ import curses as _curses # http://www.amk.ca/python/howto/curses/curses.html import curses.ascii as _curses_ascii from time import sleep as _sleep import sys as _sys -import StringIO as _StringIO +try: # Python 3 + from io import StringIO as _StringIO +except ImportError: # Python 2 + from StringIO import StringIO as _StringIO __version__ = '0.2' @@ -98,12 +101,12 @@ class CheckForKeypress (object): self.last = None # last byte number read self.lasta = None # last as an ASCII character if test_mode == True: - print prompt + print(prompt) self.i = 0 return None # redirect stderr to a file, because exiting curses mode clears # any error messages that had been printed to the screen - _sys.stderr = _StringIO.StringIO() + _sys.stderr = _StringIO() # initialize raw curses mode self._active = True self.stdscr = _curses.initscr() @@ -136,7 +139,7 @@ class CheckForKeypress (object): contents = _sys.stderr.getvalue() _sys.stderr = _sys.__stderr__ if len(contents) > 0: - print >> _sys.stderr, contents + _sys.stderr.write(contents) self._active = False def input(self): @@ -163,7 +166,7 @@ class CheckForKeypress (object): def _output(self, string): if self.test_mode == True: - print string, + _sys.stdout.write(string) return None #y,x = self.stdscr.getyx() self.stdscr.addstr(string) @@ -184,10 +187,10 @@ if __name__ == '__main__': i_max = 20 try: while c.input() == None: - c.output('%d/%d (sleeping)\n' % (i, i_max)) + c.output('{}/{} (sleeping)\n'.format(i, i_max)) _test_sleep() if i >= i_max: - raise Exception, "you didn't press a key!" + raise Exception("you didn't press a key!") i += 1 finally: c.cleanup() diff --git a/setup.py b/setup.py index 608418f..6129e72 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,6 @@ setup(name=package_name, url = 'http://blog.tremily.us/post/{}/'.format(package_name), download_url = 'http://git.tremily.us/?p={}.git/a=snapshot;h=v{};sf=tgz'.format( package_name, __version__), - license = 'GNU GPL v3+', platforms = ['all'], description = __doc__, @@ -29,6 +28,11 @@ setup(name=package_name, 'Operating System :: OS Independent', 'License :: OSI Approved :: GNU General Public License (GPL)', 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', 'Topic :: Software Development :: Libraries :: Python Modules', ], py_modules = ['curses_check_for_keypress'], -- 2.26.2