Skip to content

Commit

Permalink
inputhook: further cleanups for stdin_ready()
Browse files Browse the repository at this point in the history
 - split stdin_ready() in 3 platform specific functions and choose
   the appropriate one at import time
 - add docstrings for each of these
  • Loading branch information
cboos committed Oct 9, 2011
1 parent fa66348 commit bbdb0dd
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions IPython/lib/inputhook.py
Expand Up @@ -37,18 +37,27 @@
# Utilities
#-----------------------------------------------------------------------------

def stdin_ready():
if os.name == 'posix':
import select
infds, outfds, erfds = select.select([sys.stdin],[],[],0)
if infds:
return True
else:
return False
elif os.name == 'nt':
import msvcrt
return msvcrt.kbhit()
return True # assume there's something so that we won't wait forever
def _stdin_ready_posix():
"""Return True if there's something to read on stdin (posix version)."""
infds, outfds, erfds = select.select([sys.stdin],[],[],0)
return bool(infds)

def _stdin_ready_nt():
"""Return True if there's something to read on stdin (nt version)."""
return msvcrt.kbhit()

def _stdin_ready_other():
"""Return True, assuming there's something to read on stdin."""
return True #

if os.name == 'posix':
import select
stdin_ready = _stdin_ready_posix
elif os.name == 'nt':
import msvcrt
stdin_ready = _stdin_ready_nt
else:
stdin_ready = _stdin_ready_other


#-----------------------------------------------------------------------------
Expand Down

0 comments on commit bbdb0dd

Please sign in to comment.