heredoctest.py
29 lines
| 648 B
| text/x-python
|
PythonLexer
/ tests / heredoctest.py
Pulkit Goyal
|
r29485 | from __future__ import absolute_import, print_function | ||
Gregory Szorc
|
r27297 | |||
Matt Mackall
|
r15434 | import sys | ||
Idan Kamara
|
r15247 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r40354 | def flush(): | ||
sys.stdout.flush() | ||||
sys.stderr.flush() | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r15434 | globalvars = {} | ||
lines = sys.stdin.readlines() | ||||
while lines: | ||||
l = lines.pop(0) | ||||
if l.startswith('SALT'): | ||||
Augie Fackler
|
r25032 | print(l[:-1]) | ||
Matt Mackall
|
r15434 | elif l.startswith('>>> '): | ||
snippet = l[4:] | ||||
while lines and lines[0].startswith('... '): | ||||
l = lines.pop(0) | ||||
Yuya Nishihara
|
r22565 | snippet += l[4:] | ||
Matt Mackall
|
r15434 | c = compile(snippet, '<heredoc>', 'single') | ||
try: | ||||
Yuya Nishihara
|
r40354 | flush() | ||
Augie Fackler
|
r25032 | exec(c, globalvars) | ||
Yuya Nishihara
|
r40354 | flush() | ||
Augie Fackler
|
r25032 | except Exception as inst: | ||
Yuya Nishihara
|
r40354 | flush() | ||
Augie Fackler
|
r25032 | print(repr(inst)) | ||