Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Cordoba <[email protected]>
  • Loading branch information
impact27 and ccordoba12 authored Mar 31, 2023
1 parent 2cb59ce commit 8208bcd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
8 changes: 4 additions & 4 deletions spyder_kernels/console/tests/test_console_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ def test_runfile(tmpdir):
u.write(code)

# Run code file `d` to define `result` even after an error
client.execute_interactive("%runfile {}"
.format(repr(str(d))), timeout=TIMEOUT)
client.execute_interactive(
"%runfile {}".format(repr(str(d))), timeout=TIMEOUT)

# Verify that `result` is defined in the current namespace
client.inspect('result')
Expand All @@ -699,8 +699,8 @@ def test_runfile(tmpdir):
assert content['found']

# Run code file `u` without current namespace
client.execute_interactive("%runfile {}"
.format(repr(str(u))), timeout=TIMEOUT)
client.execute_interactive(
"%runfile {}".format(repr(str(u))), timeout=TIMEOUT)

# Verify that the variable `result2` is defined
client.inspect('result2')
Expand Down
43 changes: 17 additions & 26 deletions spyder_kernels/customize/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
import bdb
import builtins
from contextlib import contextmanager
import cProfile
from functools import partial
import io
import logging
import os
import pdb
import tempfile
import shlex
import sys
import time
Expand All @@ -33,20 +30,18 @@
)
from IPython.core.magic import (
needs_local_scope,
no_var_expand,
line_cell_magic,
magics_class,
Magics,
line_magic,
)
from IPython.core import magic_arguments

# Local imports
from spyder_kernels.comms.frontendcomm import frontend_request, CommError
from spyder_kernels.comms.frontendcomm import frontend_request
from spyder_kernels.customize.namespace_manager import NamespaceManager
from spyder_kernels.customize.spyderpdb import SpyderPdb
from spyder_kernels.customize.umr import UserModuleReloader
from spyder_kernels.customize.utils import capture_last_Expr, canonic, create_pathlist
from spyder_kernels.customize.utils import capture_last_Expr, canonic


# For logging
Expand All @@ -60,41 +55,41 @@ def runfile_arguments(func):
magic_arguments.argument(
"filename",
help="""
filename to run
Filename to run
""",
),
magic_arguments.argument(
"--args",
help="""
command line arguments (string)
Command line arguments (string)
""",
),
magic_arguments.argument(
"--wdir",
const=True,
nargs="?",
help="""
working directory
Working directory
""",
),
magic_arguments.argument(
"--post-mortem",
action="store_true",
help="""
enter post-mortem mode on error
Enter post-mortem mode on errors
""",
),
magic_arguments.argument(
"--current-namespace",
action="store_true",
help="""
use current namespace
Use current namespace
""",
),
magic_arguments.argument(
"--namespace",
help="""
namespace to run the file in
Namespace to run the file in
""",
)
]
Expand Down Expand Up @@ -123,15 +118,15 @@ def runcell_arguments(func):
"filename",
nargs="?",
help="""
filename
Filename
""",
),
magic_arguments.argument(
"--post-mortem",
action="store_true",
default=False,
help="""
Automatically enter post mortem on exception.
Enter post-mortem mode on errors
""",
)
]
Expand Down Expand Up @@ -575,11 +570,12 @@ def _post_mortem_excepthook(self, type, value, tb):

def _parse_argstring(self, magic_func, argstring):
"""
Parse a string of arguments for a magic function
Parse a string of arguments for a magic function.
This is needed because magic_arguments.parse_argstring does
plateform-dependent things with quotes and backslahes
e.g. on windows, string are removed and backslahes are escaped
platform-dependent things with quotes and backslashes. For
example, on Windows, strings are removed and backslashes are
escaped.
"""
argv = shlex.split(argstring)
args = magic_func.parser.parse_args(argv)
Expand All @@ -589,14 +585,11 @@ def _parse_argstring(self, magic_func, argstring):
return args

def _parse_runfile_argstring(self, magic_func, argstring, local_ns):
"""
Parse a string for runfile
"""
"""Parse an args string for runfile and debugfile."""
args = self._parse_argstring(magic_func, argstring)
if args.namespace is None:
args.namespace = self.shell.user_ns
else:

if local_ns is not None and args.namespace in local_ns:
args.namespace = local_ns[args.namespace]
elif args.namespace in self.shell.user_ns:
Expand All @@ -610,11 +603,9 @@ def _parse_runfile_argstring(self, magic_func, argstring, local_ns):
return args, local_ns

def _parse_runcell_argstring(self, magic_func, argstring):
"""
Parse a sting for runcell
"""
"""Parse an args string for runcell and debugcell."""
args = self._parse_argstring(magic_func, argstring)
args.cell_id = args.name
if args.cell_id is None:
args.cell_id = int(args.index)
return args
return args

0 comments on commit 8208bcd

Please sign in to comment.