|
7 | 7 |
|
8 | 8 | import asyncio |
9 | 9 | import functools |
10 | | -import random |
| 10 | +import secrets |
11 | 11 | import shutil |
12 | 12 | import threading |
13 | 13 | from collections.abc import Callable |
@@ -46,7 +46,6 @@ def _get_event_loop() -> asyncio.AbstractEventLoop: |
46 | 46 | def with_async_loop(func: Callable[..., Any], cancel_on_interrupt: bool = True) -> Callable[..., Any]: |
47 | 47 | """Decorate an async ``do_*`` command method to give it access to the event loop. |
48 | 48 |
|
49 | | -
|
50 | 49 | This decorator wraps a do_* command method. When the command is executed, |
51 | 50 | it submits the coroutine returned by the method to a background asyncio loop |
52 | 51 | and waits for the result synchronously (blocking the cmd2 loop, as expected |
@@ -79,6 +78,9 @@ def __init__(self) -> None: |
79 | 78 | super().__init__() |
80 | 79 | self.intro = 'Welcome to the Async Commands example. Type "help" to see available commands.' |
81 | 80 |
|
| 81 | + # Create an instance of SystemRandom |
| 82 | + self._secure_generator = secrets.SystemRandom() |
| 83 | + |
82 | 84 | if self.main_session.key_bindings is None: |
83 | 85 | self.main_session.key_bindings = KeyBindings() |
84 | 86 |
|
@@ -118,14 +120,14 @@ def handle_control_t(self, _event) -> None: |
118 | 120 | word = "fnord" |
119 | 121 |
|
120 | 122 | # Generate a random RGB color tuple |
121 | | - r = random.randint(0, 255) |
122 | | - g = random.randint(0, 255) |
123 | | - b = random.randint(0, 255) |
| 123 | + r = self._secure_generator.randint(0, 255) |
| 124 | + g = self._secure_generator.randint(0, 255) |
| 125 | + b = self._secure_generator.randint(0, 255) |
124 | 126 |
|
125 | 127 | # Get terminal width to calculate padding for right-alignment |
126 | 128 | cols, _ = shutil.get_terminal_size() |
127 | 129 | extra_width = cols - len(word) - 1 |
128 | | - padding_size = random.randint(0, extra_width) |
| 130 | + padding_size = self._secure_generator.randint(0, extra_width) |
129 | 131 | padding = " " * padding_size |
130 | 132 |
|
131 | 133 | # Use rich to generate the overall text to print out |
|
0 commit comments