Skip to content

Commit

Permalink
Review feedback.
Browse files Browse the repository at this point in the history
# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
stuhood committed Mar 16, 2021
1 parent e52f630 commit 0fd7dd6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/python/pants/engine/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import sys
from typing import Callable, Optional, cast
from typing import Callable, Optional, TextIO

from colors import blue, cyan, green, magenta, red, yellow

Expand All @@ -19,9 +19,9 @@ class Console:

def __init__(
self,
stdin=None,
stdout=None,
stderr=None,
stdin: TextIO | None = None,
stdout: TextIO | None = None,
stderr: TextIO | None = None,
use_colors: bool = True,
session: Optional[SchedulerSession] = None,
):
Expand All @@ -35,19 +35,19 @@ def __init__(
self._session = session

@property
def stdin(self):
def stdin(self) -> TextIO:
if self._session:
self._session.teardown_dynamic_ui()
return self._stdin

@property
def stdout(self):
def stdout(self) -> TextIO:
if self._session:
self._session.teardown_dynamic_ui()
return self._stdout

@property
def stderr(self):
def stderr(self) -> TextIO:
if self._session:
self._session.teardown_dynamic_ui()
return self._stderr
Expand All @@ -56,7 +56,7 @@ def input(self, prompt: str | None = None) -> str:
"""Equivalent to the `input` builtin, but clears any running UI before rendering."""
if prompt is not None:
self.write_stdout(prompt)
return cast(str, self.stdin.readline()).rstrip("\n")
return self.stdin.readline().rstrip("\n")

def write_stdout(self, payload: str) -> None:
self.stdout.write(payload)
Expand Down
6 changes: 5 additions & 1 deletion src/python/pants/init/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ def initialize_stdio(global_bootstrap_options: OptionValueContainer) -> Iterator
log_path,
)
sys.stdin = TextIOWrapper(
BufferedReader(raw_stdin), encoding=locale.getpreferredencoding(False)
BufferedReader(raw_stdin),
# NB: We set the default encoding explicitly to bypass logic in the TextIOWrapper
# constructor that would poke the underlying file (which is not valid until a
# `stdio_destination` is set).
encoding=locale.getpreferredencoding(False),
)

sys.__stdin__, sys.__stdout__, sys.__stderr__ = sys.stdin, sys.stdout, sys.stderr
Expand Down
4 changes: 2 additions & 2 deletions src/rust/engine/src/externs/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ py_class!(pub class PyStdioRead |py| {
let read = py.allow_threads(|| {
stdio::get_destination().read_stdin(&mut buffer)
}).map_err(|e| PyErr::new::<exc::Exception, _>(py, (e.to_string(),)))?;
// NB: copy_from_slice requires a buffer of equal length, so we are forced to copy in some
// trailing zeros.
// NB: `as_mut_slice` exposes a `&[Cell<u8>]`, which we can't use directly in `read`. We use
// `copy_from_slice` instead, which unfortunately involves some extra copying.
py_buffer.copy_from_slice(py, &buffer)?;
Ok(read)
}
Expand Down

0 comments on commit 0fd7dd6

Please sign in to comment.