Open yongrenjie opened 3 years ago
from https://stackoverflow.com/a/25558325 (slightly updated for py3), the following allows a chunk of text to be directly executed as if in a REPL:
import sys
import code
infile = open('cmd.py')
def readcmd(prompt):
line = infile.readline()
if not line:
sys.exit(0)
print(prompt, end="")
print(line.rstrip())
return line.rstrip()
code.interact(readfunc=readcmd)
>>>
at the start of every line.
When exec'ing the doctests we would have to strip away the first n characters. Probably this will be set with yet another option.