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

PR: Fix resetting variables after clicking on the reset button (IPython console) #16014

Merged
merged 1 commit into from Jul 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 58 additions & 20 deletions spyder/plugins/ipythonconsole/widgets/shell.py
Expand Up @@ -453,42 +453,69 @@ def _reset_namespace(self):

def reset_namespace(self, warning=False, message=False):
"""Reset the namespace by removing all names defined by the user."""
reset_str = _("Remove all variables")
warn_str = _("All user-defined variables will be removed. "
"Are you sure you want to proceed?")

# Don't show the warning when running our tests.
if running_under_pytest():
warning = False

# This is necessary to make resetting variables work in external
# kernels.
# See spyder-ide/spyder#9505.
try:
kernel_env = self.kernel_manager._kernel_spec.env
except AttributeError:
kernel_env = {}

if warning:
reset_str = _("Remove all variables")
warn_str = _("All user-defined variables will be removed. "
"Are you sure you want to proceed?")
box = MessageCheckBox(icon=QMessageBox.Warning, parent=self)
box.setWindowTitle(reset_str)
box.set_checkbox_text(_("Don't show again."))
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
box.setDefaultButton(QMessageBox.Yes)

box.set_checkbox_text(_("Don't show again."))
box.set_checked(False)
box.set_check_visible(True)
box.setText(warn_str)

answer = box.show()
box.buttonClicked.connect(
lambda button: self.handle_reset_message_answer(
box, button, message)
)
box.show()
else:
self._perform_reset(message)

def handle_reset_message_answer(self, message_box, button, message):
"""
Handle the answer of the reset namespace message box.

Parameters
----------
message_box
Instance of the message box shown to the user.
button: QPushButton
Instance of the button clicked by the user on the dialog.
message: bool
Whether to show a message in the console telling users the
namespace was reset.
"""
if message_box.buttonRole(button) == QMessageBox.YesRole:
self._update_reset_options(message_box)
self._perform_reset(message)
else:
self._update_reset_options(message_box)

# Update checkbox based on user interaction
self.set_conf(
'show_reset_namespace_warning', not box.is_checked())
self.ipyclient.reset_warning = not box.is_checked()
def _perform_reset(self, message):
"""
Perform the reset namespace operation.

if answer != QMessageBox.Yes:
return
Parameters
----------
message: bool
Whether to show a message in the console telling users the
namespace was reset.
"""
# This is necessary to make resetting variables work in external
# kernels.
# See spyder-ide/spyder#9505.
try:
kernel_env = self.kernel_manager._kernel_spec.env
except AttributeError:
kernel_env = {}

try:
if self.is_waiting_pdb_input():
Expand Down Expand Up @@ -526,6 +553,17 @@ def reset_namespace(self, warning=False, message=False):
except AttributeError:
pass

def _update_reset_options(self, message_box):
"""
Update options and variables based on the interaction in the
reset warning message box shown to the user.
"""
self.set_conf(
'show_reset_namespace_warning',
not message_box.is_checked()
)
self.ipyclient.reset_warning = not message_box.is_checked()

def create_shortcuts(self):
"""Create shortcuts for ipyconsole."""
inspect = self.config_shortcut(
Expand Down