Skip to content

Commit ab78c63

Browse files
committed
snapshot.py(refactor[performance]): Extract helper function for session snapshot creation
why: Address PERF203 linting warning about try-except blocks within loops, which can cause performance overhead. what: - Created _create_session_snapshot_safely helper function to isolate exception handling - Refactored ServerSnapshot.from_server to use the helper function instead of inline try-except - Added comprehensive docstrings explaining the purpose and implementation - Maintained the same behavior for both test and production environments - Improved code readability and maintainability This approach resolves the linting warning while preserving the intended behavior and special handling for test environments.
1 parent 3b96f47 commit ab78c63

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

Diff for: src/libtmux/snapshot.py

+54-18
Original file line numberDiff line numberDiff line change
@@ -673,25 +673,11 @@ def from_server(
673673
# For doctest support, handle case where there might not be sessions
674674
if hasattr(server, "sessions") and server.sessions:
675675
for session in server.sessions:
676-
try:
677-
session_snapshot = SessionSnapshot.from_session(
678-
session,
679-
capture_content=include_content,
680-
server_snapshot=snapshot,
681-
)
676+
session_snapshot = _create_session_snapshot_safely(
677+
session, include_content, snapshot
678+
)
679+
if session_snapshot is not None:
682680
sessions_snapshot.append(session_snapshot)
683-
except Exception as e:
684-
# For doctests, just continue if we can't create a session snapshot
685-
if "test" in sys.modules:
686-
import warnings
687-
688-
warnings.warn(
689-
f"Failed to create session snapshot: {e}",
690-
stacklevel=2,
691-
)
692-
continue
693-
else:
694-
raise
695681

696682
# Set additional attributes
697683
object.__setattr__(snapshot, "sessions_snapshot", sessions_snapshot)
@@ -704,6 +690,56 @@ def from_server(
704690
return snapshot
705691

706692

693+
def _create_session_snapshot_safely(
694+
session: Session, include_content: bool, server_snapshot: ServerSnapshot
695+
) -> SessionSnapshot | None:
696+
"""Create a session snapshot with safe error handling for testability.
697+
698+
This helper function isolates the try-except block from the loop to address the
699+
PERF203 linting warning about try-except within a loop. By moving the exception
700+
handling to a separate function, we maintain the same behavior while improving
701+
the code structure and performance.
702+
703+
Parameters
704+
----------
705+
session : Session
706+
The session to create a snapshot from
707+
include_content : bool
708+
Whether to capture the content of the panes
709+
server_snapshot : ServerSnapshot
710+
The server snapshot this session belongs to
711+
712+
Returns
713+
-------
714+
SessionSnapshot | None
715+
A snapshot of the session, or None if creation failed in a test environment
716+
717+
Notes
718+
-----
719+
In test environments, failures to create snapshots are logged as warnings and
720+
None is returned. In production environments, exceptions are re-raised.
721+
"""
722+
try:
723+
return SessionSnapshot.from_session(
724+
session,
725+
capture_content=include_content,
726+
server_snapshot=server_snapshot,
727+
)
728+
except Exception as e:
729+
# For doctests, just log and return None if we can't create a session snapshot
730+
if "test" in sys.modules:
731+
import warnings
732+
733+
warnings.warn(
734+
f"Failed to create session snapshot: {e}",
735+
stacklevel=2,
736+
)
737+
return None
738+
else:
739+
# In production, we want the exception to propagate
740+
raise
741+
742+
707743
def filter_snapshot(
708744
snapshot: ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot,
709745
filter_func: t.Callable[

0 commit comments

Comments
 (0)