Repository: Textualize/rich
File: rich/console.py (Python)

Code before:
                new_segments, self.width, pad=False
            ):
                buffer_extend(line)

    def _check_and_render_buffer(self) -> None:
        """Check if the buffer may be rendered. Render it if it can."""
        if self.quiet:
            del self._buffer[:]
            return
        with self._lock:
            if self._buffer_index == 0:

                if self.record:
                    with self._record_buffer_lock:
                        self._record_buffer.extend(self._buffer[:])

                if self.is_jupyter:  # pragma: no cover
                    from .jupyter import display

                    display(self._buffer, self._render_buffer(self._buffer[:]))
                    del self._buffer[:]
                else:
                    if WINDOWS:
                        try:
                            file_no = self.file.fileno()
                            stdout_num = sys.stdout.fileno()
                            stderr_num = sys.stderr.fileno()
                        except (ValueError, io.UnsupportedOperation):
                            file_no = -1
                            stdout_num = 1
                            stderr_num = 2

                        is_std_stream = file_no in (stdout_num, stderr_num)
                        legacy_windows_std = self.legacy_windows and is_std_stream
                        if legacy_windows_std:
                            from rich._win32_console import LegacyWindowsTerm
                            from rich._windows_renderer import legacy_windows_render

                            legacy_windows_render(
                                self._buffer[:], LegacyWindowsTerm(self.file)
                            )
                        else:
                            # Either a non-std stream on legacy Windows, or modern Windows.
                            text = self._render_buffer(self._buffer[:])
                            # https://bugs.python.org/issue37871
                            write = self.file.write
                            for line in text.splitlines(True):
                                try:
                                    write(line)
                                except UnicodeEncodeError as error:
                                    error.reason = f"{error.reason}\n*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***"

Review comment (suggestion):
These probably don't need to be in a try except blog. Suggest replacing them with constants. `pty.STDOUT_FILENO` and `pty.STDERR_FILENO` contain the values so they don't need to be hard coded.

Code after:
                renderables = hook.process_renderables(renderables)
            new_segments: List[Segment] = []
            extend = new_segments.extend
            render = self.render
            render_options = self.options
            for renderable in renderables:
                extend(render(renderable, render_options))
            buffer_extend = self._buffer.extend
            for line in Segment.split_and_crop_lines(
                new_segments, self.width, pad=False
            ):
                buffer_extend(line)

    def _check_buffer(self) -> None:
        """Check if the buffer may be rendered. Render it if it can (e.g. Console.quiet is False)
        Rendering is supported on Windows, Unix and Jupyter environments. For
        legacy Windows consoles, the win32 API is called directly.
        This method will also record what it renders if recording is enabled via Console.record.
        """
        if self.quiet:
            del self._buffer[:]
            return
        with self._lock:
            if self._buffer_index == 0:

                if self.record:
                    with self._record_buffer_lock:
                        self._record_buffer.extend(self._buffer[:])

                if self.is_jupyter:  # pragma: no cover
                    from .jupyter import display

                    display(self._buffer, self._render_buffer(self._buffer[:]))
                    del self._buffer[:]
                else:
                    if WINDOWS:
                        try:
                            file_no = self.file.fileno()
                        except (ValueError, io.UnsupportedOperation):
                            file_no = -1

                        stdout_num = _STDOUT_FILENO
                        stderr_num = _STDERR_FILENO
                        is_std_stream = file_no in (stdout_num, stderr_num)
                        legacy_windows_std = self.legacy_windows and is_std_stream
                        if legacy_windows_std:
                            from rich._win32_console import LegacyWindowsTerm
                            from rich._windows_renderer import legacy_windows_render

                            legacy_windows_render(
                                self._buffer[:], LegacyWindowsTerm(self.file)