Skip to content

Commit

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

subrepo:
  subdir:   "external-deps/spyder-kernels"
  merged:   "a75f0fa67e"
upstream:
  origin:   "https://github.com/impact27/spyder-kernels.git"
  branch:   "profile_script"
  commit:   "a75f0fa67e"
git-subrepo:
  version:  "0.4.3"
  origin:   "???"
  commit:   "???"
  • Loading branch information
impact27 committed Oct 20, 2021
1 parent a22d73a commit 5a4132d
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 65 deletions.
8 changes: 4 additions & 4 deletions external-deps/spyder-kernels/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
;
[subrepo]
remote = https://github.com/spyder-ide/spyder-kernels.git
branch = 2.x
commit = ac40350c9260e2e5d73f0fc3f8a9cc15da8d8a03
parent = 01421c2ae26c7a3861fc4c912f295f9f2b2c1c1f
branch = profile_script
commit = a75f0fa67e4d7c53a49f5530cae2a6847dff955a
parent = a22d73afb4f0983824e1126b13ba3a9849885b29
method = merge
cmdver = 0.4.1
cmdver = 0.4.3
46 changes: 46 additions & 0 deletions external-deps/spyder-kernels/spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from spyder_kernels.utils.nsview import get_remote_data, make_remote_view
from spyder_kernels.console.shell import SpyderShell

if PY3:
import faulthandler

# Excluded variables from the Variable Explorer (i.e. they are not
# shown at all there)
Expand Down Expand Up @@ -78,6 +80,8 @@ def __init__(self, *args, **kwargs):
'get_matplotlib_backend': self.get_matplotlib_backend,
'pdb_input_reply': self.pdb_input_reply,
'_interrupt_eventloop': self._interrupt_eventloop,
'enable_faulthandler': self.enable_faulthandler,
"flush_std": self.flush_std,
}
for call_id in handlers:
self.frontend_comm.register_call_handler(
Expand All @@ -88,8 +92,14 @@ def __init__(self, *args, **kwargs):
self._mpl_backend_error = None
self._running_namespace = None
self._pdb_input_line = None
self.faulthandler_handle = None

# -- Public API -----------------------------------------------------------
def do_shutdown(self, restart):
"""Disable faulthandler if enabled before proceeding."""
self.disable_faulthandler()
super(SpyderKernel, self).do_shutdown(restart)

def frontend_call(self, blocking=False, broadcast=True,
timeout=None, callback=None):
"""Call the frontend."""
Expand All @@ -105,6 +115,42 @@ def frontend_call(self, blocking=False, broadcast=True,
callback=callback,
timeout=timeout)

def flush_std(self):
"""Flush C standard streams."""
sys.__stderr__.flush()
sys.__stdout__.flush()

def enable_faulthandler(self, fn):
"""
Open a file to save the faulthandling and identifiers for
internal threads.
"""
if not PY3:
# Not implemented
return
self.disable_faulthandler()
f = open(fn, 'w')
self.faulthandler_handle = f
f.write("Main thread id:\n")
f.write(hex(threading.main_thread().ident))
f.write('\nSystem threads ids:\n')
f.write(" ".join([hex(thread.ident) for thread in threading.enumerate()
if thread is not threading.main_thread()]))
f.write('\n')
faulthandler.enable(f)

def disable_faulthandler(self):
"""
Cancel the faulthandling, close the file handle and remove the file.
"""
if not PY3:
# Not implemented
return
if self.faulthandler_handle:
faulthandler.disable()
self.faulthandler_handle.close()
self.faulthandler_handle = None

# --- For the Variable Explorer
def set_namespace_view_settings(self, settings):
"""Set namespace_view_settings."""
Expand Down
3 changes: 3 additions & 0 deletions external-deps/spyder-kernels/spyder_kernels/console/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ def init_pdb(self):

# Set our own magics
kernel.shell.register_magic_function(varexp)
# register profile magic
import spydercustomize
kernel.shell.register_magic_function(spydercustomize.profile)

# Set Pdb class to be used by %debug and %pdb.
# This makes IPython consoles to use the class defined in our
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NamespaceManager(object):
"""

def __init__(self, filename, namespace=None, current_namespace=False,
file_code=None):
file_code=None, stack_depth=1):
self.filename = filename
self.ns_globals = namespace
self.ns_locals = None
Expand All @@ -30,6 +30,9 @@ def __init__(self, filename, namespace=None, current_namespace=False,
self._previous_main = None
self._reset_main = False
self._file_code = file_code
ipython_shell = get_ipython()
self.context_globals = ipython_shell.get_global_scope(stack_depth + 1)
self.context_locals = ipython_shell.get_local_scope(stack_depth + 1)

def __enter__(self):
"""
Expand All @@ -39,10 +42,8 @@ def __enter__(self):
ipython_shell = get_ipython()
if self.ns_globals is None:
if self.current_namespace:
# stack_depth = parent of calling function
stack_depth = 2
self.ns_globals = ipython_shell.get_global_scope(stack_depth)
self.ns_locals = ipython_shell.get_local_scope(stack_depth)
self.ns_globals = self.context_globals
self.ns_locals = self.context_locals
if '__file__' in self.ns_globals:
self._previous_filename = self.ns_globals['__file__']
self.ns_globals['__file__'] = self.filename
Expand Down Expand Up @@ -91,13 +92,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.ns_globals.pop('__file__')

if not self.current_namespace:
# stack_depth = parent of calling function
stack_depth = 2
ns_globals = ipython_shell.get_global_scope(stack_depth)
ns_locals = ipython_shell.get_local_scope(stack_depth)
ns_globals.update(self.ns_globals)
if ns_locals and self.ns_locals:
ns_locals.update(self.ns_locals)
self.context_globals.update(self.ns_globals)
if self.context_locals and self.ns_locals:
self.context_locals.update(self.ns_locals)

if self._previous_main:
sys.modules['__main__'] = self._previous_main
Expand Down
154 changes: 136 additions & 18 deletions external-deps/spyder-kernels/spyder_kernels/customize/spydercustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

import bdb
import cmd
import contextlib
import cProfile
import io
import logging
import os
import pdb
import tempfile
import shlex
import sys
import time
Expand All @@ -27,7 +30,7 @@

from spyder_kernels.comms.frontendcomm import CommError, frontend_request
from spyder_kernels.customize.namespace_manager import NamespaceManager
from spyder_kernels.customize.spyderpdb import SpyderPdb, enter_debugger
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

Expand Down Expand Up @@ -422,7 +425,8 @@ def transform_cell(code, indent_only=False):
return '\n' * number_empty_lines + code


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

if debugger:
debugger.run(
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
)
else:
exec(compiled, ns_globals, ns_locals)

except SystemExit as status:
# ignore exit(0)
if status.code:
Expand Down Expand Up @@ -496,7 +517,8 @@ def get_file_code(filename, save_all=True):


def runfile(filename=None, args=None, wdir=None, namespace=None,
post_mortem=False, current_namespace=False):
post_mortem=False, current_namespace=False, stack_depth=0,
**kwargs):
"""
Run filename
args: command line arguments (string)
Expand Down Expand Up @@ -544,7 +566,8 @@ def runfile(filename=None, args=None, wdir=None, namespace=None,
return

with NamespaceManager(filename, namespace, current_namespace,
file_code=file_code) as (ns_globals, ns_locals):
file_code=file_code, stack_depth=stack_depth + 1
) as (ns_globals, ns_locals):
sys.argv = [filename]
if args is not None:
for arg in shlex.split(args):
Expand Down Expand Up @@ -575,7 +598,8 @@ def runfile(filename=None, args=None, wdir=None, namespace=None,
ipython_shell.run_cell_magic('cython', '', f.read())
else:
exec_code(file_code, filename, ns_globals, ns_locals,
post_mortem=post_mortem)
post_mortem=post_mortem,
**kwargs)

sys.argv = ['']

Expand Down Expand Up @@ -604,17 +628,72 @@ def debugfile(filename=None, args=None, wdir=None, post_mortem=False,
if filename is None:
return

enter_debugger(
filename, True,
"runfile({}" +
", args=%r, wdir=%r, current_namespace=%r)" % (
args, wdir, current_namespace))
shell = get_ipython()
recursive = shell.is_debugging()
if recursive:
if os.name == 'nt':
code_filename = filename.replace('\\', '/')
else:
code_filename = filename

code = (
"runfile({}".format(repr(code_filename)) +
", args=%r, wdir=%r, current_namespace=%r)" % (
args, wdir, current_namespace))

shell.pdb_session.enter_recursive_debugger(
code, filename, True,
)

else:
debugger = get_new_debugger(filename, True)
runfile(
filename=debugger.canonic(filename),
args=args, wdir=wdir,
current_namespace=current_namespace,
debugger=debugger,
stack_depth=1)


builtins.debugfile = debugfile


def runcell(cellname, filename=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()


def profile_file(filename=None, args=None, wdir=None, post_mortem=False,
current_namespace=False):
"""
Profile filename
args: command line arguments (string)
wdir: working directory
post_mortem: boolean, included for compatiblity with runfile
"""
with profile_tmp_file() as tmp_file:
runfile(
filename=filename, args=args, wdir=wdir,
current_namespace=current_namespace,
profile_filename=tmp_file,
stack_depth=1)


builtins.profile_file = profile_file


def runcell(cellname, filename=None, post_mortem=False, stack_depth=0,
**kwargs):
"""
Run a code cell from an editor as a file.
Expand Down Expand Up @@ -669,9 +748,10 @@ def runcell(cellname, filename=None, post_mortem=False):
except Exception:
file_code = None
with NamespaceManager(filename, current_namespace=True,
file_code=file_code) as (ns_globals, ns_locals):
file_code=file_code, stack_depth=stack_depth + 1
) as (ns_globals, ns_locals):
exec_code(cell_code, filename, ns_globals, ns_locals,
post_mortem=post_mortem)
post_mortem=post_mortem, **kwargs)


