##// END OF EJS Templates
Merge pull request #12611 from barhenkro/pathlib_interactiveshell
Matthias Bussonnier -
r26082:7fe090c9 merge
parent child Browse files
Show More
@@ -757,6 +757,7 b' class InteractiveShell(SingletonConfigurable):'
757 self.meta = Struct()
757 self.meta = Struct()
758
758
759 # Temporary files used for various purposes. Deleted at exit.
759 # Temporary files used for various purposes. Deleted at exit.
760 # The files here are stored with Path from Pathlib
760 self.tempfiles = []
761 self.tempfiles = []
761 self.tempdirs = []
762 self.tempdirs = []
762
763
@@ -3595,16 +3596,17 b' class InteractiveShell(SingletonConfigurable):'
3595 - data(None): if data is given, it gets written out to the temp file
3596 - data(None): if data is given, it gets written out to the temp file
3596 immediately, and the file is closed again."""
3597 immediately, and the file is closed again."""
3597
3598
3598 dirname = tempfile.mkdtemp(prefix=prefix)
3599 dir_path = Path(tempfile.mkdtemp(prefix=prefix))
3599 self.tempdirs.append(dirname)
3600 self.tempdirs.append(dir_path)
3600
3601
3601 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3602 handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path))
3602 os.close(handle) # On Windows, there can only be one open handle on a file
3603 os.close(handle) # On Windows, there can only be one open handle on a file
3603 self.tempfiles.append(filename)
3604
3605 file_path = Path(filename)
3606 self.tempfiles.append(file_path)
3604
3607
3605 if data:
3608 if data:
3606 with open(filename, 'w') as tmp_file:
3609 file_path.write_text(data)
3607 tmp_file.write(data)
3608 return filename
3610 return filename
3609
3611
3610 @undoc
3612 @undoc
@@ -3761,14 +3763,14 b' class InteractiveShell(SingletonConfigurable):'
3761 # Cleanup all tempfiles and folders left around
3763 # Cleanup all tempfiles and folders left around
3762 for tfile in self.tempfiles:
3764 for tfile in self.tempfiles:
3763 try:
3765 try:
3764 os.unlink(tfile)
3766 tfile.unlink()
3765 except OSError:
3767 except FileNotFoundError:
3766 pass
3768 pass
3767
3769
3768 for tdir in self.tempdirs:
3770 for tdir in self.tempdirs:
3769 try:
3771 try:
3770 os.rmdir(tdir)
3772 tdir.rmdir()
3771 except OSError:
3773 except FileNotFoundError:
3772 pass
3774 pass
3773
3775
3774 # Clear all user namespaces to release all references cleanly.
3776 # Clear all user namespaces to release all references cleanly.
General Comments 0
You need to be logged in to leave comments. Login now