Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid import of nearby temporary with %edit #4848

Merged
merged 1 commit into from
Jan 30, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if there's anything left in the directory. I think that's fine, because temporary files should be cleaned up anyway, I just wanted to note it.

except OSError:
pass

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

Expand Down