Skip to content

Commit

Permalink
avoid import of nearby temporary with %edit
Browse files Browse the repository at this point in the history
use mk**s**temp now,
and create files in subfolders.

should close ipythongh-4731

Note that some part of the logic could now use
http://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile
That will be deleted on close and avoid IPython to track it.
  • Loading branch information
Carreau committed Jan 22, 2014
1 parent 9230c6e commit 09b7763
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ def init_instance_attrs(self):

# Temporary files used for various purposes. Deleted at exit.
self.tempfiles = []
self.tempdirs = []

# Keep track of readline usage (later set by init_readline)
self.has_readline = False
Expand Down Expand Up @@ -3003,15 +3004,19 @@ def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
def mktempfile(self, data=None, prefix='ipython_edit_'):
"""Make a new tempfile and return its filename.
This makes a call to tempfile.mktemp, but it registers the created
filename internally so ipython cleans it up at exit time.
This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
but it registers the created filename internally so ipython cleans it up
at exit time.
Optional inputs:
- data(None): if data is given, it gets written out to the temp file
immediately, and the file is closed again."""

filename = tempfile.mktemp('.py', prefix)
dirname = tempfile.mkdtemp(prefix=prefix)
self.tempdirs.append(dirname)

handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
self.tempfiles.append(filename)

if data:
Expand Down Expand Up @@ -3164,13 +3169,19 @@ def atexit_operations(self):
# history db
self.history_manager.end_session()

# Cleanup all tempfiles left around
# Cleanup all tempfiles and folders left around
for tfile in self.tempfiles:
try:
os.unlink(tfile)
except OSError:
pass

for tdir in self.tempdirs:
try:
os.rmdir(tdir)
except OSError:
pass

# Clear all user namespaces to release all references cleanly.
self.reset(new_session=False)

Expand Down

0 comments on commit 09b7763

Please sign in to comment.