Skip to content

Commit 1be7e8d

Browse files
committed
Foldd back _complete_render_option into it's public counterpart
1 parent 8f4fbdb commit 1be7e8d

File tree

2 files changed

+15
-37
lines changed

2 files changed

+15
-37
lines changed

pyinstrument/__main__.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import shutil
1010
import sys
1111
import time
12-
from typing import Any, List, Optional, TextIO, Tuple, cast
12+
from typing import Any, List, TextIO, cast
1313

1414
import pyinstrument
1515
from pyinstrument import Profiler, renderers
@@ -410,7 +410,10 @@ def store_and_consume_remaining(
410410

411411

412412
def compute_render_options(
413-
options: CommandLineOptions, renderer_class: type[renderers.Renderer], output_file: TextIO
413+
options: CommandLineOptions,
414+
renderer_class: type[renderers.Renderer],
415+
unicode_support: bool,
416+
color_support: bool,
414417
) -> dict[str, Any]:
415418
"""
416419
Given a list of CommandLineOptions, compute the
@@ -420,33 +423,9 @@ def compute_render_options(
420423
421424
"""
422425

423-
unicode_support: bool = file_supports_unicode(output_file)
424-
color_support: bool = file_supports_color(output_file)
425-
426-
error, render_options = _compute_render_options(
427-
options, renderer_class, unicode_support, color_support
428-
)
429-
if error is not None:
430-
raise OptionsParseError(error)
431-
assert render_options is not None
432-
return render_options
433-
434-
435-
def _compute_render_options(
436-
options: CommandLineOptions,
437-
renderer_class: type[renderers.Renderer],
438-
unicode_support: bool,
439-
color_support: bool,
440-
) -> Tuple[Optional[str], Optional[dict[str, Any]]]:
441-
"""
442-
Similar as compute_render_options, but return a tuple (error message, data)
443-
if there is an error; this will let us reuse _compute_render_options in magics.
444-
445-
output_file, has been replaced by unicode_support:bool, color_support:bool
446-
"""
447426
# parse show/hide options
448427
if options.hide_fnmatch is not None and options.hide_regex is not None:
449-
return ("You can‘t specify both --hide and --hide-regex", None)
428+
raise OptionsParseError("You can‘t specify both --hide and --hide-regex")
450429

451430
hide_regex: str | None
452431
show_regex: str | None
@@ -461,7 +440,7 @@ def _compute_render_options(
461440
options.show_all,
462441
]
463442
if show_options_used.count(True) > 1:
464-
return ("You can only specify one of --show, --show-regex and --show-all", None)
443+
raise OptionsParseError("You can only specify one of --show, --show-regex and --show-all")
465444

466445
if options.show_fnmatch is not None:
467446
show_regex = fnmatch.translate(options.show_fnmatch)
@@ -510,7 +489,7 @@ def _compute_render_options(
510489

511490
keypath.set_value_at_keypath(render_options, key, parsed_value)
512491

513-
return None, render_options
492+
return render_options
514493

515494

516495
class OptionsParseError(Exception):
@@ -521,7 +500,10 @@ def create_renderer(
521500
renderer_class: type[renderers.Renderer], options: CommandLineOptions, output_file: TextIO
522501
) -> renderers.Renderer:
523502
render_options = compute_render_options(
524-
options, renderer_class=renderer_class, output_file=output_file
503+
options,
504+
renderer_class=renderer_class,
505+
unicode_support=file_supports_unicode(output_file),
506+
color_support=file_supports_color(output_file),
525507
)
526508

527509
try:

pyinstrument/magic/magic.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from IPython.display import IFrame, display
1515

1616
from pyinstrument import Profiler, renderers
17-
from pyinstrument.__main__ import _compute_render_options
17+
from pyinstrument.__main__ import compute_render_options
1818
from pyinstrument.frame import Frame
1919
from pyinstrument.frame_ops import delete_frame_from_tree
2020
from pyinstrument.processors import ProcessorOptions
@@ -241,17 +241,13 @@ def pyinstrument(self, line, cell=None):
241241
)
242242
return
243243

244-
html_error, html_config = _compute_render_options(
244+
html_config = compute_render_options(
245245
args, renderer_class=HTMLRenderer, unicode_support=True, color_support=True
246246
)
247-
if html_error is not None:
248-
raise ValueError(html_error)
249247

250-
text_error, text_config = _compute_render_options(
248+
text_config = compute_render_options(
251249
args, renderer_class=HTMLRenderer, unicode_support=True, color_support=True
252250
)
253-
if text_error is not None:
254-
raise ValueError(text_error)
255251

256252
html_renderer = renderers.HTMLRenderer(show_all=args.show_all, timeline=args.timeline)
257253
html_renderer.preprocessors.append(strip_ipython_frames_processor)

0 commit comments

Comments
 (0)