Skip to content

Commit

Permalink
Remove sys.stdin redirection (closes #1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Oct 1, 2024
1 parent f016b23 commit 5d38dba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion product/runtime/docs/sphinx/android.rst
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ by non-Python libraries. If you want to redirect them as well, see
`AndroidPlatform.redirectStdioToLogcat
<java/com/chaquo/python/android/AndroidPlatform.html#redirectStdioToLogcat()>`_.

By default, :any:`sys.stdin` always returns EOF. If you want to run some code which takes
:any:`sys.stdin` always returns EOF. If you want to run some code which takes
interactive text input, have a look at the `console app template
<https://github.com/chaquo/chaquopy-console>`_.

Expand Down
13 changes: 0 additions & 13 deletions product/runtime/src/main/python/java/android/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,11 @@


def initialize():
sys.stdin = EmptyInputStream()

# Log levels are consistent with those used by Java.
sys.stdout = TextLogStream(Log.INFO, "python.stdout")
sys.stderr = TextLogStream(Log.WARN, "python.stderr")


class EmptyInputStream(io.TextIOBase):
def readable(self):
return True

def read(self, size=None):
return ""

def readline(self, size=None):
return ""


class TextLogStream(io.TextIOWrapper):
def __init__(self, level, tag):
super().__init__(BinaryLogStream(self, level, tag),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def test_str(self):
for stream_name, level in [("stdout", "I"), ("stderr", "W")]:
with self.subTest(stream=stream_name):
stream = getattr(sys, stream_name)
original_stream = getattr(sys, f"__{stream_name}__")
self.assertIsNot(stream, original_stream)

# We use assertIn rather than assertEqual, because the demo app wraps the
# TextLogStream with a ConsoleOutputStream.
tag = f"python.{stream_name}"
Expand Down Expand Up @@ -303,9 +306,19 @@ def write(b, lines=None):
class TestAndroidInput(FilterWarningsCase):

def test_str(self):
self.assertIs(sys.stdin, sys.__stdin__)
self.assertTrue(sys.stdin.readable())
self.assertFalse(sys.stdin.writable())
self.assertEqual("", sys.stdin.read())
self.assertEqual("", sys.stdin.read(42))
self.assertEqual("", sys.stdin.readline())
self.assertEqual("", sys.stdin.readline(42))

def test_bytes(self):
buffer = sys.stdin.buffer
self.assertTrue(buffer.readable())
self.assertFalse(buffer.writable())
self.assertEqual(b"", buffer.read())
self.assertEqual(b"", buffer.read(42))
self.assertEqual(b"", buffer.readline())
self.assertEqual(b"", buffer.readline(42))

0 comments on commit 5d38dba

Please sign in to comment.