Skip to content

Commit

Permalink
move CachedKernelMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Sep 19, 2022
1 parent 2db91a1 commit 0fee0f4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 77 deletions.
75 changes: 0 additions & 75 deletions spyder/plugins/ipythonconsole/utils/kernel_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,78 +402,3 @@ def get_fault_text(self):
end_idx = None
text += "\nMain thread:\n" + match.group(0)[:end_idx] + "\n"
return text


class CachedKernelMixin:
"""Cached kernel mixin."""

def __init__(self):
super().__init__()
self._cached_kernel_properties = None

def close_cached_kernel(self):
"""Close the cached kernel."""
if self._cached_kernel_properties is None:
return
kernel = self._cached_kernel_properties[-1]
kernel.close(now=True)
kernel.remove_files()
self._cached_kernel_properties = None

def check_cached_kernel_spec(self, kernel_spec):
"""Test if kernel_spec corresponds to the cached kernel_spec."""
if self._cached_kernel_properties is None:
return False
(
cached_spec,
cached_env,
cached_argv,
_,
) = self._cached_kernel_properties

# Call interrupt_mode so the dict will be the same
kernel_spec.interrupt_mode
cached_spec.interrupt_mode

if "PYTEST_CURRENT_TEST" in cached_env:
# Make tests faster by using cached kernels
# hopefully the kernel will never use PYTEST_CURRENT_TEST
cached_env["PYTEST_CURRENT_TEST"] = (
kernel_spec.env["PYTEST_CURRENT_TEST"])
return (
cached_spec.__dict__ == kernel_spec.__dict__
and kernel_spec.argv == cached_argv
and kernel_spec.env == cached_env
)

def get_cached_kernel(self, kernel_spec, cache=True):
"""Get a new kernel, and cache one for next time."""
# Cache another kernel for next time.
new_kernel_handler = KernelHandler.new_from_spec(kernel_spec)

if not cache:
# remove/don't use cache if requested
self.close_cached_kernel()
return new_kernel_handler

# Check cached kernel has the same configuration as is being asked
cached_kernel_handler = None
if self._cached_kernel_properties is not None:
cached_kernel_handler = self._cached_kernel_properties[-1]
if not self.check_cached_kernel_spec(kernel_spec):
# Close the kernel
self.close_cached_kernel()
cached_kernel_handler = None

# Cache the new kernel
self._cached_kernel_properties = (
kernel_spec,
kernel_spec.env,
kernel_spec.argv,
new_kernel_handler,
)

if cached_kernel_handler is None:
return KernelHandler.new_from_spec(kernel_spec)

return cached_kernel_handler
4 changes: 2 additions & 2 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
from spyder.api.widgets.menus import MENU_SEPARATOR
from spyder.config.base import (
get_home_dir, running_under_pytest)
from spyder.plugins.ipythonconsole.utils.kernel_handler import KernelHandler
from spyder.plugins.ipythonconsole.utils.kernelspec import SpyderKernelSpec
from spyder.plugins.ipythonconsole.utils.style import create_qss_style
from spyder.plugins.ipythonconsole.widgets import (
ClientWidget, ConsoleRestartDialog, COMPLETION_WIDGET_TYPE,
KernelConnectionDialog, PageControlWidget)
from spyder.plugins.ipythonconsole.utils.kernel_handler import (
CachedKernelMixin, KernelHandler)
from spyder.plugins.ipythonconsole.widgets.mixins import CachedKernelMixin
from spyder.py3compat import PY38_OR_MORE
from spyder.utils import encoding, programs, sourcecode
from spyder.utils.misc import get_error_match, remove_backslashes
Expand Down
87 changes: 87 additions & 0 deletions spyder/plugins/ipythonconsole/widgets/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)

"""
IPython Console mixins.
"""

# Local imports
from spyder.plugins.ipythonconsole.utils.kernel_handler import KernelHandler


class CachedKernelMixin:
"""Cached kernel mixin."""

def __init__(self):
super().__init__()
self._cached_kernel_properties = None

def close_cached_kernel(self):
"""Close the cached kernel."""
if self._cached_kernel_properties is None:
return
kernel = self._cached_kernel_properties[-1]
kernel.close(now=True)
kernel.remove_files()
self._cached_kernel_properties = None

def check_cached_kernel_spec(self, kernel_spec):
"""Test if kernel_spec corresponds to the cached kernel_spec."""
if self._cached_kernel_properties is None:
return False
(
cached_spec,
cached_env,
cached_argv,
_,
) = self._cached_kernel_properties

# Call interrupt_mode so the dict will be the same
kernel_spec.interrupt_mode
cached_spec.interrupt_mode

if "PYTEST_CURRENT_TEST" in cached_env:
# Make tests faster by using cached kernels
# hopefully the kernel will never use PYTEST_CURRENT_TEST
cached_env["PYTEST_CURRENT_TEST"] = (
kernel_spec.env["PYTEST_CURRENT_TEST"])
return (
cached_spec.__dict__ == kernel_spec.__dict__
and kernel_spec.argv == cached_argv
and kernel_spec.env == cached_env
)

def get_cached_kernel(self, kernel_spec, cache=True):
"""Get a new kernel, and cache one for next time."""
# Cache another kernel for next time.
new_kernel_handler = KernelHandler.new_from_spec(kernel_spec)

if not cache:
# remove/don't use cache if requested
self.close_cached_kernel()
return new_kernel_handler

# Check cached kernel has the same configuration as is being asked
cached_kernel_handler = None
if self._cached_kernel_properties is not None:
cached_kernel_handler = self._cached_kernel_properties[-1]
if not self.check_cached_kernel_spec(kernel_spec):
# Close the kernel
self.close_cached_kernel()
cached_kernel_handler = None

# Cache the new kernel
self._cached_kernel_properties = (
kernel_spec,
kernel_spec.env,
kernel_spec.argv,
new_kernel_handler,
)

if cached_kernel_handler is None:
return KernelHandler.new_from_spec(kernel_spec)

return cached_kernel_handler

0 comments on commit 0fee0f4

Please sign in to comment.