MCPcopy Create free account
hub / github.com/ipython/ipython / _strip_prompts

Function _strip_prompts

IPython/core/inputtransformer.py:398–453  ·  view source on GitHub ↗

Remove matching input prompts from a block of input. Parameters ---------- prompt_re : regular expression A regular expression matching any input prompt (including continuation) initial_re : regular expression, optional A regular expression matching only the init

(prompt_re, initial_re=None, turnoff_re=None)

Source from the content-addressed store, hash-verified

396
397
398def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):
399 """Remove matching input prompts from a block of input.
400
401 Parameters
402 ----------
403 prompt_re : regular expression
404 A regular expression matching any input prompt (including continuation)
405 initial_re : regular expression, optional
406 A regular expression matching only the initial prompt, but not continuation.
407 If no initial expression is given, prompt_re will be used everywhere.
408 Used mainly for plain Python prompts, where the continuation prompt
409 ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
410
411 If initial_re and prompt_re differ,
412 only initial_re will be tested against the first line.
413 If any prompt is found on the first two lines,
414 prompts will be stripped from the rest of the block.
415 """
416 if initial_re is None:
417 initial_re = prompt_re
418 line = ''
419 while True:
420 line = (yield line)
421
422 # First line of cell
423 if line is None:
424 continue
425 out, n1 = initial_re.subn('', line, count=1)
426 if turnoff_re and not n1:
427 if turnoff_re.match(line):
428 # We're in e.g. a cell magic; disable this transformer for
429 # the rest of the cell.
430 while line is not None:
431 line = (yield line)
432 continue
433
434 line = (yield out)
435
436 if line is None:
437 continue
438 # check for any prompt on the second line of the cell,
439 # because people often copy from just after the first prompt,
440 # so we might not see it in the first line.
441 out, n2 = prompt_re.subn('', line, count=1)
442 line = (yield out)
443
444 if n1 or n2:
445 # Found a prompt in the first two lines - check for it in
446 # the rest of the cell as well.
447 while line is not None:
448 line = (yield prompt_re.sub('', line, count=1))
449
450 else:
451 # Prompts not in input - wait for reset
452 while line is not None:
453 line = (yield line)
454
455@CoroutineInputTransformer.wrap

Callers 2

classic_promptFunction · 0.85
ipy_promptFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected