Skip to content

Commit

Permalink
fix: Clear O_NONBLOCK flag after using ainput()
Browse files Browse the repository at this point in the history
This is to ensure we can still use regular blocking IO such as
input() and print() after using ainput.

Fixes #17, #19
  • Loading branch information
plammens committed Jun 11, 2022
1 parent b202562 commit ea69345
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/loveletter_cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import more_itertools as mitt
import valid8
from aioconsole import ainput
from multimethod import multimethod

import loveletter.game
Expand All @@ -22,6 +21,7 @@
from loveletter_cli.exceptions import Restart
from loveletter_cli.server_process import ServerProcess
from loveletter_cli.ui import (
ainput,
async_ask_valid_input,
draw_game,
pause,
Expand Down
21 changes: 20 additions & 1 deletion src/loveletter_cli/ui/input.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import enum
import functools
import os
import sys
import textwrap
from typing import Callable, Tuple, Type, TypeVar

import aioconsole
import more_itertools
import valid8
from aioconsole import ainput

from .misc import printable_width

Expand All @@ -14,6 +16,21 @@
_DEFAULT = object()


# Define ainput() based on OS: if the OS uses the O_NONBLOCK flag, we have to
# clear it after every call to ainput() to ensure compatibility with blocking
# IO such as print() and input().
if hasattr(os, "set_blocking"):

@functools.wraps(aioconsole.ainput)
async def ainput(*args, **kwargs):
result = await aioconsole.ainput(*args, **kwargs)
os.set_blocking(sys.stdin.fileno(), True)
return result

else:
ainput = aioconsole.ainput


def ask_valid_input(*args, **kwargs) -> _T:
error_message, parser, prompt, validation_errors = _ask_valid_input_parse_args(
*args, **kwargs
Expand Down Expand Up @@ -162,4 +179,6 @@ async def pause() -> None:
# the user has to enter twice before input is detected;
# but the asynchronous nature is needed to ensure other events are handled in time
# (e.g. when the connection is lost).
# Also caused trouble on Linux and Unix regarding the O_NONBLOCK flag
# (see #17 and #19) but this has been fixed.
await ainput("Enter something to continue... ")

0 comments on commit ea69345

Please sign in to comment.