Skip to content

Commit

Permalink
git subrepo clone --branch=profile_script --force https://github.com/…
Browse files Browse the repository at this point in the history
…impact27/spyder-kernels.git external-deps/spyder-kernels

subrepo:
  subdir:   "external-deps/spyder-kernels"
  merged:   "481b13903"
upstream:
  origin:   "https://github.com/impact27/spyder-kernels.git"
  branch:   "profile_script"
  commit:   "481b13903"
git-subrepo:
  version:  "0.4.1"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "a04d8c2"
  • Loading branch information
Quentin Peter committed Nov 4, 2021
1 parent 8fa1c94 commit ae69c07
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
6 changes: 3 additions & 3 deletions external-deps/spyder-kernels/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[subrepo]
remote = https://github.com/spyder-ide/spyder-kernels.git
branch = profile_script
commit = a75f0fa67e4d7c53a49f5530cae2a6847dff955a
parent = a22d73afb4f0983824e1126b13ba3a9849885b29
commit = 481b13903ee50ea12d2f7e3a07f5240476fa221c
parent = 8fa1c945a47a004ca174b74c624159d8d5df89f2
method = merge
cmdver = 0.4.3
cmdver = 0.4.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_free_port():
return port


def frontend_request(blocking=True, timeout=None):
def frontend_request(blocking, timeout=None):
"""
Send a request to the frontend.
Expand Down
31 changes: 21 additions & 10 deletions external-deps/spyder-kernels/spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Standard library imports
from distutils.version import LooseVersion
import logging
import os
import sys
import threading
Expand All @@ -22,9 +23,9 @@
from traitlets.config.loader import LazyConfigValue

# Local imports
from spyder_kernels.py3compat import TEXT_TYPES, to_text_string
from spyder_kernels.comms.frontendcomm import FrontendComm
from spyder_kernels.py3compat import PY3, input
from spyder_kernels.py3compat import (
TEXT_TYPES, to_text_string, PY3, input, TimeoutError)
from spyder_kernels.comms.frontendcomm import FrontendComm, CommError
from spyder_kernels.utils.iofuncs import iofunctions
from spyder_kernels.utils.mpl import (
MPL_BACKENDS_FROM_SPYDER, MPL_BACKENDS_TO_SPYDER, INLINE_FIGURE_FORMATS)
Expand All @@ -34,6 +35,10 @@
if PY3:
import faulthandler


logger = logging.getLogger(__name__)


# Excluded variables from the Variable Explorer (i.e. they are not
# shown at all there)
EXCLUDED_NAMES = ['In', 'Out', 'exit', 'get_ipython', 'quit']
Expand Down Expand Up @@ -88,7 +93,6 @@ def __init__(self, *args, **kwargs):
call_id, handlers[call_id])

self.namespace_view_settings = {}
self._pdb_step = None
self._mpl_backend_error = None
self._running_namespace = None
self._pdb_input_line = None
Expand Down Expand Up @@ -301,13 +305,20 @@ def do_complete(self, code, cursor_pos):
return self.shell.pdb_session.do_complete(code, cursor_pos)
return self._do_complete(code, cursor_pos)

def publish_pdb_state(self):
"""Publish Pdb state."""
if self.shell.pdb_session:
state = dict(namespace_view = self.get_namespace_view(),
var_properties = self.get_var_properties(),
step = self._pdb_step)
def publish_pdb_state(self, step):
"""
Publish Variable Explorer state and Pdb step through
send_spyder_msg.
"""
state = dict(
namespace_view=self.get_namespace_view(),
var_properties=self.get_var_properties(),
step=step
)
try:
self.frontend_call(blocking=False).pdb_state(state)
except (CommError, TimeoutError):
logger.debug("Could not send Pdb state to the frontend.")

def set_spyder_breakpoints(self, breakpoints):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import sys
import time
import warnings
from functools import partial

from IPython import __version__ as ipy_version
from IPython.core.getipython import get_ipython
Expand All @@ -33,6 +34,7 @@
from spyder_kernels.customize.spyderpdb import SpyderPdb, get_new_debugger
from spyder_kernels.customize.umr import UserModuleReloader
from spyder_kernels.py3compat import TimeoutError, PY2, _print, encode
from spyder_kernels.customize.utils import create_pathlist

if not PY2:
from IPython.core.inputtransformer2 import (
Expand Down Expand Up @@ -380,7 +382,7 @@ def post_mortem_excepthook(type, value, tb):
def get_current_file_name():
"""Get the current file name."""
try:
return frontend_request().current_filename()
return frontend_request(blocking=True).current_filename()
except Exception:
_print("This command failed to be executed because an error occurred"
" while trying to get the current file name from Spyder's"
Expand Down Expand Up @@ -426,7 +428,7 @@ def transform_cell(code, indent_only=False):


def exec_code(code, filename, ns_globals, ns_locals=None, post_mortem=False,
profile_filename=None, debugger=None):
exec_fun=None):
"""Execute code and display any exception."""
# Tell IPython to hide this frame (>7.16)
__tracebackhide__ = True
Expand Down Expand Up @@ -467,22 +469,16 @@ def exec_code(code, filename, ns_globals, ns_locals=None, post_mortem=False,
else:
compiled = compile(transform_cell(code), filename, 'exec')

if debugger:
debugger.run(
if exec_fun is None:
exec(
compiled,
globals=ns_globals,
locals=ns_locals
)
elif profile_filename:
# Run with profiler
cProfile.runctx(
compiled,
globals=ns_globals,
locals=ns_locals,
filename=profile_filename
)
ns_globals,
ns_locals)
else:
exec(compiled, ns_globals, ns_locals)
exec_fun(
compiled,
ns_globals,
ns_locals)

except SystemExit as status:
# ignore exit(0)
Expand All @@ -506,7 +502,7 @@ def get_file_code(filename, save_all=True):
"""Retrive the content of a file."""
# Get code from spyder
try:
file_code = frontend_request().get_file_code(
file_code = frontend_request(blocking=True).get_file_code(
filename, save_all=save_all)
except (CommError, TimeoutError, RuntimeError):
file_code = None
Expand Down Expand Up @@ -613,6 +609,14 @@ def runfile(filename=None, args=None, wdir=None, namespace=None,
builtins.spyder_runfile = runfile


def normalise_filename(filename):
"""Normalise path for window."""
# Recursive
if os.name == 'nt':
return filename.replace('\\', '/')
return filename


def debugfile(filename=None, args=None, wdir=None, post_mortem=False,
current_namespace=False):
"""
Expand All @@ -629,15 +633,10 @@ def debugfile(filename=None, args=None, wdir=None, post_mortem=False,
return

shell = get_ipython()
recursive = shell.is_debugging()
if recursive:
if os.name == 'nt':
code_filename = filename.replace('\\', '/')
else:
code_filename = filename

if shell.is_debugging():
# Recursive
code = (
"runfile({}".format(repr(code_filename)) +
"runfile({}".format(repr(normalise_filename(filename))) +
", args=%r, wdir=%r, current_namespace=%r)" % (
args, wdir, current_namespace))

Expand All @@ -651,7 +650,7 @@ def debugfile(filename=None, args=None, wdir=None, post_mortem=False,
filename=debugger.canonic(filename),
args=args, wdir=wdir,
current_namespace=current_namespace,
debugger=debugger,
exec_fun=debugger.run,
stack_depth=1)


Expand All @@ -661,16 +660,16 @@ def debugfile(filename=None, args=None, wdir=None, post_mortem=False,
@contextlib.contextmanager
def profile_tmp_file():
"""Get temporary file for profiling results."""
tempdir = tempfile.TemporaryDirectory()
profile_file = os.path.join(tempdir.name, "profile.results")
try:
yield profile_file
finally:
if os.path.isfile(profile_file):
with open(profile_file, "br") as f:
profile_result = f.read()
frontend_request().show_profile_file(profile_result)
tempdir.cleanup()
with tempfile.TemporaryDirectory() as tempdir:
profile_file = os.path.join(tempdir, "profile.results")
try:
yield profile_file
finally:
if os.path.isfile(profile_file):
with open(profile_file, "br") as f:
profile_result = f.read()
frontend_request(blocking=False).show_profile_file(
profile_result, create_pathlist())


def profile_file(filename=None, args=None, wdir=None, post_mortem=False,
Expand All @@ -682,10 +681,11 @@ def profile_file(filename=None, args=None, wdir=None, post_mortem=False,
post_mortem: boolean, included for compatiblity with runfile
"""
with profile_tmp_file() as tmp_file:
exec_fun = partial(cProfile.runctx, filename=tmp_file)
runfile(
filename=filename, args=args, wdir=wdir,
current_namespace=current_namespace,
profile_filename=tmp_file,
exec_fun=exec_fun,
stack_depth=1)


Expand Down Expand Up @@ -728,7 +728,8 @@ def runcell(cellname, filename=None, post_mortem=False, stack_depth=0,
ipython_shell = get_ipython()
try:
# Get code from spyder
cell_code = frontend_request().run_cell(cellname, filename)
cell_code = frontend_request(
blocking=True).run_cell(cellname, filename)
except Exception:
_print("This command failed to be executed because an error occurred"
" while trying to get the cell code from Spyder's"
Expand Down Expand Up @@ -768,14 +769,10 @@ def debugcell(cellname, filename=None, post_mortem=False):

shell = get_ipython()
if shell.is_debugging():
if os.name == 'nt':
code_filename = filename.replace('\\', '/')
else:
code_filename = filename

# Recursive
code = (
"runcell({}, ".format(repr(cellname)) +
"{})".format(repr(code_filename)))
"{})".format(repr(normalise_filename(filename))))
shell.pdb_session.enter_recursive_debugger(
code, filename, False,
)
Expand All @@ -784,7 +781,7 @@ def debugcell(cellname, filename=None, post_mortem=False):
runcell(
cellname=cellname,
filename=debugger.canonic(filename),
debugger=debugger,
exec_fun=debugger.run,
stack_depth=1)


Expand All @@ -794,10 +791,11 @@ def debugcell(cellname, filename=None, post_mortem=False):
def profile_cell(cellname, filename=None, post_mortem=False):
"""Profile a cell."""
with profile_tmp_file() as tmp_file:
exec_fun = partial(cProfile.runctx, filename=tmp_file)
runcell(
cellname=cellname,
filename=filename,
profile_filename=tmp_file,
exec_fun=exec_fun,
stack_depth=1)


Expand Down Expand Up @@ -828,7 +826,7 @@ def cell_count(filename=None):
raise RuntimeError('Could not get cell count from frontend.')
try:
# Get code from spyder
cell_count = frontend_request().cell_count(filename)
cell_count = frontend_request(blocking=True).cell_count(filename)
return cell_count
except Exception:
etype, error, tb = sys.exc_info()
Expand Down
21 changes: 6 additions & 15 deletions external-deps/spyder-kernels/spyder_kernels/customize/spyderpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def do_where(self, arg):
Take a number as argument as an (optional) number of context line to
print"""
super(SpyderPdb, self).do_where(arg)
frontend_request().do_where()
frontend_request(blocking=False).do_where()

do_w = do_where

Expand Down Expand Up @@ -506,7 +506,7 @@ def is_name_or_composed(text):
def preloop(self):
"""Ask Spyder for breakpoints before the first prompt is created."""
try:
pdb_settings = frontend_request().get_pdb_settings()
pdb_settings = frontend_request(blocking=True).get_pdb_settings()
self.pdb_ignore_lib = pdb_settings['pdb_ignore_lib']
self.pdb_execute_events = pdb_settings['pdb_execute_events']
self.pdb_use_exclamation_mark = pdb_settings[
Expand Down Expand Up @@ -607,7 +607,7 @@ def postcmd(self, stop, line):
"""
Notify spyder on any pdb command.
"""
self.notify_spyder(self.curframe)
self.notify_spyder()
return super(SpyderPdb, self).postcmd(stop, line)

if PY2:
Expand Down Expand Up @@ -704,16 +704,13 @@ def set_spyder_breakpoints(self, breakpoints):
logger.debug(
"Could not send a Pdb continue call to the frontend.")

def notify_spyder(self, frame=None):
def notify_spyder(self):
"""Send kernel state to the frontend."""
if frame is None:
frame = self.curframe

frame = self.curframe
if frame is None:
return

kernel = get_ipython().kernel

# Get filename and line number of the current frame
fname = self.canonic(frame.f_code.co_filename)
if PY2:
Expand All @@ -728,13 +725,7 @@ def notify_spyder(self, frame=None):
if isinstance(fname, basestring) and isinstance(lineno, int):
step = dict(fname=fname, lineno=lineno)

# Publish Pdb state so we can update the Variable Explorer
# and the Editor on the Spyder side
kernel._pdb_step = step
try:
kernel.publish_pdb_state()
except (CommError, TimeoutError):
logger.debug("Could not send Pdb state to the frontend.")
get_ipython().kernel.publish_pdb_state(step)

def run(self, cmd, globals=None, locals=None):
"""Debug a statement executed via the exec() function.
Expand Down

0 comments on commit ae69c07

Please sign in to comment.