From def58361ef057f85e7dee4e7217e56d1c57c8e3e 2013-08-13 19:54:11 From: Thomas Kluyver Date: 2013-08-13 19:54:11 Subject: [PATCH] Fix warning when running IPython.kernel tests The signature of TemporaryDirectory.cleanup() changed just before Python 3.2, and our TemporaryWorkingDirectory subclass was receiving an unexpected parameter. This copies in a newer TemporaryDirectory.cleanup() implementation for Python 2.7. --- diff --git a/IPython/utils/tempdir.py b/IPython/utils/tempdir.py index 714c70e..f492935 100644 --- a/IPython/utils/tempdir.py +++ b/IPython/utils/tempdir.py @@ -32,15 +32,30 @@ except ImportError: def __enter__(self): return self.name - def cleanup(self): - if not self._closed: - self._rmtree(self.name) + def cleanup(self, _warn=False): + if self.name and not self._closed: + try: + self._rmtree(self.name) + except (TypeError, AttributeError) as ex: + # Issue #10188: Emit a warning on stderr + # if the directory could not be cleaned + # up due to missing globals + if "None" not in str(ex): + raise + print("ERROR: {!r} while cleaning up {!r}".format(ex, self,), + file=_sys.stderr) + return self._closed = True + if _warn: + self._warn("Implicitly cleaning up {!r}".format(self), + ResourceWarning) def __exit__(self, exc, value, tb): self.cleanup() - __del__ = cleanup + def __del__(self): + # Issue a ResourceWarning if implicit cleanup needed + self.cleanup(_warn=True) # XXX (ncoghlan): The following code attempts to make @@ -124,9 +139,9 @@ class TemporaryWorkingDirectory(TemporaryDirectory): _os.chdir(self.name) - def cleanup(self): + def cleanup(self, _warn=False): #Revert to old cwd. _os.chdir(self.old_wd) #Cleanup - super(TemporaryWorkingDirectory, self).cleanup() + super(TemporaryWorkingDirectory, self).cleanup(_warn=_warn)