Skip to content

Commit ffac4e4

Browse files
committed
Upgrade rich to 13.3.3
1 parent 141523c commit ffac4e4

26 files changed

+304
-197
lines changed

news/rich.vendor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Upgrade rich to 13.3.3

src/pip/_vendor/rich/_export_format.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
</head>
1313
<html>
1414
<body>
15-
<code>
16-
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">{code}</pre>
17-
</code>
15+
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code>{code}</code></pre>
1816
</body>
1917
</html>
2018
"""

src/pip/_vendor/rich/_fileno.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
from typing import IO, Callable
4+
5+
6+
def get_fileno(file_like: IO[str]) -> int | None:
7+
"""Get fileno() from a file, accounting for poorly implemented file-like objects.
8+
9+
Args:
10+
file_like (IO): A file-like object.
11+
12+
Returns:
13+
int | None: The result of fileno if available, or None if operation failed.
14+
"""
15+
fileno: Callable[[], int] | None = getattr(file_like, "fileno", None)
16+
if fileno is not None:
17+
try:
18+
return fileno()
19+
except Exception:
20+
# `fileno` is documented as potentially raising a OSError
21+
# Alas, from the issues, there are so many poorly implemented file-like objects,
22+
# that `fileno()` can raise just about anything.
23+
return None
24+
return None

src/pip/_vendor/rich/_null_file.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33

44

55
class NullFile(IO[str]):
6-
7-
# TODO: "mode", "name" and "closed" are only required for Python 3.6.
8-
9-
@property
10-
def mode(self) -> str:
11-
return ""
12-
13-
@property
14-
def name(self) -> str:
15-
return "NullFile"
16-
17-
def closed(self) -> bool:
18-
return False
19-
206
def close(self) -> None:
217
pass
228

src/pip/_vendor/rich/align.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def __rich_measure__(
303303
),
304304
width=60,
305305
style="on dark_blue",
306-
title="Algin",
306+
title="Align",
307307
)
308308

309309
console.print(

src/pip/_vendor/rich/ansi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:
4343
if start > position:
4444
yield _AnsiToken(ansi_text[position:start])
4545
if sgr:
46+
if sgr == "(":
47+
position = end + 1
48+
continue
4649
if sgr.endswith("m"):
4750
yield _AnsiToken("", sgr[1:-1], osc)
4851
else:

src/pip/_vendor/rich/cells.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _get_codepoint_cell_size(codepoint: int) -> int:
6060
"""Get the cell size of a character.
6161
6262
Args:
63-
character (str): A single character.
63+
codepoint (int): Codepoint of a character.
6464
6565
Returns:
6666
int: Number of cells (0, 1 or 2) occupied by that character.

src/pip/_vendor/rich/color.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,14 @@ def get_ansi_codes(self, foreground: bool = True) -> Tuple[str, ...]:
513513
def downgrade(self, system: ColorSystem) -> "Color":
514514
"""Downgrade a color system to a system with fewer colors."""
515515

516-
if self.type in [ColorType.DEFAULT, system]:
516+
if self.type in (ColorType.DEFAULT, system):
517517
return self
518518
# Convert to 8-bit color from truecolor color
519519
if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR:
520520
assert self.triplet is not None
521-
red, green, blue = self.triplet.normalized
522-
_h, l, s = rgb_to_hls(red, green, blue)
523-
# If saturation is under 10% assume it is grayscale
524-
if s < 0.1:
521+
_h, l, s = rgb_to_hls(*self.triplet.normalized)
522+
# If saturation is under 15% assume it is grayscale
523+
if s < 0.15:
525524
gray = round(l * 25.0)
526525
if gray == 0:
527526
color_number = 16
@@ -531,8 +530,13 @@ def downgrade(self, system: ColorSystem) -> "Color":
531530
color_number = 231 + gray
532531
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
533532

533+
red, green, blue = self.triplet
534+
six_red = red / 95 if red < 95 else 1 + (red - 95) / 40
535+
six_green = green / 95 if green < 95 else 1 + (green - 95) / 40
536+
six_blue = blue / 95 if blue < 95 else 1 + (blue - 95) / 40
537+
534538
color_number = (
535-
16 + 36 * round(red * 5.0) + 6 * round(green * 5.0) + round(blue * 5.0)
539+
16 + 36 * round(six_red) + 6 * round(six_green) + round(six_blue)
536540
)
537541
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
538542