builtins.runcell = runcell
Expand All @@ -686,15 +766,53 @@ def debugcell(cellname, filename=None, post_mortem=False):
if filename is None:
return

enter_debugger(
filename, False,
"runcell({}, ".format(repr(cellname)) +
"{})")
shell = get_ipython()
if shell.is_debugging():
if os.name == 'nt':
code_filename = filename.replace('\\', '/')
else:
code_filename = filename

code = (
"runcell({}, ".format(repr(cellname)) +
"{})".format(repr(code_filename)))
shell.pdb_session.enter_recursive_debugger(
code, filename, False,
)
else:
debugger = get_new_debugger(filename, False)
runcell(
cellname=cellname,
filename=debugger.canonic(filename),
debugger=debugger,
stack_depth=1)


builtins.debugcell = debugcell


def profile_cell(cellname, filename=None, post_mortem=False):
"""Profile a cell."""
with profile_tmp_file() as tmp_file:
runcell(
cellname=cellname,
filename=filename,
profile_filename=tmp_file,
stack_depth=1)


builtins.profile_cell = profile_cell


def profile(line):
"""Profile the given line."""
with profile_tmp_file() as tmp_file:
cProfile.run(
line,
filename=tmp_file
)


def cell_count(filename=None):
"""
Get the number of cells in a file.
Expand Down
Loading

0 comments on commit 5a4132d

Please sign in to comment.