Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: session: only create one session to access options when there's no current session #1565

Merged
merged 1 commit into from
May 30, 2023
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
11 changes: 9 additions & 2 deletions pyocd/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from inspect import (getfullargspec, signature)
from types import SimpleNamespace
from typing import (Any, Callable, Generator, Sequence, Union, cast, Dict, List, Mapping, Optional, TYPE_CHECKING)
from typing_extensions import Self

from . import exceptions
from .options_manager import OptionsManager
Expand Down Expand Up @@ -94,8 +95,11 @@ class Session(Notifier):
## @brief Weak reference to the most recently created session.
_current_session: Optional[weakref.ref] = None

## An empty session used for options when there is no other session available.
_options_session: Optional["Session"] = None

@classmethod
def get_current(cls) -> "Session":
def get_current(cls) -> Self:
"""@brief Return the most recently created Session instance or a default Session.

By default this method will return the most recently created Session object that is
Expand All @@ -111,7 +115,10 @@ def get_current(cls) -> "Session":
if session is not None:
return session

return Session(None)
# There isn't another session available, so lazily create the options session and return it.
if cls._options_session is None:
cls._options_session = cls(None)
return cls._options_session

def __init__(
self,
Expand Down