Skip to content

clib.session: Refactor to simplify the checking of currently open session #3523

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

Merged
merged 3 commits into from
Oct 16, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 16, 2024

Just some refactoring to simplify the codes. See the changes for more details.

@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Oct 16, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 16, 2024
@@ -160,7 +160,7 @@ def session_pointer(self) -> ctp.c_void_p:
If trying to access without a currently open GMT session (i.e., outside of
the context manager).
"""
if not hasattr(self, "_session_pointer") or self._session_pointer is None:
if getattr(self, "_session_pointer", None) is None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not hasattr(self, "_session_pointer") or self._session_pointer is None: is equivalent to if getattr(self, "_session_pointer", None) is None:.

@@ -338,19 +338,12 @@ def create(self, name: str):
name
A name for this session. Doesn't really affect the outcome.
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If self.session_pointer is defined and is not None, then we know we have an open session. The try..except statement was added in 2018 (#210) and is not necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #3448 (comment), a with contextlib.supress block should ideally be used when there is only one line below it. I feel like we should keep the previous try-except block.

Copy link
Member Author

@seisman seisman Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's impossible that the msg assignment and the raise GMTCLibError lines can raise a GMTCLibNoSessionError exception, so we should be safe.

Edit: Read astral-sh/ruff#1947 again, actually it's unclear if the raise GMTCLibError statement will be executed or not. I think we have to go back to try..except.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted back to try..except in da9c5e1 and improve the comments to make it easier to understand.

@seisman seisman removed the needs review This PR has higher priority and needs review. label Oct 16, 2024
@seisman seisman marked this pull request as draft October 16, 2024 04:41
@seisman seisman force-pushed the refactor/session-pointer branch from b960654 to 42ae467 Compare October 16, 2024 04:49
@seisman seisman force-pushed the refactor/session-pointer branch from 42ae467 to 3d0c91b Compare October 16, 2024 04:54
@seisman seisman marked this pull request as ready for review October 16, 2024 04:54
@seisman seisman added the needs review This PR has higher priority and needs review. label Oct 16, 2024
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Oct 16, 2024
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Oct 16, 2024
@seisman seisman merged commit c0ad049 into main Oct 16, 2024
21 checks passed
@seisman seisman deleted the refactor/session-pointer branch October 16, 2024 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants