##// END OF EJS Templates
Do not use NamedTemporaryFile in handle_image_tempfile...
Takafumi Arakaki -
Show More
@@ -0,0 +1,20 b''
1 #-----------------------------------------------------------------------------
2 # Copyright (C) 2012- The IPython Development Team
3 #
4 # Distributed under the terms of the BSD License. The full license is in
5 # the file COPYING, distributed as part of this software.
6 #-----------------------------------------------------------------------------
7
8 import os
9
10 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
11
12
13 def test_named_file_in_temporary_directory():
14 with NamedFileInTemporaryDirectory('filename') as file:
15 name = file.name
16 assert not file.closed
17 assert os.path.exists(name)
18 file.write('test')
19 assert file.closed
20 assert not os.path.exists(name)
@@ -22,7 +22,6 b' import signal'
22 import os
22 import os
23 import sys
23 import sys
24 import time
24 import time
25 import tempfile
26 import subprocess
25 import subprocess
27 from io import BytesIO
26 from io import BytesIO
28 import base64
27 import base64
@@ -39,6 +38,7 b' from IPython.core import page'
39 from IPython.utils.warn import warn, error, fatal
38 from IPython.utils.warn import warn, error, fatal
40 from IPython.utils import io
39 from IPython.utils import io
41 from IPython.utils.traitlets import List, Enum, Any
40 from IPython.utils.traitlets import List, Enum, Any
41 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
42
42
43 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
43 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
44 from IPython.frontend.terminal.console.completer import ZMQCompleter
44 from IPython.frontend.terminal.console.completer import ZMQCompleter
@@ -285,7 +285,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
285 raw = base64.decodestring(data[mime])
285 raw = base64.decodestring(data[mime])
286 imageformat = self._imagemime[mime]
286 imageformat = self._imagemime[mime]
287 ext = '.{0}'.format(imageformat)
287 ext = '.{0}'.format(imageformat)
288 with nested(tempfile.NamedTemporaryFile(suffix=ext),
288 with nested(NamedFileInTemporaryDirectory('tmp.{0}'.format(ext)),
289 open(os.devnull, 'w')) as (f, devnull):
289 open(os.devnull, 'w')) as (f, devnull):
290 f.write(raw)
290 f.write(raw)
291 f.flush()
291 f.flush()
@@ -3,14 +3,14 b''
3 This is copied from the stdlib and will be standard in Python 3.2 and onwards.
3 This is copied from the stdlib and will be standard in Python 3.2 and onwards.
4 """
4 """
5
5
6 import os as _os
7
6 # This code should only be used in Python versions < 3.2, since after that we
8 # This code should only be used in Python versions < 3.2, since after that we
7 # can rely on the stdlib itself.
9 # can rely on the stdlib itself.
8 try:
10 try:
9 from tempfile import TemporaryDirectory
11 from tempfile import TemporaryDirectory
10
12
11 except ImportError:
13 except ImportError:
12
13 import os as _os
14 from tempfile import mkdtemp, template
14 from tempfile import mkdtemp, template
15
15
16 class TemporaryDirectory(object):
16 class TemporaryDirectory(object):
@@ -74,3 +74,33 b' except ImportError:'
74 self._rmdir(path)
74 self._rmdir(path)
75 except self._os_error:
75 except self._os_error:
76 pass
76 pass
77
78
79 class NamedFileInTemporaryDirectory(object):
80
81 def __init__(self, filename, mode='w+b', bufsize=-1, **kwds):
82 """
83 Open a file named `filename` in a temporary directory.
84
85 This context manager is preferred over `NamedTemporaryFile` in
86 stdlib `tempfile` when one needs to reopen the file.
87
88 Arguments `mode` and `bufsize` are passed to `open`.
89 Rest of the arguments are passed to `TemporaryDirectory`.
90
91 """
92 self._tmpdir = TemporaryDirectory(**kwds)
93 path = _os.path.join(self._tmpdir.name, filename)
94 self.file = open(path, mode, bufsize)
95
96 def cleanup(self):
97 self.file.close()
98 self._tmpdir.cleanup()
99
100 __del__ = cleanup
101
102 def __enter__(self):
103 return self.file
104
105 def __exit__(self, type, value, traceback):
106 self.cleanup()
General Comments 0
You need to be logged in to leave comments. Login now