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)