Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@
pysr_logger = logging.getLogger(__name__)


def _stdin_is_compatible() -> bool:
"""Return whether Python's stdin is a usable interactive stream."""
stdin = sys.stdin
if stdin is None or stdin.closed:
return False
try:
if not stdin.isatty():
return False
os.fstat(stdin.fileno())
except (AttributeError, OSError, ValueError):
return False
return True


def _resolve_input_stream(input_stream: str) -> AnyValue:
if input_stream == "stdin" and not _stdin_is_compatible():
return jl.seval("devnull")
return jl.seval(input_stream)


def _process_constraints(
operators: dict[int, list[str]],
constraints: dict[str, int | tuple[int, ...]],
Expand Down Expand Up @@ -780,10 +800,11 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
`TensorBoardLoggerSpec`.
Default is `None`.
input_stream : str
The stream to read user input from. By default, this is `"stdin"`.
If you encounter issues with reading from `stdin`, like a hang,
you can simply pass `"devnull"` to this argument. You can also
reference an arbitrary Julia object in the `Main` namespace.
The stream to read user input from. By default, this is `"stdin"`,
which falls back to `"devnull"` when Python's stdin is not an
interactive terminal. You can pass `"devnull"` to explicitly disable
reading from stdin, or reference an arbitrary Julia object in the
`Main` namespace.
Default is `"stdin"`.
run_id : str
A unique identifier for the run. Will be generated using the
Expand Down Expand Up @@ -2152,7 +2173,7 @@ def _run(
else "nothing"
)

input_stream = jl.seval(self.input_stream)
input_stream = _resolve_input_stream(self.input_stream)

load_required_packages(
turbo=self.turbo,
Expand Down
40 changes: 40 additions & 0 deletions pysr/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from pysr.sr import (
_check_assertions,
_process_constraints,
_resolve_input_stream,
_stdin_is_compatible,
_suggest_keywords,
_validate_elementwise_loss,
_validate_export_mappings,
Expand All @@ -63,6 +65,44 @@
from juliacall import JuliaError # type: ignore


class TestInputStream(unittest.TestCase):
def test_stdin_resolves_to_devnull_when_not_interactive(self):
stdin = mock.Mock(closed=False)
stdin.isatty.return_value = False
seval = mock.Mock(return_value="DEVNULL")
with (
mock.patch("pysr.sr.sys.stdin", stdin),
mock.patch("pysr.sr.jl", mock.Mock(seval=seval)),
):
self.assertEqual(_resolve_input_stream("stdin"), "DEVNULL")
seval.assert_called_once_with("devnull")

def test_stdin_resolves_to_stdin_when_interactive(self):
stdin = mock.Mock(closed=False)
stdin.isatty.return_value = True
stdin.fileno.return_value = 0
seval = mock.Mock(return_value="STDIN")
with (
mock.patch("pysr.sr.sys.stdin", stdin),
mock.patch("pysr.sr.os.fstat"),
mock.patch("pysr.sr.jl", mock.Mock(seval=seval)),
):
self.assertTrue(_stdin_is_compatible())
self.assertEqual(_resolve_input_stream("stdin"), "STDIN")
seval.assert_called_once_with("stdin")

def test_custom_input_stream_is_preserved(self):
stdin = mock.Mock(closed=False)
stdin.isatty.return_value = False
seval = mock.Mock(return_value="STREAM")
with (
mock.patch("pysr.sr.sys.stdin", stdin),
mock.patch("pysr.sr.jl", mock.Mock(seval=seval)),
):
self.assertEqual(_resolve_input_stream("Main.my_stream"), "STREAM")
seval.assert_called_once_with("Main.my_stream")


class TestPipeline(unittest.TestCase):
def setUp(self):
# Using inspect,
Expand Down
Loading