Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions logbar/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@

logger = LogBar.shared()

@contextmanager
def _fallback_nullcontext():
yield


def _safe_nullcontext(_nullcontext=nullcontext, _fallback=_fallback_nullcontext):
if callable(_nullcontext):
return _nullcontext()
return _fallback()


# ANSI helpers for the animated title effect + colour styling
ANSI_RESET = "\033[0m"
ANSI_BOLD_RESET = "\033[22m"
Expand Down Expand Up @@ -492,15 +503,15 @@ def manual(self ):
def _render_lock_context(self):
lock_provider = render_lock if callable(render_lock) else None
if lock_provider is None:
return nullcontext(), False
return _safe_nullcontext(), False

try:
lock_obj = lock_provider()
except Exception:
return nullcontext(), False
return _safe_nullcontext(), False

if lock_obj is None:
return nullcontext(), False
return _safe_nullcontext(), False

if hasattr(lock_obj, "__enter__") and hasattr(lock_obj, "__exit__"):
return lock_obj, True
Expand All @@ -519,7 +530,7 @@ def _managed_lock():

return _managed_lock(), True

return nullcontext(), False
return _safe_nullcontext(), False

def _fallback_detach_registry(self) -> None:
try:
Expand All @@ -532,7 +543,7 @@ def _fallback_detach_registry(self) -> None:
return

state_lock = getattr(logbar_module, "_STATE_LOCK", None)
lock_context = nullcontext()
lock_context = _safe_nullcontext()

if state_lock is not None:
if hasattr(state_lock, "__enter__") and hasattr(state_lock, "__exit__"):
Expand Down