fix hidconsole for python 2/3

This commit is contained in:
Daniel Pavel 2013-05-06 17:38:09 +02:00
parent 6b75286885
commit 79cd52833c
1 changed files with 11 additions and 14 deletions

View File

@ -29,7 +29,7 @@ strhex = lambda d: hexlify(d).decode('ascii').upper()
try:
unicode
# this is certanly Python 2
is_string = lambda d: isinstance(d, unicode) or isinstance(d, str)
is_string = lambda d: isinstance(d, unicode)
# no easy way to distinguish between b'' and '' :(
# or (isinstance(d, str) \
# and not any((chr(k) in d for k in range(0x00, 0x1F))) \
@ -74,6 +74,11 @@ def _print(marker, data, scroll=False):
else:
sys.stdout.write('\n')
# flush stdout manually...
# because trying to open stdin/out unbuffered programatically
# works much too differently in Python 2/3
sys.stdout.flush()
def _error(text, scroll=False):
_print("!!", text, scroll)
@ -183,13 +188,6 @@ def main():
# file may not exist yet
pass
# re-open stdout unbuffered
try:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
except:
# will fail in python3
pass
try:
from threading import Thread
t = Thread(target=_continuous_read, args=(handle,))
@ -227,9 +225,8 @@ def main():
print ("")
else:
time.sleep(1)
except Exception as e:
print ('%s: %s' % (type(e).__name__, e))
finally:
print (".. Closing handle %r" % handle)
hidapi.close(handle)
if interactive: