Skip to content

Commit 4b9faca

Browse files
committed
Updated comments and renamed a function.
1 parent 1c8d8f3 commit 4b9faca

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

cmd2/cmd2.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class AsyncAlert:
309309

310310

311311
class _ConsoleCache(threading.local):
312-
"""Thread-local storage for cached Rich consoles used by print_to()."""
312+
"""Thread-local storage for cached Rich consoles used by core print methods."""
313313

314314
def __init__(self) -> None:
315315
self.stdout: Cmd2BaseConsole | None = None
@@ -449,7 +449,7 @@ def __init__(
449449
self.scripts_add_to_history = True # Scripts and pyscripts add commands to history
450450
self.timing = False # Prints elapsed time for each command
451451

452-
# Thread-local storage for cached Rich consoles used by print_to()
452+
# Cached Rich consoles used by core print methods.
453453
self._console_cache = _ConsoleCache()
454454

455455
# The maximum number of items to display in a completion table. If the number of completion
@@ -1335,20 +1335,21 @@ def visible_prompt(self) -> str:
13351335
"""
13361336
return su.strip_style(self.prompt)
13371337

1338-
def _create_base_printing_console(
1338+
def _get_core_print_console(
13391339
self,
13401340
*,
13411341
file: IO[str],
13421342
emoji: bool,
13431343
markup: bool,
13441344
highlight: bool,
13451345
) -> Cmd2BaseConsole:
1346-
"""Get a Cmd2BaseConsole configured for the specified stream and formatting settings.
1346+
"""Get a console configured for the specified stream and formatting settings.
13471347
1348-
This method manages a thread-local cache for consoles printing to self.stdout or
1349-
sys.stderr to avoid the overhead of repeated initialization. It returns a cached
1350-
instance if its configuration matches the request. Otherwise, a new console is
1351-
created and cached.
1348+
This method is intended for internal use by cmd2's core print methods.
1349+
To avoid the overhead of repeated initialization, it manages a thread-local
1350+
cache for consoles targeting ``self.stdout`` or ``sys.stderr``. It returns a cached
1351+
instance if its configuration matches the request. For all other streams, or if
1352+
the configuration has changed, a new console is created.
13521353
13531354
Note: This implementation works around a bug in Rich where passing formatting settings
13541355
(emoji, markup, and highlight) directly to console.print() or console.log() does not
@@ -1437,7 +1438,7 @@ def print_to(
14371438
See the Rich documentation for more details on emoji codes, markup tags, and highlighting.
14381439
"""
14391440
try:
1440-
self._create_base_printing_console(
1441+
self._get_core_print_console(
14411442
file=file,
14421443
emoji=emoji,
14431444
markup=markup,
@@ -1753,7 +1754,7 @@ def ppaged(
17531754
soft_wrap = True
17541755

17551756
# Generate the bytes to send to the pager
1756-
console = self._create_base_printing_console(
1757+
console = self._get_core_print_console(
17571758
file=self.stdout,
17581759
emoji=emoji,
17591760
markup=markup,

tests/test_cmd2.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,14 +2536,12 @@ def test_poutput_all_keyword_args(outsim_app):
25362536
(True, False, True),
25372537
],
25382538
)
2539-
def test_create_base_printing_console_caching(
2540-
base_app: cmd2.Cmd, stream: str, emoji: bool, markup: bool, highlight: bool
2541-
) -> None:
2542-
"""Test that base printing consoles are cached and reused when settings match."""
2539+
def test_get_core_print_console_caching(base_app: cmd2.Cmd, stream: str, emoji: bool, markup: bool, highlight: bool) -> None:
2540+
"""Test that printing consoles are cached and reused when settings match."""
25432541
file = sys.stderr if stream == 'stderr' else base_app.stdout
25442542

25452543
# Initial creation
2546-
console1 = base_app._create_base_printing_console(
2544+
console1 = base_app._get_core_print_console(
25472545
file=file,
25482546
emoji=emoji,
25492547
markup=markup,
@@ -2555,7 +2553,7 @@ def test_create_base_printing_console_caching(
25552553
assert cached is console1
25562554

25572555
# Identical request should return the same object
2558-
console2 = base_app._create_base_printing_console(
2556+
console2 = base_app._get_core_print_console(
25592557
file=file,
25602558
emoji=emoji,
25612559
markup=markup,
@@ -2568,20 +2566,20 @@ def test_create_base_printing_console_caching(
25682566
'stream',
25692567
['stdout', 'stderr'],
25702568
)
2571-
def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: str) -> None:
2569+
def test_get_core_print_console_invalidation(base_app: cmd2.Cmd, stream: str) -> None:
25722570
"""Test that changing settings, theme, or ALLOW_STYLE invalidates the cache."""
25732571
file = sys.stderr if stream == 'stderr' else base_app.stdout
25742572

25752573
# Initial creation
2576-
console1 = base_app._create_base_printing_console(
2574+
console1 = base_app._get_core_print_console(
25772575
file=file,
25782576
emoji=True,
25792577
markup=True,
25802578
highlight=True,
25812579
)
25822580

25832581
# Changing emoji should create a new console
2584-
console2 = base_app._create_base_printing_console(
2582+
console2 = base_app._get_core_print_console(
25852583
file=file,
25862584
emoji=False,
25872585
markup=True,
@@ -2591,7 +2589,7 @@ def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: s
25912589
assert getattr(base_app._console_cache, stream) is console2
25922590

25932591
# Changing markup should create a new console
2594-
console3 = base_app._create_base_printing_console(
2592+
console3 = base_app._get_core_print_console(
25952593
file=file,
25962594
emoji=False,
25972595
markup=False,
@@ -2601,7 +2599,7 @@ def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: s
26012599
assert getattr(base_app._console_cache, stream) is console3
26022600

26032601
# Changing highlight should create a new console
2604-
console4 = base_app._create_base_printing_console(
2602+
console4 = base_app._get_core_print_console(
26052603
file=file,
26062604
emoji=False,
26072605
markup=False,
@@ -2614,7 +2612,7 @@ def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: s
26142612
orig_allow_style = ru.ALLOW_STYLE
26152613
try:
26162614
ru.ALLOW_STYLE = ru.AllowStyle.ALWAYS if orig_allow_style != ru.AllowStyle.ALWAYS else ru.AllowStyle.NEVER
2617-
console5 = base_app._create_base_printing_console(
2615+
console5 = base_app._get_core_print_console(
26182616
file=file,
26192617
emoji=False,
26202618
markup=False,
@@ -2631,7 +2629,7 @@ def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: s
26312629
old_theme = ru.APP_THEME
26322630
try:
26332631
ru.APP_THEME = Theme()
2634-
console6 = base_app._create_base_printing_console(
2632+
console6 = base_app._get_core_print_console(
26352633
file=file,
26362634
emoji=False,
26372635
markup=False,
@@ -2643,11 +2641,11 @@ def test_create_base_printing_console_invalidation(base_app: cmd2.Cmd, stream: s
26432641
ru.APP_THEME = old_theme
26442642

26452643

2646-
def test_create_base_printing_console_non_cached(base_app: cmd2.Cmd) -> None:
2644+
def test_get_core_print_console_non_cached(base_app: cmd2.Cmd) -> None:
26472645
"""Test that arbitrary file objects are not cached."""
26482646
file = io.StringIO()
26492647

2650-
console1 = base_app._create_base_printing_console(
2648+
console1 = base_app._get_core_print_console(
26512649
file=file,
26522650
emoji=True,
26532651
markup=True,
@@ -2659,7 +2657,7 @@ def test_create_base_printing_console_non_cached(base_app: cmd2.Cmd) -> None:
26592657
assert base_app._console_cache.stderr is None
26602658

26612659
# A second request for the same file should still create a new object
2662-
console2 = base_app._create_base_printing_console(
2660+
console2 = base_app._get_core_print_console(
26632661
file=file,
26642662
emoji=True,
26652663
markup=True,

0 commit comments

Comments
 (0)