refactor: replace try-except blocks with contextlib.suppress for cleaner error handling across multiple files
This commit is contained in:
+8
-22
@@ -5,7 +5,7 @@ import os
|
||||
import select
|
||||
import signal
|
||||
import sys
|
||||
from contextlib import nullcontext
|
||||
from contextlib import nullcontext, suppress
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -14,11 +14,9 @@ if sys.platform == "win32":
|
||||
if sys.stdout.encoding != "utf-8":
|
||||
os.environ["PYTHONIOENCODING"] = "utf-8"
|
||||
# Re-open stdout/stderr with UTF-8 encoding
|
||||
try:
|
||||
with suppress(Exception):
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
import typer
|
||||
from loguru import logger
|
||||
@@ -83,35 +81,29 @@ def _flush_pending_tty_input() -> None:
|
||||
except Exception:
|
||||
return
|
||||
|
||||
try:
|
||||
with suppress(Exception):
|
||||
import termios
|
||||
|
||||
termios.tcflush(fd, termios.TCIFLUSH)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
with suppress(Exception):
|
||||
while True:
|
||||
ready, _, _ = select.select([fd], [], [], 0)
|
||||
if not ready:
|
||||
break
|
||||
if not os.read(fd, 4096):
|
||||
break
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def _restore_terminal() -> None:
|
||||
"""Restore terminal to its original state (echo, line buffering, etc.)."""
|
||||
if _SAVED_TERM_ATTRS is None:
|
||||
return
|
||||
try:
|
||||
with suppress(Exception):
|
||||
import termios
|
||||
|
||||
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, _SAVED_TERM_ATTRS)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _init_prompt_session() -> None:
|
||||
@@ -119,12 +111,10 @@ def _init_prompt_session() -> None:
|
||||
global _PROMPT_SESSION, _SAVED_TERM_ATTRS
|
||||
|
||||
# Save terminal state so we can restore it on exit
|
||||
try:
|
||||
with suppress(Exception):
|
||||
import termios
|
||||
|
||||
_SAVED_TERM_ATTRS = termios.tcgetattr(sys.stdin.fileno())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
from nanobot.config.paths import get_cli_history_path
|
||||
|
||||
@@ -936,10 +926,8 @@ def _run_gateway(
|
||||
config.gateway.host or "127.0.0.1", port
|
||||
)
|
||||
writer.close()
|
||||
try:
|
||||
with suppress(Exception):
|
||||
await writer.wait_closed()
|
||||
except Exception:
|
||||
pass
|
||||
break
|
||||
except OSError:
|
||||
await asyncio.sleep(0.1)
|
||||
@@ -1520,10 +1508,8 @@ def _login_openai_codex() -> None:
|
||||
from oauth_cli_kit import get_token, login_oauth_interactive
|
||||
|
||||
token = None
|
||||
try:
|
||||
with suppress(Exception):
|
||||
token = get_token()
|
||||
except Exception:
|
||||
pass
|
||||
if not (token and token.access):
|
||||
console.print("[cyan]Starting interactive OAuth login...[/cyan]\n")
|
||||
token = login_oauth_interactive(
|
||||
|
||||
Reference in New Issue
Block a user