-
Notifications
You must be signed in to change notification settings - Fork 3
History memory leak fix #143
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6de1d1c
demo service wip
themoenen 1a6dad6
Merge branch 'main' of github.com:acceleratedscience/openad-toolkit
themoenen 6680dcb
merge
themoenen a7a2b98
Merge branch 'main' of github.com:acceleratedscience/openad-toolkit
themoenen 4234e12
simplified
themoenen 314eb57
Centralized history functions
themoenen 0795085
Stable history
themoenen 2b63278
Stable history
themoenen b7daeb9
Stable history
themoenen 6fec0c4
Cleanup for PR
themoenen c79ace3
fix syntax bug
brian316 a06b798
chore black lint
brian316 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import os | ||
| import readline | ||
| from openad.helpers.output import output_text, output_error, output_success | ||
|
|
||
| DEBUG_HIST = False | ||
|
|
||
|
|
||
| def init_history(cmd_pointer): | ||
| """ | ||
| Load history content from the .cmd_history file: | ||
| - On startup | ||
| - When switching workspaces | ||
| """ | ||
| readline.set_history_length(cmd_pointer.histfile_size) | ||
| if readline and os.path.exists(cmd_pointer.histfile): | ||
| try: | ||
| is_startup = readline.get_current_history_length() == 0 | ||
| if is_startup: | ||
| readline.read_history_file(cmd_pointer.histfile) | ||
| if DEBUG_HIST: | ||
| output_success(f"load_history_file: {cmd_pointer.histfile}", return_val=False) | ||
| except Exception as err: # pylint: disable=broad-exception-caught | ||
| if DEBUG_HIST: | ||
| output_error(["load_history_file", err], return_val=False) | ||
| elif DEBUG_HIST: | ||
| output_error( | ||
| [ | ||
| f"load_history_file - .cmd_history not found in {cmd_pointer.settings['workspace']}", | ||
| cmd_pointer.histfile, | ||
| ], | ||
| return_val=False, | ||
| ) | ||
|
|
||
|
|
||
| def clear_memory_history(cmd_pointer): | ||
| """ | ||
| Clear the in-memory history without removing the history file. | ||
| Used when switching workspaces. | ||
|
|
||
| Workaround needed because readline doesn't let you clear in-memory | ||
| history without also deleting the history file. | ||
| """ | ||
| try: | ||
| readline.write_history_file(cmd_pointer.histfile + "--temp") | ||
| readline.clear_history() | ||
| os.remove(cmd_pointer.histfile + "--temp") | ||
| except Exception as err: # pylint: disable=broad-exception-caught | ||
| if DEBUG_HIST: | ||
| output_error(["refresh_history", err], return_val=False) | ||
|
|
||
|
|
||
| def add_history_entry(cmd_pointer, inp): | ||
| """ | ||
| Add the current command to the in-memory history. | ||
| This is called when a command is executed. | ||
| """ | ||
| try: | ||
| # Ignore super long commands | ||
| if len(str(str(inp).strip())) < int(4096): | ||
| readline.add_history(str(str(inp).strip())) | ||
|
|
||
| # Cap the in-memory history | ||
| # Without this, it will keep on growing until you switch workspaces or restart kernel | ||
| if readline.get_current_history_length() > cmd_pointer.histfile_size: | ||
| readline.remove_history_item(0) | ||
|
|
||
| if DEBUG_HIST: | ||
| output_text(f"add_history_entry #{cmd_pointer.histfile_size}: {inp}", return_val=False) | ||
| except Exception as err: # pylint: disable=broad-exception-caught | ||
| if DEBUG_HIST: | ||
| output_error(["add_history_entry", err], return_val=False) | ||
|
|
||
|
|
||
| def update_history_file(cmd_pointer): | ||
| """ | ||
| Write in-memory history to disk. | ||
| """ | ||
| try: | ||
| readline.write_history_file(cmd_pointer.histfile) | ||
| if DEBUG_HIST: | ||
| output_text(f"update_history_file: {cmd_pointer.histfile}", return_val=False) | ||
| except Exception as err: # pylint: disable=broad-exception-caught | ||
| if DEBUG_HIST: | ||
| output_error(["update_history_file", err], return_val=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@themoenen added this fix. not sure how this got passed by. can you test?