raise ParseFailure("Could not find where header ends")
return edited[last_header_line + len(header_divider):]
+TEMPFILE_PREFIX = 'doctest-interactive'
+def unsafe_get_edited_text(source, editor, encoding='utf-8'):
+ Send `source` (str) to a text editor to be modified by the user. Return
+ the resulting edited string.
+ `editor` is a program that satisfies the requirements of $EDITOR, i.e. one
+ can invoke it as `editor foo` where foo is an absolute file path.
+ `editor` is given in the form of an argv list, e.g. ['vim', '-g'] or
+ ['emacs'] (but NOT 'vim -g' or 'emacs')
+ The exact details of how this works should be that the source is saved to
+ disk, and then the editor invoked on the source, and then the source
+ read back into memory, modified.
+ This is an inherently unsafe operation, as the name of the file on disk can
+ change before the editor is invoked. In addition, to support Windows, the
+ additional burden is to actually close and reopen the file again (creating
+ two race conditions). However, since two copies of the same race condition
+ isn't really worse than one, this model is also used on Unix.
+ with tempfile.NamedTemporaryFile(
+ subprocess.call(editor + [f.name])
+ f = open(f.name, 'r', encoding=encoding)