Skip to content

Commit

Permalink
Fix state leaks in combination of standalone options
Browse files Browse the repository at this point in the history
Follow-up on 936a8f5
  • Loading branch information
kdeldycke committed Feb 21, 2024
1 parent a72cf9c commit c394f2e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
This version is not released yet and is under active development.
```

- Force callback calls in all standalone options before exiting to prevent state leaks in non-`ExtraContext`-based CLIs.
- Run tests on `macos-14`. Remove tests on `macos-12`.

## [4.7.3 (2024-01-06)](https://github.com/kdeldycke/click-extra/compare/v4.7.2...v4.7.3)
Expand Down
6 changes: 6 additions & 0 deletions click_extra/colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ def print_help(ctx: Context, param: Parameter, value: bool) -> None:
"""
if value and not ctx.resilient_parsing:
echo(ctx.get_help(), color=ctx.color)
# XXX We need to explicitly close the context before exiting, so all callbacks
# from options which cleans up CLI state are properly invoked.
# We need to perform this here so combinations of standalone options works
# well, and not only in our custom ExtraContext. This is a follow up on
# 936a8f5eff916bad5dfef1b4696c09b42a23ca61.
ctx.close()
ctx.exit()


Expand Down
6 changes: 6 additions & 0 deletions click_extra/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ def load_conf(self, ctx: Context, param: Parameter, path_pattern: str) -> None:
message = "No configuration file found."
if explicit_conf:
logger.critical(message)
# XXX We need to explicitly close the context before exiting, so all
# callbacks from options which cleans up CLI state are properly
# invoked. We need to perform this here so combinations of standalone
# options works well, and not only in our custom ExtraContext. This is
# a follow up on 936a8f5eff916bad5dfef1b4696c09b42a23ca61.
ctx.close()
ctx.exit(2)
else:
logger.debug(message)
Expand Down
6 changes: 6 additions & 0 deletions click_extra/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,10 @@ def sort_by_depth(line):
)
echo(output, color=ctx.color)

# XXX We need to explicitly close the context before exiting, so all callbacks
# from options which cleans up CLI state are properly invoked.
# We need to perform this here so combinations of standalone options works
# well, and not only in our custom ExtraContext. This is a follow up on
# 936a8f5eff916bad5dfef1b4696c09b42a23ca61.
ctx.close()
ctx.exit()
6 changes: 6 additions & 0 deletions click_extra/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,11 @@ def print_and_exit(

echo(self.render_message(), color=ctx.color)

# XXX We need to explicitly close the context before exiting, so all callbacks
# from options which cleans up CLI state are properly invoked.
# We need to perform this here so combinations of standalone options works
# well, and not only in our custom ExtraContext. This is a follow up on
# 936a8f5eff916bad5dfef1b4696c09b42a23ca61.
ctx.close()
ctx.exit()
return # type: ignore[unreachable]

0 comments on commit c394f2e

Please sign in to comment.