Skip to content

Commit

Permalink
Merge branch 'magics_runfile' into improve_namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Mar 31, 2023
2 parents c7bdba2 + 8208bcd commit 701cc7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 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
34 changes: 15 additions & 19 deletions spyder_kernels/customize/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,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 +123,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 @@ -663,11 +663,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 @@ -677,14 +678,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 @@ -698,11 +696,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 701cc7b

Please sign in to comment.