diff options
author | Julien Cristau <julien.cristau@logilab.fr> | 2013-02-22 09:46:41 +0100 |
---|---|---|
committer | Julien Cristau <julien.cristau@logilab.fr> | 2013-02-22 09:46:41 +0100 |
commit | f3fbd3b38a8624c83e6f13616947c2775f194cac (patch) | |
tree | a16c08a331cc065931d6715ea6a60aafc58d14dd /testlib.py | |
parent | 6d1c386f313a6c88d3ea4e5ba612266dbac2c362 (diff) | |
download | logilab-common-f3fbd3b38a8624c83e6f13616947c2775f194cac.tar.gz |
[testlib] check for generators in with_tempdir
If the decorated callable is a generator, we shouldn't reset tempfile.tempdir
until the end of the iteration. Hopefully nobody uses this with nested/chained
generators, or they'd stomp all over each other's tempdirs.
Closes #117533
Diffstat (limited to 'testlib.py')
-rw-r--r-- | testlib.py | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -124,6 +124,21 @@ __unittest = 1 def with_tempdir(callable): """A decorator ensuring no temporary file left when the function return Work only for temporary file create with the tempfile module""" + if is_generator(callable): + def proxy(*args, **kwargs): + old_tmpdir = tempfile.gettempdir() + new_tmpdir = tempfile.mkdtemp(prefix="temp-lgc-") + tempfile.tempdir = new_tmpdir + try: + for x in callable(*args, **kwargs): + yield x + finally: + try: + rmtree(new_tmpdir, ignore_errors=True) + finally: + tempfile.tempdir = old_tmpdir + return proxy + @wraps(callable) def proxy(*args, **kargs): |