@contextlib.contextmanager<EOL>def override_temp(replacement):
if not os.path.isdir(replacement):<EOL><INDENT>os.makedirs(replacement)<EOL><DEDENT>saved = tempfile.tempdir<EOL>tempfile.tempdir = replacement<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>tempfile.tempdir = saved<EOL><DEDENT>
Monkey-patch tempfile.tempdir with replacement, ensuring it exists