src/pip/_vendor/rich/console.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
import io
32
import os
43
import platform
54
import sys
@@ -48,6 +47,7 @@
4847
from . import errors, themes
4948
from ._emoji_replace import _emoji_replace
5049
from ._export_format import CONSOLE_HTML_FORMAT, CONSOLE_SVG_FORMAT
50+
from ._fileno import get_fileno
5151
from ._log_render import FormatTimeCallable, LogRender
5252
from .align import Align, AlignMethod
5353
from .color import ColorSystem, blend_rgb
@@ -711,11 +711,6 @@ def __init__(
711711
self._force_terminal = None
712712
if force_terminal is not None:
713713
self._force_terminal = force_terminal
714-
else:
715-
# If FORCE_COLOR env var has any value at all, we force terminal.
716-
force_color = self._environ.get("FORCE_COLOR")
717-
if force_color is not None:
718-
self._force_terminal = True
719714

720715
self._file = file
721716
self.quiet = quiet
@@ -758,7 +753,7 @@ def __init__(
758753
self._is_alt_screen = False
759754

760755
def __repr__(self) -> str:
761-
return f"<console width={self.width} {str(self._color_system)}>"
756+
return f"<console width={self.width} {self._color_system!s}>"
762757

763758
@property
764759
def file(self) -> IO[str]:
@@ -949,6 +944,15 @@ def is_terminal(self) -> bool:
949944
# Return False for Idle which claims to be a tty but can't handle ansi codes
950945
return False
951946

947+
if self.is_jupyter:
948+
# return False for Jupyter, which may have FORCE_COLOR set
949+
return False
950+
951+
# If FORCE_COLOR env var has any value at all, we assume a terminal.
952+
force_color = self._environ.get("FORCE_COLOR")
953+
if force_color is not None:
954+
self._force_terminal = True
955+
952956
isatty: Optional[Callable[[], bool]] = getattr(self.file, "isatty", None)
953957
try:
954958
return False if isatty is None else isatty()
@@ -1146,7 +1150,7 @@ def status(
11461150
status: RenderableType,
11471151
*,
11481152
spinner: str = "dots",
1149-
spinner_style: str = "status.spinner",
1153+
spinner_style: StyleType = "status.spinner",
11501154
speed: float = 1.0,
11511155
refresh_per_second: float = 12.5,
11521156
) -> "Status":
@@ -1523,7 +1527,7 @@ def check_text() -> None:
15231527
if text:
15241528
sep_text = Text(sep, justify=justify, end=end)
15251529
append(sep_text.join(text))
1526-
del text[:]
1530+
text.clear()
15271531

15281532
for renderable in objects:
15291533
renderable = rich_cast(renderable)
@@ -2006,12 +2010,11 @@ def _check_buffer(self) -> None:
20062010
if WINDOWS:
20072011
use_legacy_windows_render = False
20082012
if self.legacy_windows:
2009-
try:
2013+
fileno = get_fileno(self.file)
2014+
if fileno is not None:
20102015
use_legacy_windows_render = (
2011-
self.file.fileno() in _STD_STREAMS_OUTPUT
2016+
fileno in _STD_STREAMS_OUTPUT
20122017
)
2013-
except (ValueError, io.UnsupportedOperation):
2014-
pass
20152018

20162019
if use_legacy_windows_render:
20172020
from pip._vendor.rich._win32_console import LegacyWindowsTerm
@@ -2026,13 +2029,31 @@ def _check_buffer(self) -> None:
20262029
# Either a non-std stream on legacy Windows, or modern Windows.
20272030
text = self._render_buffer(self._buffer[:])
20282031
# https://bugs.python.org/issue37871
2032+
# https://github.com/python/cpython/issues/82052
2033+
# We need to avoid writing more than 32Kb in a single write, due to the above bug
20292034
write = self.file.write
2030-
for line in text.splitlines(True):
2031-
try:
2032-
write(line)
2033-
except UnicodeEncodeError as error:
2034-
error.reason = f"{error.reason}\n*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***"
2035-
raise
2035+
# Worse case scenario, every character is 4 bytes of utf-8
2036+
MAX_WRITE = 32 * 1024 // 4
2037+
try:
2038+
if len(text) <= MAX_WRITE:
2039+
write(text)
2040+
else:
2041+
batch: List[str] = []
2042+
batch_append = batch.append
2043+
size = 0
2044+
for line in text.splitlines(True):
2045+
if size + len(line) > MAX_WRITE and batch:
2046+
write("".join(batch))
2047+
batch.clear()
2048+
size = 0
2049+
batch_append(line)
2050+
size += len(line)
2051+
if batch:
2052+
write("".join(batch))
2053+
batch.clear()
2054+
except UnicodeEncodeError as error:
2055+
error.reason = f"{error.reason}\n*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***"
2056+
raise
20362057
else:
20372058
text = self._render_buffer(self._buffer[:])
20382059
try:

src/pip/_vendor/rich/default_styles.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@
138138
"tree.line": Style(),
139139
"markdown.paragraph": Style(),
140140
"markdown.text": Style(),
141-
"markdown.emph": Style(italic=True),
141+
"markdown.em": Style(italic=True),
142+
"markdown.emph": Style(italic=True), # For commonmark backwards compatibility
142143
"markdown.strong": Style(bold=True),
143-
"markdown.code": Style(bgcolor="black", color="bright_white"),
144-
"markdown.code_block": Style(dim=True, color="cyan", bgcolor="black"),
144+
"markdown.code": Style(bold=True, color="cyan", bgcolor="black"),
145+
"markdown.code_block": Style(color="cyan", bgcolor="black"),
145146
"markdown.block_quote": Style(color="magenta"),
146147
"markdown.list": Style(color="cyan"),
147148
"markdown.item": Style(),
@@ -157,7 +158,8 @@
157158
"markdown.h6": Style(italic=True),
158159
"markdown.h7": Style(italic=True, dim=True),
159160
"markdown.link": Style(color="bright_blue"),
160-
"markdown.link_url": Style(color="blue"),
161+
"markdown.link_url": Style(color="blue", underline=True),
162+
"markdown.s": Style(strike=True),
161163
"iso8601.date": Style(color="blue"),
162164
"iso8601.time": Style(color="magenta"),
163165
"iso8601.timezone": Style(color="yellow"),

0 commit comments

Comments
 (0)