diff --git a/tests/suite/modules/io_related/test_marshal.py b/tests/suite/modules/io_related/test_marshal.py index bdfca3cf7..1e4f7e700 100644 --- a/tests/suite/modules/io_related/test_marshal.py +++ b/tests/suite/modules/io_related/test_marshal.py @@ -108,15 +108,16 @@ def test_file_multiple_reads(self): l.append(marshal.dumps({i:i})) data = b''.join(l) - with open('tempfile.txt', 'wb') as f: + tmpfile = os.path.join(self.temporary_dir, 'tempfile_%d.txt' % os.getpid()) + with open(tmpfile, 'wb') as f: f.write(data) - with open('tempfile.txt', 'rb') as f: + with open(tmpfile, 'rb') as f: for i in range(10): obj = marshal.load(f) self.assertEqual(obj, {i:i}) - self.delete_files('tempfile.txt') + self.delete_files(tmpfile) def test_string_interning(self): self.assertEqual(marshal.dumps(['abc', 'abc'], 0), b'[\x02\x00\x00\x00u\x03\x00\x00\x00abcu\x03\x00\x00\x00abc') diff --git a/tests/suite/modules/misc/test_zlib.py b/tests/suite/modules/misc/test_zlib.py index 49ad941c3..0a063537e 100644 --- a/tests/suite/modules/misc/test_zlib.py +++ b/tests/suite/modules/misc/test_zlib.py @@ -11,11 +11,11 @@ from iptest import IronPythonTestCase, run_test -def create_gzip(text): +def create_gzip(text, path): import gzip - with gzip.open('test_data.gz', 'wb') as f: + with gzip.open(path, 'wb') as f: f.write(text) - with open('test_data.gz', 'rb') as f: + with open(path, 'rb') as f: gzip_compress = f.read() return gzip_compress @@ -48,11 +48,12 @@ def setUp(self): zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS) self.deflate_data = deflate_compress.compress(self.text) + deflate_compress.flush() self.zlib_data = zlib_compress.compress(self.text) + zlib_compress.flush() - self.gzip_data = create_gzip(self.text) + self.gzip_file = os.path.join(self.temporary_dir, "test_data_%d.gz" % os.getpid()) + self.gzip_data = create_gzip(self.text, self.gzip_file) def tearDown(self): super(ZlibTest, self).tearDown() - os.remove("test_data.gz") + os.remove(self.gzip_file) def test_gzip(self): """decompression with gzip header""" diff --git a/tests/suite/modules/system_related/test_nt.py b/tests/suite/modules/system_related/test_nt.py index cfc4f1af9..95d02e284 100644 --- a/tests/suite/modules/system_related/test_nt.py +++ b/tests/suite/modules/system_related/test_nt.py @@ -24,26 +24,29 @@ def test_computername(self): self.assertTrue('COMPUTERNAME' in nt.environ or 'computername' in nt.environ) def test_mkdir(self): - nt.mkdir('dir_create_test') - self.assertEqual(nt.listdir(nt.getcwd()).count('dir_create_test'), 1) + dirname = 'dir_create_test_%d' % os.getpid() + dirpath = os.path.join(self.temporary_dir, dirname) + nt.mkdir(dirpath) + self.assertEqual(nt.listdir(self.temporary_dir).count(dirname), 1) - nt.rmdir('dir_create_test') - self.assertEqual(nt.listdir(nt.getcwd()).count('dir_create_test'), 0) + nt.rmdir(dirpath) + self.assertEqual(nt.listdir(self.temporary_dir).count(dirname), 0) def test_mkdir_negative(self): - nt.mkdir("dir_create_test") + dirpath = os.path.join(self.temporary_dir, 'dir_create_test_%d' % os.getpid()) + nt.mkdir(dirpath) try: - nt.mkdir("dir_create_test") + nt.mkdir(dirpath) self.assertUnreachabale("Cannot create the same directory twice") except WindowsError as e: self.assertEqual(e.errno, 17) #if it fails once...it should fail again - self.assertRaises(WindowsError, nt.mkdir, "dir_create_test") - nt.rmdir('dir_create_test') - nt.mkdir("dir_create_test") - self.assertRaises(WindowsError, nt.mkdir, "dir_create_test") - nt.rmdir('dir_create_test') + self.assertRaises(WindowsError, nt.mkdir, dirpath) + nt.rmdir(dirpath) + nt.mkdir(dirpath) + self.assertRaises(WindowsError, nt.mkdir, dirpath) + nt.rmdir(dirpath) def test_listdir(self): self.assertEqual(nt.listdir(nt.getcwd()), nt.listdir()) @@ -208,7 +211,7 @@ def test_fdopen(self): self.assertRaises(ValueError,os.fdopen,0,"p") stuff = b"\x00a\x01\x02b\x03 \x04 \x05\n\x06_\0xFE\0xFFxyz" - name = "cp5633.txt" + name = os.path.join(self.temporary_dir, "cp5633_%d.txt" % os.getpid()) fd = nt.open(name, nt.O_CREAT | nt.O_BINARY | nt.O_TRUNC | nt.O_WRONLY) f = os.fdopen(fd, 'wb') f.write(stuff) @@ -225,7 +228,7 @@ def test_fstat(self): self.assertTrue(result!=0,"0,The file stat object was not returned correctly") result = None - tmpfile = "tmpfile1.tmp" + tmpfile = os.path.join(self.temporary_dir, "tmpfile1_%d.tmp" % os.getpid()) f = open(tmpfile, "w") result = nt.fstat(f.fileno()) self.assertFalse(result is None,"0,The file stat object was not returned correctly") @@ -244,11 +247,12 @@ def test_fstat(self): def test_chmod(self): # chmod tests: # BUG 828,830 - nt.mkdir('tmp2') - nt.chmod('tmp2', 256) # NOTE: change to flag when stat is implemented - self.assertRaises(OSError, lambda:nt.rmdir('tmp2')) - nt.chmod('tmp2', 128) - nt.rmdir('tmp2') + tmp2 = os.path.join(self.temporary_dir, 'tmp2_%d' % os.getpid()) + nt.mkdir(tmp2) + nt.chmod(tmp2, 256) # NOTE: change to flag when stat is implemented + self.assertRaises(OSError, lambda:nt.rmdir(tmp2)) + nt.chmod(tmp2, 128) + nt.rmdir(tmp2) # /BUG ################################################################################################ @@ -283,62 +287,63 @@ def test_popen(self): dir_pipe.read() dir_pipe.close() - tmpfile = 'tmpfile.tmp' + tmpfile = os.path.join(self.temporary_dir, 'tmpfile_%d.tmp' % os.getpid()) f = open(tmpfile, 'w') f.close() nt.unlink(tmpfile) try: - nt.chmod('tmpfile.tmp', 256) + nt.chmod(tmpfile, 256) except Exception: pass #should throw when trying to access file deleted by unlink else: self.assertTrue(False,"Error! Trying to access file deleted by unlink should have thrown.") try: - tmpfile = "tmpfile2.tmp" - f = open(tmpfile, "w") + tmpfile2 = os.path.join(self.temporary_dir, 'tmpfile2_%d.tmp' % os.getpid()) + f = open(tmpfile2, "w") f.write("testing chmod") f.close() - nt.chmod(tmpfile, 256) - self.assertRaises(OSError, nt.unlink, tmpfile) - nt.chmod(tmpfile, 128) - nt.unlink(tmpfile) - self.assertRaises(IOError, open, tmpfile) + nt.chmod(tmpfile2, 256) + self.assertRaises(OSError, nt.unlink, tmpfile2) + nt.chmod(tmpfile2, 128) + nt.unlink(tmpfile2) + self.assertRaises(IOError, open, tmpfile2) finally: try: - nt.chmod(tmpfile, 128) - nt.unlink(tmpfile) + nt.chmod(tmpfile2, 128) + nt.unlink(tmpfile2) except Exception as e: print("exc", e) # verify that nt.stat reports times in seconds, not ticks... import time - tmpfile = 'tmpfile.tmp' - f = open(tmpfile, 'w') + tmpfile3 = os.path.join(self.temporary_dir, 'tmpfile3_%d.tmp' % os.getpid()) + f = open(tmpfile3, 'w') f.close() t = time.time() - mt = nt.stat(tmpfile).st_mtime - nt.unlink(tmpfile) # this deletes the file + mt = nt.stat(tmpfile3).st_mtime + nt.unlink(tmpfile3) # this deletes the file self.assertTrue(abs(t-mt) < 60, "time differs by too much " + str(abs(t-mt))) - tmpfile = 'tmpfile.tmp' # need to open it again since we deleted it with 'unlink' - f = open(tmpfile, 'w') + tmpfile3 = os.path.join(self.temporary_dir, 'tmpfile3_%d.tmp' % os.getpid()) # need to open it again since we deleted it with 'unlink' + f = open(tmpfile3, 'w') f.close() - nt.chmod('tmpfile.tmp', 256) - nt.chmod('tmpfile.tmp', 128) - nt.unlink('tmpfile.tmp') + nt.chmod(tmpfile3, 256) + nt.chmod(tmpfile3, 128) + nt.unlink(tmpfile3) # utime tests def test_utime(self): - open('temp_file_does_not_exist.txt', 'w').close() + tmpfile = os.path.join(self.temporary_dir, 'temp_utime_%d.txt' % os.getpid()) + open(tmpfile, 'w').close() import nt x = nt.stat('.') - nt.utime('temp_file_does_not_exist.txt', (x[7], x[8])) - y = nt.stat('temp_file_does_not_exist.txt') + nt.utime(tmpfile, (x[7], x[8])) + y = nt.stat(tmpfile) self.assertEqual(x[7], y[7]) self.assertEqual(x[8], y[8]) - nt.unlink('temp_file_does_not_exist.txt') + nt.unlink(tmpfile) # times test def test_times(self): @@ -380,16 +385,16 @@ def test_unsetenv(self): # remove tests def test_remove(self): # remove an existing file - handler = open("create_test_file.txt","w") + tmpfile = os.path.join(self.temporary_dir, 'create_test_file_%d.txt' % os.getpid()) + handler = open(tmpfile, "w") handler.close() - path1 = nt.getcwd() - nt.remove(path1+'\\create_test_file.txt') - self.assertEqual(nt.listdir(nt.getcwd()).count('create_test_file.txt'), 0) + nt.remove(tmpfile) + self.assertFalse(os.path.exists(tmpfile)) - self.assertRaisesNumber(OSError, 2, nt.remove, path1+'\\create_test_file2.txt') - self.assertRaisesNumber(OSError, 2, nt.unlink, path1+'\\create_test_file2.txt') - self.assertRaisesNumber(OSError, 22, nt.remove, path1+'\\create_test_file?.txt') - self.assertRaisesNumber(OSError, 22, nt.unlink, path1+'\\create_test_file?.txt') + self.assertRaisesNumber(OSError, 2, nt.remove, tmpfile) + self.assertRaisesNumber(OSError, 2, nt.unlink, tmpfile) + self.assertRaisesNumber(OSError, 22, nt.remove, os.path.join(self.temporary_dir, 'create_test_file?.txt')) + self.assertRaisesNumber(OSError, 22, nt.unlink, os.path.join(self.temporary_dir, 'create_test_file?.txt')) # the path is a type other than string self.assertRaises(TypeError, nt.remove, 1) @@ -399,55 +404,51 @@ def test_remove(self): def test_remove_negative(self): import stat self.assertRaisesNumber(WindowsError, errno.ENOENT, lambda : nt.remove('some_file_that_does_not_exist')) + tmpfile = os.path.join(self.temporary_dir, 'some_test_file_%d.txt' % os.getpid()) try: - open('some_test_file.txt', 'w').close() - nt.chmod('some_test_file.txt', stat.S_IREAD) - self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) - nt.chmod('some_test_file.txt', stat.S_IWRITE) + open(tmpfile, 'w').close() + nt.chmod(tmpfile, stat.S_IREAD) + self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove(tmpfile)) + nt.chmod(tmpfile, stat.S_IWRITE) - with open('some_test_file.txt', 'w+'): - self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) + with open(tmpfile, 'w+'): + self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove(tmpfile)) finally: - nt.chmod('some_test_file.txt', stat.S_IWRITE) - nt.unlink('some_test_file.txt') + nt.chmod(tmpfile, stat.S_IWRITE) + nt.unlink(tmpfile) # rename tests def test_rename(self): # normal test - handler = open("oldnamefile.txt","w") + oldname = os.path.join(self.temporary_dir, 'oldnamefile_%d.txt' % os.getpid()) + newname = os.path.join(self.temporary_dir, 'newnamefile_%d.txt' % os.getpid()) + + handler = open(oldname, "w") handler.close() - str_old = "oldnamefile.txt" - dst = "newnamefile.txt" - nt.rename(str_old,dst) - self.assertEqual(nt.listdir(nt.getcwd()).count(dst), 1) - self.assertEqual(nt.listdir(nt.getcwd()).count(str_old), 0) - nt.remove(dst) + nt.rename(oldname, newname) + self.assertTrue(os.path.exists(newname)) + self.assertFalse(os.path.exists(oldname)) + nt.remove(newname) # the destination name is a directory - handler = open("oldnamefile.txt","w") + handler = open(oldname, "w") handler.close() - str_old = "oldnamefile.txt" - dst = "newnamefile.txt" - nt.mkdir(dst) - self.assertRaises(OSError, nt.rename,str_old,dst) - nt.rmdir(dst) - nt.remove(str_old) + nt.mkdir(newname) + self.assertRaises(OSError, nt.rename, oldname, newname) + nt.rmdir(newname) + nt.remove(oldname) # the dst already exists - handler1 = open("oldnamefile.txt","w") + handler1 = open(oldname, "w") handler1.close() - handler2 = open("newnamefile.txt","w") + handler2 = open(newname, "w") handler2.close() - str_old = "oldnamefile.txt" - dst = "newnamefile.txt" - self.assertRaises(OSError, nt.rename,str_old,dst) - nt.remove(str_old) - nt.remove(dst) + self.assertRaises(OSError, nt.rename, oldname, newname) + nt.remove(oldname) + nt.remove(newname) # the source file specified does not exist - str_old = "oldnamefile.txt" - dst = "newnamefile.txt" - self.assertRaises(OSError, nt.rename,str_old,dst) + self.assertRaises(OSError, nt.rename, oldname, newname) @unittest.skipUnless(sys.platform == "win32", 'windir is Windows specific') def test_spawnle(self): @@ -743,7 +744,7 @@ def test_urandom(self): # write/read tests def test_write(self): # write the file - tempfilename = "temp.txt" + tempfilename = os.path.join(self.temporary_dir, "temp_%d.txt" % os.getpid()) file = open(tempfilename,"w") nt.write(file.fileno(), b"Hello,here is the value of test string") file.close() @@ -757,7 +758,7 @@ def test_write(self): # BUG 8783 the argument buffersize in nt.read(fd, buffersize) is less than zero # the string written to the file is empty string - tempfilename = "temp.txt" + tempfilename = os.path.join(self.temporary_dir, "temp2_%d.txt" % os.getpid()) file = open(tempfilename,"w") nt.write(file.fileno(), b"bug test") file.close() @@ -768,45 +769,45 @@ def test_write(self): # open test def test_open(self): - open('temp.txt', 'w+').close() + tmpfile = os.path.join(self.temporary_dir, 'temp_open_%d.txt' % os.getpid()) + open(tmpfile, 'w+').close() try: - fd = nt.open('temp.txt', nt.O_WRONLY | nt.O_CREAT) + fd = nt.open(tmpfile, nt.O_WRONLY | nt.O_CREAT) nt.close(fd) - self.assertRaisesNumber(OSError, 17, nt.open, 'temp.txt', nt.O_CREAT | nt.O_EXCL) + self.assertRaisesNumber(OSError, 17, nt.open, tmpfile, nt.O_CREAT | nt.O_EXCL) for flag in [nt.O_EXCL, nt.O_APPEND]: - fd = nt.open('temp.txt', nt.O_RDONLY | flag) + fd = nt.open(tmpfile, nt.O_RDONLY | flag) nt.close(fd) - fd = nt.open('temp.txt', nt.O_WRONLY | flag) + fd = nt.open(tmpfile, nt.O_WRONLY | flag) nt.close(fd) - fd = nt.open('temp.txt', nt.O_RDWR | flag) + fd = nt.open(tmpfile, nt.O_RDWR | flag) nt.close(fd) # sanity test - tempfilename = "temp.txt" - fd = nt.open(tempfilename,256,1) + fd = nt.open(tmpfile,256,1) nt.close(fd) - nt.unlink('temp.txt') + nt.unlink(tmpfile) - f = nt.open('temp.txt', nt.O_TEMPORARY | nt.O_CREAT) + f = nt.open(tmpfile, nt.O_TEMPORARY | nt.O_CREAT) nt.close(f) - self.assertRaises(OSError, nt.stat, 'temp.txt') + self.assertRaises(OSError, nt.stat, tmpfile) # TODO: These tests should probably test more functionality regarding O_SEQUENTIAL/O_RANDOM - f = nt.open('temp.txt', nt.O_TEMPORARY | nt.O_CREAT | nt.O_SEQUENTIAL | nt.O_RDWR) + f = nt.open(tmpfile, nt.O_TEMPORARY | nt.O_CREAT | nt.O_SEQUENTIAL | nt.O_RDWR) nt.close(f) - self.assertRaises(OSError, nt.stat, 'temp.txt') + self.assertRaises(OSError, nt.stat, tmpfile) - f = nt.open('temp.txt', nt.O_TEMPORARY | nt.O_CREAT | nt.O_RANDOM | nt.O_RDWR) + f = nt.open(tmpfile, nt.O_TEMPORARY | nt.O_CREAT | nt.O_RANDOM | nt.O_RDWR) nt.close(f) - self.assertRaises(OSError, nt.stat, 'temp.txt') + self.assertRaises(OSError, nt.stat, tmpfile) finally: try: # should fail if the file doesn't exist - nt.unlink('temp.txt') + nt.unlink(tmpfile) except: pass @@ -841,22 +842,24 @@ def test_flags(self): self.assertEqual(nt.O_TEXT,16384) def test_access(self): - open('new_file_name', 'w').close() + tmpfile = os.path.join(self.temporary_dir, 'new_file_name_%d' % os.getpid()) + open(tmpfile, 'w').close() - self.assertEqual(nt.access('new_file_name', nt.F_OK), True) - self.assertEqual(nt.access('new_file_name', nt.R_OK), True) + self.assertEqual(nt.access(tmpfile, nt.F_OK), True) + self.assertEqual(nt.access(tmpfile, nt.R_OK), True) self.assertEqual(nt.access('does_not_exist.py', nt.F_OK), False) self.assertEqual(nt.access('does_not_exist.py', nt.R_OK), False) - nt.chmod('new_file_name', 0x100) # S_IREAD - self.assertEqual(nt.access('new_file_name', nt.W_OK), False) - nt.chmod('new_file_name', 0x80) # S_IWRITE + nt.chmod(tmpfile, 0x100) # S_IREAD + self.assertEqual(nt.access(tmpfile, nt.W_OK), False) + nt.chmod(tmpfile, 0x80) # S_IWRITE - nt.unlink('new_file_name') + nt.unlink(tmpfile) - nt.mkdir('new_dir_name') - self.assertEqual(nt.access('new_dir_name', nt.R_OK), True) - nt.rmdir('new_dir_name') + tmpdir = os.path.join(self.temporary_dir, 'new_dir_name_%d' % os.getpid()) + nt.mkdir(tmpdir) + self.assertEqual(nt.access(tmpdir, nt.R_OK), True) + nt.rmdir(tmpdir) self.assertRaises(TypeError, nt.access, None, 1) @@ -876,7 +879,7 @@ def test_umask(self): nt.umask(orig) def test_cp16413(self): - tmpfile = 'tmpfile.tmp' + tmpfile = os.path.join(self.temporary_dir, 'tmpfile_%d.tmp' % os.getpid()) f = open(tmpfile, 'w') f.close() nt.chmod(tmpfile, 0o777) @@ -962,7 +965,7 @@ def test_popen_cp34837(self): p.wait() def test_fsync(self): - fsync_file_name = 'text_fsync.txt' + fsync_file_name = os.path.join(self.temporary_dir, 'text_fsync_%d.txt' % os.getpid()) fd = nt.open(fsync_file_name, nt.O_WRONLY | nt.O_CREAT) # negative test, make sure it raises on invalid (closed) fd diff --git a/tests/suite/test_imp.py b/tests/suite/test_imp.py index a42d11505..863ab6f66 100644 --- a/tests/suite/test_imp.py +++ b/tests/suite/test_imp.py @@ -913,33 +913,34 @@ def load_module(self, name): import time def test_file_coding(self): + tmpdir = self.temporary_dir try: import os - with open('test_coding_mod.py', 'wb+') as f: + with open(os.path.join(tmpdir, 'test_coding_mod.py'), 'wb+') as f: f.write(b"# coding: utf-8\nx = '\xc3\xa6ble'\n") - with path_modifier('.'): + with path_modifier(tmpdir): import test_coding_mod self.assertEqual(test_coding_mod.x[0], '\xe6') finally: - os.unlink('test_coding_mod.py') + os.unlink(os.path.join(tmpdir, 'test_coding_mod.py')) try: - with open('test_coding_2.py', 'wb+') as f: + with open(os.path.join(tmpdir, 'test_coding_2.py'), 'wb+') as f: f.write(b"\xef\xbb\xbf# -*- coding: utf-8 -*-\n") f.write(b"x = u'ABCDE'\n") - with path_modifier('.'): + with path_modifier(tmpdir): import test_coding_2 self.assertEqual(test_coding_2.x, 'ABCDE') finally: - os.unlink('test_coding_2.py') + os.unlink(os.path.join(tmpdir, 'test_coding_2.py')) try: - with open('test_coding_3.py', 'wb+') as f: + with open(os.path.join(tmpdir, 'test_coding_3.py'), 'wb+') as f: f.write(b"# -*- coding: utf-8 -*-\n") f.write(b"raise Exception()") f.close() try: - with path_modifier('.'): + with path_modifier(tmpdir): import test_coding_3 except Exception as e: tb = sys.exc_info()[2].tb_next @@ -947,7 +948,7 @@ def test_file_coding(self): while tb.tb_next is not None: tb = tb.tb_next # importlib has a longer traceback self.assertEqual(tb.tb_lineno, 2) finally: - os.unlink('test_coding_3.py') + os.unlink(os.path.join(tmpdir, 'test_coding_3.py')) def test_module_subtype(self): class x(type(sys)): @@ -1225,14 +1226,15 @@ def test_ximp_load_module(self): mod.__file__ = 'does_not_exist.py' sys.modules['my_module_test'] = mod - with open('test.py', 'w+') as f: + tmpfile = os.path.join(self.temporary_dir, 'test_%d.py' % os.getpid()) + with open(tmpfile, 'w+') as f: f.write('x = 42') try: - with open('test.py') as inp_file: + with open(tmpfile) as inp_file: imp.load_module('my_module_test', inp_file, 'does_not_exist.py', ('', 'U', 1)) finally: - os.unlink('test.py') + os.unlink(tmpfile) self.assertEqual(mod.x, 42) diff --git a/tests/suite/test_isinstance.py b/tests/suite/test_isinstance.py index e1796f51f..efc8ec739 100644 --- a/tests/suite/test_isinstance.py +++ b/tests/suite/test_isinstance.py @@ -79,15 +79,17 @@ def max(a,b): def test_redirect(self): """stdin, stdout redirect and input, raw_input tests""" + tmpfile = os.path.join(self.temporary_dir, "testfile_%d.tmp" % os.getpid()) + old_stdin = sys.stdin old_stdout = sys.stdout - sys.stdout = open("testfile.tmp", "w") + sys.stdout = open(tmpfile, "w") print("Into the file") print("2+2") sys.stdout.close() sys.stdout = old_stdout - sys.stdin = open("testfile.tmp", "r") + sys.stdin = open(tmpfile, "r") s = input() self.assertTrue(s == "Into the file") s = eval(input()) @@ -95,8 +97,8 @@ def test_redirect(self): sys.stdin.close() sys.stdin = old_stdin - f = open("testfile.tmp", "r") - g = open("testfile.tmp", "r") + f = open(tmpfile, "r") + g = open(tmpfile, "r") s = f.readline() t = g.readline() self.assertTrue(s == t) @@ -105,16 +107,15 @@ def test_redirect(self): f.close() g.close() - f = open("testfile.tmp", "w") + f = open(tmpfile, "w") f.writelines(["1\n", "2\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "0\n"]) f.close() - f = open("testfile.tmp", "r") + f = open(tmpfile, "r") l = f.readlines() self.assertTrue(l == ["1\n", "2\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "0\n"]) f.close() - import os - os.remove("testfile.tmp") + os.remove(tmpfile) def test_conversions(self): success=False diff --git a/tests/suite/test_python25.py b/tests/suite/test_python25.py index ddffa826a..573d1f305 100644 --- a/tests/suite/test_python25.py +++ b/tests/suite/test_python25.py @@ -282,8 +282,14 @@ def test_thread_lock(self): def test_with_open(self): - with open('abc.txt', 'w'): - pass + import tempfile, os + fd, path = tempfile.mkstemp(suffix='.txt', prefix='ipy_test_') + os.close(fd) + try: + with open(path, 'w'): + pass + finally: + os.remove(path) def test_try_catch_finally(self): # test try-catch-finally syntax