Python tempfile._get_default_tempdir() Examples
The following are 29
code examples of tempfile._get_default_tempdir().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tempfile
, or try the search function
.
Example #1
Source File: manipulate2.py From gym-malware with MIT License | 6 votes |
def upx_unpack(self, seed=None): # dump bytez to a temporary file tmpfilename = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names())) with open(tmpfilename, 'wb') as outfile: outfile.write(self.bytez) with open(os.devnull, 'w') as DEVNULL: retcode = subprocess.call( ['upx', tmpfilename, '-d', '-o', tmpfilename + '_unpacked'], stdout=DEVNULL, stderr=DEVNULL) os.unlink(tmpfilename) if retcode == 0: # sucessfully unpacked with open(tmpfilename + '_unpacked', 'rb') as result: self.bytez = result.read() os.unlink(tmpfilename + '_unpacked') return self.bytez
Example #2
Source File: test_barracuda_converter.py From RLs with Apache License 2.0 | 6 votes |
def test_barracuda_converter(): path_prefix = os.path.dirname(os.path.abspath(__file__)) tmpfile = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + ".nn" ) # make sure there are no left-over files if os.path.isfile(tmpfile): os.remove(tmpfile) tf2bc.convert(path_prefix + "/BasicLearning.pb", tmpfile) # test if file exists after conversion assert os.path.isfile(tmpfile) # currently converter produces small output file even if input file is empty # 100 bytes is high enough to prove that conversion was successful assert os.path.getsize(tmpfile) > 100 # cleanup os.remove(tmpfile)
Example #3
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 6 votes |
def wfuzz_me_test_generator_previous_session(prev_session_cli, next_session_cli, expected_list): def test(self): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # first session with wfuzz.get_session(prev_session_cli) as s: ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz(save=filename)] # second session wfuzzp as payload with wfuzz.get_session(next_session_cli.replace("$$PREVFILE$$", filename)) as s: ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(expected_list)) return test
Example #4
Source File: test_tempfile.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #5
Source File: manipulate2.py From gym-malware with MIT License | 5 votes |
def upx_pack(self, seed=None): # tested with UPX 3.91 random.seed(seed) tmpfilename = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names())) # dump bytez to a temporary file with open(tmpfilename, 'wb') as outfile: outfile.write(self.bytez) options = ['--force', '--overlay=copy'] compression_level = random.randint(1, 9) options += ['-{}'.format(compression_level)] # --exact # compression levels -1 to -9 # --overlay=copy [default] # optional things: # --compress-exports=0/1 # --compress-icons=0/1/2/3 # --compress-resources=0/1 # --strip-relocs=0/1 options += ['--compress-exports={}'.format(random.randint(0, 1))] options += ['--compress-icons={}'.format(random.randint(0, 3))] options += ['--compress-resources={}'.format(random.randint(0, 1))] options += ['--strip-relocs={}'.format(random.randint(0, 1))] with open(os.devnull, 'w') as DEVNULL: retcode = subprocess.call( ['upx'] + options + [tmpfilename, '-o', tmpfilename + '_packed'], stdout=DEVNULL, stderr=DEVNULL) os.unlink(tmpfilename) if retcode == 0: # successfully packed with open(tmpfilename + '_packed', 'rb') as infile: self.bytez = infile.read() os.unlink(tmpfilename + '_packed') return self.bytez
Example #6
Source File: report.py From elf_diff with GNU General Public License v3.0 | 5 votes |
def generate(self): import codecs if self.settings.html_file: html_file = self.settings.html_file else: html_file = "elf_diff_" + self.getReportBasename() + ".html" print("Writing html file " + html_file) with codecs.open(html_file, "w", "utf-8") as f: self.writeHTML(f) if self.settings.pdf_file: import tempfile tmp_html_file = tempfile._get_default_tempdir() + \ "/" + next(tempfile._get_candidate_names()) + ".html" with codecs.open(tmp_html_file, "w", "utf-8") as f: self.writeHTML(f, skip_details = True) import pdfkit pdfkit.from_url(tmp_html_file, self.settings.pdf_file) import os os.remove(tmp_html_file)
Example #7
Source File: test_tempfile.py From android_universal with MIT License | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory with tempfile.TemporaryDirectory() as our_temp_directory: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError() with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def bad_writer(*args, **kwargs): fp = orig_open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer) as orig_open: # test again with failing write() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), [])
Example #8
Source File: test_tempfile.py From android_universal with MIT License | 5 votes |
def test_wanted_dirs(self): # _candidate_tempdir_list contains the expected directories # Make sure the interesting environment variables are all set. with support.EnvironmentVarGuard() as env: for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: env[envname] = os.path.abspath(envname) cand = tempfile._candidate_tempdir_list() for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: raise ValueError self.assertIn(dirname, cand) try: dirname = os.getcwd() except (AttributeError, OSError): dirname = os.curdir self.assertIn(dirname, cand) # Not practical to try to verify the presence of OS-specific # paths in this list. # We test _get_default_tempdir some more by testing gettempdir.
Example #9
Source File: test_tempfile.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #10
Source File: util.py From picrust2 with GNU General Public License v3.0 | 5 votes |
def generate_temp_filename(temp_dir=None, prefix="", suffix=""): '''Function to generate path to temporary filenames (does not create the) files). The input arguments can be used to customize the temporary filename. If no temporary directory is specified then the default temprary directory will be used.''' # If temp_dir not set then get default directory. if not temp_dir: temp_dir = tempfile._get_default_tempdir() return(join(temp_dir, prefix + next(tempfile._get_candidate_names()) + suffix))
Example #11
Source File: test_tempfile.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory with tempfile.TemporaryDirectory() as our_temp_directory: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError() with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def bad_writer(*args, **kwargs): fp = orig_open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer) as orig_open: # test again with failing write() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), [])
Example #12
Source File: test_tempfile.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_wanted_dirs(self): # _candidate_tempdir_list contains the expected directories # Make sure the interesting environment variables are all set. with support.EnvironmentVarGuard() as env: for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: env[envname] = os.path.abspath(envname) cand = tempfile._candidate_tempdir_list() for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: raise ValueError self.assertIn(dirname, cand) try: dirname = os.getcwd() except (AttributeError, OSError): dirname = os.curdir self.assertIn(dirname, cand) # Not practical to try to verify the presence of OS-specific # paths in this list. # We test _get_default_tempdir some more by testing gettempdir.
Example #13
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #14
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def wfuzz_me_test_generator_recipe(url, payloads, params, expected_list): def test(self): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # Wfuzz results with wfuzz.FuzzSession(url=url, **params) as s: s.export_to_file(filename) if payloads is None: fuzzed = s.fuzz() else: fuzzed = s.get_payloads(payloads).fuzz() ret_list = [(x.code, x.history.urlparse.path) for x in fuzzed] # repeat test with recipe as only parameter with wfuzz.FuzzSession(recipe=[filename]) as s: if payloads is None: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] else: same_list = [(x.code, x.history.urlparse.path) for x in s.get_payloads(payloads).fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) return test
Example #15
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def wfuzz_me_test_generator_saveres(url, payloads, params, expected_list): def test(self): if not expected_list: return temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # Wfuzz results with wfuzz.FuzzSession(url=url, **dict(list(params.items()) + list(dict(save=filename).items()))) as s: if payloads is None: fuzzed = s.fuzz() else: fuzzed = s.get_payloads(payloads).fuzz() ret_list = [(x.code, x.history.urlparse.path) for x in fuzzed] # repeat test with performaing same saved request with wfuzz.FuzzSession(payloads=[("wfuzzp", dict(fn=filename))], url="FUZZ") as s: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) # repeat test with performaing FUZZ[url] saved request with wfuzz.FuzzSession(payloads=[("wfuzzp", dict(fn=filename))], url="FUZZ[url]") as s: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) return test
Example #16
Source File: screenshot.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def process(self, fuzzresult): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name + ".png") subprocess.call(['cutycapt', '--url=%s' % pipes.quote(fuzzresult.url), '--out=%s' % filename]) self.add_result("Screnshot taken, output at %s" % filename)
Example #17
Source File: test_analysis.py From ChainConsumer with MIT License | 5 votes |
def test_file_loading2(self): data = self.data[:1000] directory = tempfile._get_default_tempdir() filename = next(tempfile._get_candidate_names()) filename = directory + os.sep + filename + ".npy" np.save(filename, data) consumer = ChainConsumer() consumer.add_chain(filename) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) assert np.abs(actual[1] - 5.0) < 0.5
Example #18
Source File: test_analysis.py From ChainConsumer with MIT License | 5 votes |
def test_file_loading1(self): data = self.data[:1000] directory = tempfile._get_default_tempdir() filename = next(tempfile._get_candidate_names()) filename = directory + os.sep + filename + ".txt" np.savetxt(filename, data) consumer = ChainConsumer() consumer.add_chain(filename) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) assert np.abs(actual[1] - 5.0) < 0.5
Example #19
Source File: test_tempfile.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory with tempfile.TemporaryDirectory() as our_temp_directory: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError() with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), [])
Example #20
Source File: test_tempfile.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_wanted_dirs(self): # _candidate_tempdir_list contains the expected directories # Make sure the interesting environment variables are all set. with support.EnvironmentVarGuard() as env: for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: env[envname] = os.path.abspath(envname) cand = tempfile._candidate_tempdir_list() for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: raise ValueError self.assertIn(dirname, cand) try: dirname = os.getcwd() except (AttributeError, OSError): dirname = os.curdir self.assertIn(dirname, cand) # Not practical to try to verify the presence of OS-specific # paths in this list. # We test _get_default_tempdir some more by testing gettempdir.
Example #21
Source File: utils.py From lemur with Apache License 2.0 | 5 votes |
def mktemppath(): try: path = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) ) yield path finally: try: os.unlink(path) except OSError as e: current_app.logger.debug("No file {0}".format(path))
Example #22
Source File: test_tempfile.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory with tempfile.TemporaryDirectory() as our_temp_directory: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError() with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(FileNotFoundError): tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), [])
Example #23
Source File: test_tempfile.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_wanted_dirs(self): # _candidate_tempdir_list contains the expected directories # Make sure the interesting environment variables are all set. with support.EnvironmentVarGuard() as env: for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: env[envname] = os.path.abspath(envname) cand = tempfile._candidate_tempdir_list() for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = os.getenv(envname) if not dirname: raise ValueError self.assertIn(dirname, cand) try: dirname = os.getcwd() except (AttributeError, OSError): dirname = os.curdir self.assertIn(dirname, cand) # Not practical to try to verify the presence of OS-specific # paths in this list. # We test _get_default_tempdir some more by testing gettempdir.
Example #24
Source File: utilities.py From DeepFormants with MIT License | 5 votes |
def generate_tmp_filename(extension): return tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + "." + extension
Example #25
Source File: render_point_cloud.py From differentiable-point-clouds with MIT License | 5 votes |
def render_point_cloud(point_cloud, cfg): """ Wraps the call to blender to render the image """ cfg = edict(cfg) temp_dir = tempfile._get_default_tempdir() temp_name = next(tempfile._get_candidate_names()) in_file = f"{temp_dir}/{temp_name}.npz" point_cloud_save = np.reshape(point_cloud, (1, -1, 3)) np.savez(in_file, point_cloud_save) temp_name = next(tempfile._get_candidate_names()) out_file = f"{temp_dir}/{temp_name}.png" args = build_command_line_args([["in_file", in_file], ["out_file", out_file], ["vis_azimuth", cfg.vis_azimuth], ["vis_elevation", cfg.vis_elevation], ["vis_dist", cfg.vis_dist], ["cycles_samples", cfg.render_cycles_samples], ["like_train_data", True], ["voxels", False], ["colored_subsets", False], ["image_size", cfg.render_image_size]], as_string=False) full_args = [blender_exec, "--background", "-P", python_script, "--"] + args subprocess.check_call(full_args, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) image = imageio.imread(out_file) os.remove(in_file) os.remove(out_file) return image
Example #26
Source File: test_tempfile.py From oss-ftp with MIT License | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #27
Source File: test_tempfile.py From BinderFilter with MIT License | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) open = io.open def bad_writer(*args, **kwargs): fp = open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer): # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #28
Source File: test_tempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_no_files_left_behind(self): # use a private empty directory our_temp_directory = tempfile.mkdtemp() try: # force _get_default_tempdir() to consider our empty directory def our_candidate_list(): return [our_temp_directory] with support.swap_attr(tempfile, "_candidate_tempdir_list", our_candidate_list): # verify our directory is empty after _get_default_tempdir() tempfile._get_default_tempdir() self.assertEqual(os.listdir(our_temp_directory), []) def raise_OSError(*args, **kwargs): raise OSError(-1) with support.swap_attr(io, "open", raise_OSError): # test again with failing io.open() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) def bad_writer(*args, **kwargs): fp = orig_open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer) as orig_open: # test again with failing write() with self.assertRaises(IOError) as cm: tempfile._get_default_tempdir() self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.listdir(our_temp_directory), []) finally: shutil.rmtree(our_temp_directory)
Example #29
Source File: utilities.py From RoboGif with Apache License 2.0 | 5 votes |
def get_new_temp_file_path(extension): tmp_dir = tempfile._get_default_tempdir() tmp_name = next(tempfile._get_candidate_names()) tmp_file = os.path.join(tmp_dir, tmp_name + "." + extension) return tmp_file