Skip to content

Commit 62fa204

Browse files
committed
Fix %time argument handling
1 parent 5694174 commit 62fa204

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

IPython/core/completer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,10 +2292,8 @@ def _extract_code(self, line: str) -> str:
22922292
magic_method, "has_arguments", False
22932293
):
22942294
# e.g. %debug, %time
2295-
# try:
2296-
arguments = magic_method.parser.parse_argstring(args)
2297-
if hasattr(arguments, "statement"):
2298-
return " ".join(arguments.statement)
2295+
args, extra = magic_method.parser.parse_argstring(args, partial=True)
2296+
return " ".join(extra)
22992297
except UsageError:
23002298
return line
23012299

IPython/core/magic_arguments.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ def error(self, message):
161161
"""
162162
raise UsageError(message)
163163

164-
def parse_argstring(self, argstring):
164+
def parse_argstring(self, argstring, *, partial=False):
165165
""" Split a string into an argument list and parse that argument list.
166166
"""
167167
argv = arg_split(argstring)
168+
if partial:
169+
return self.parse_known_args(argv)
168170
return self.parse_args(argv)
169171

170172

@@ -190,10 +192,10 @@ def construct_parser(magic_func):
190192
return parser
191193

192194

193-
def parse_argstring(magic_func, argstring):
195+
def parse_argstring(magic_func, argstring, *, partial=False):
194196
""" Parse the string of arguments for the given magic function.
195197
"""
196-
return magic_func.parser.parse_argstring(argstring)
198+
return magic_func.parser.parse_argstring(argstring, partial=partial)
197199

198200

199201
def real_name(magic_func):

IPython/core/magics/execution.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,10 @@ def pdb(self, parameter_s=''):
442442
Set break point at LINE in FILE.
443443
"""
444444
)
445-
@magic_arguments.argument('statement', nargs='*',
446-
help="""
447-
Code to run in debugger.
448-
You can omit this in cell magic mode.
449-
"""
445+
@magic_arguments.kwds(
446+
epilog="""
447+
Any remaining arguments will be treated as code to run in the debugger.
448+
"""
450449
)
451450
@no_var_expand
452451
@line_cell_magic
@@ -455,12 +454,12 @@ def debug(self, line="", cell=None, local_ns=None):
455454
"""Activate the interactive debugger.
456455
457456
This magic command support two ways of activating debugger.
458-
One is to activate debugger before executing code. This way, you
457+
One is to activate debugger before executing code. This way, you
459458
can set a break point, to step through the code from the point.
460459
You can use this mode by giving statements to execute and optionally
461460
a breakpoint.
462461
463-
The other one is to activate debugger in post-mortem mode. You can
462+
The other one is to activate debugger in post-mortem mode. You can
464463
activate this mode simply running %debug without any argument.
465464
If an exception has just occurred, this lets you inspect its stack
466465
frames interactively. Note that this will always work only on the last
@@ -476,9 +475,9 @@ def debug(self, line="", cell=None, local_ns=None):
476475
the magic line is always left unmodified.
477476
478477
"""
479-
args = magic_arguments.parse_argstring(self.debug, line)
478+
args, extra = magic_arguments.parse_argstring(self.debug, line, partial=True)
480479

481-
if not (args.breakpoint or args.statement or cell):
480+
if not (args.breakpoint or extra or cell):
482481
self._debug_post_mortem()
483482
elif not (args.breakpoint or cell):
484483
# If there is no breakpoints, the line is just code to execute
@@ -487,7 +486,7 @@ def debug(self, line="", cell=None, local_ns=None):
487486
# Here we try to reconstruct the code from the output of
488487
# parse_argstring. This might not work if the code has spaces
489488
# For example this fails for `print("a b")`
490-
code = "\n".join(args.statement)
489+
code = " ".join(extra)
491490
if cell:
492491
code += "\n" + cell
493492
self._debug_exec(code, args.breakpoint, local_ns)
@@ -1268,13 +1267,10 @@ def timeit(self, line='', cell=None, local_ns=None):
12681267
dest="no_raise_error",
12691268
help="If given, don't re-raise exceptions",
12701269
)
1271-
@magic_arguments.argument(
1272-
"statement",
1273-
nargs="*",
1274-
help="""
1275-
Code to run.
1276-
You can omit this in cell magic mode.
1277-
""",
1270+
@magic_arguments.kwds(
1271+
epilog="""
1272+
Any remaining arguments will be treated as code to run in the debugger.
1273+
"""
12781274
)
12791275
@skip_doctest
12801276
@needs_local_scope
@@ -1347,8 +1343,8 @@ def time(self, line="", cell=None, local_ns=None):
13471343
Wall time: 0.00 s
13481344
Compiler : 0.78 s
13491345
"""
1350-
args = magic_arguments.parse_argstring(self.time, line)
1351-
line = "\n".join(args.statement)
1346+
args, extra = magic_arguments.parse_argstring(self.time, line, partial=True)
1347+
line = " ".join(extra)
13521348

13531349
if line and cell:
13541350
raise UsageError("Can't use statement directly after '%%time'!")

0 commit comments

Comments
 (0)