summaryrefslogtreecommitdiff
path: root/src/pip/_vendor/rich/syntax.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/pip/_vendor/rich/syntax.py')
-rw-r--r--src/pip/_vendor/rich/syntax.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/pip/_vendor/rich/syntax.py b/src/pip/_vendor/rich/syntax.py
index 01bdd0439..25b226a3a 100644
--- a/src/pip/_vendor/rich/syntax.py
+++ b/src/pip/_vendor/rich/syntax.py
@@ -4,6 +4,7 @@ import re
import sys
import textwrap
from abc import ABC, abstractmethod
+from pathlib import Path
from typing import (
Any,
Dict,
@@ -338,8 +339,7 @@ class Syntax(JupyterMixin):
Returns:
[Syntax]: A Syntax object that may be printed to the console
"""
- with open(path, "rt", encoding=encoding) as code_file:
- code = code_file.read()
+ code = Path(path).read_text(encoding=encoding)
if not lexer:
lexer = cls.guess_lexer(path, code=code)
@@ -494,7 +494,10 @@ class Syntax(JupyterMixin):
# Skip over tokens until line start
while line_no < _line_start:
- _token_type, token = next(tokens)
+ try:
+ _token_type, token = next(tokens)
+ except StopIteration:
+ break
yield (token, None)
if token.endswith("\n"):
line_no += 1
@@ -671,6 +674,8 @@ class Syntax(JupyterMixin):
line_offset = max(0, start_line - 1)
lines: Union[List[Text], Lines] = text.split("\n", allow_blank=ends_on_nl)
if self.line_range:
+ if line_offset > len(lines):
+ return
lines = lines[line_offset:end_line]
if self.indent_guides and not options.ascii_only: