def DeleteSetup(filename): import nt try: nt.remove('scripts\Custom\QuickBattleGame\Missions\\' + filename + '.py') nt.remove('scripts\Custom\QuickBattleGame\Missions\\' + filename + '.pyc') except: print("Cannot remove", filename)
def __test_modified_module(): test_name = "test_modified_module.py" new_stdout_name = "new_stdout.log" #create the file f = open(test_name, "w") f.writelines('''A=1; print "First Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, ["First Run\n"]) AreEqual(new_module.A, 1) #--Do everything again with different values... #recreate the file f = open(test_name, "w") f.writelines('''A=2; print "Second Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, []) AreEqual(new_module.A, 1) #cleanup nt.remove(test_name) nt.remove(new_stdout_name)
def __test_modified_module(): test_name = "test_modified_module.py" new_stdout_name = "new_stdout.log" #create the file f = open(test_name, "w") f.writelines('''A=1; print "First Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, ["First Run\n"]) AreEqual(new_module.A, 1) #--Do everything again with different values... #recreate the file f = open(test_name, "w") f.writelines('''A=2; print "Second Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, []) AreEqual(new_module.A, 1) #cleanup nt.remove(test_name) nt.remove(new_stdout_name)
def server_thread(): global EXIT_CODE global HAS_EXITED import nt serverFile = path_combine(testpath.temporary_dir, "cp7451server.py") write_to_file(serverFile, server) EXIT_CODE = nt.system("%s %s" % (sys.executable, serverFile)) HAS_EXITED = True try: nt.remove(serverFile) except: pass
def ClearCache(): try: files = nt.listdir(nt.getcwd() + "\\cache") except: print "Error reading cache folder" return for i in range(0, len(files)): if files[i].find(".jpeg") > -1: try: nt.remove(nt.getcwd() + "\\cache\\" + files[i]) except: x = None
def ClearCache(): try: files = nt.listdir(nt.getcwd() + "\\cache") except: print "Error reading cache folder" return for i in range(0, len(files)): if files[i].find(".jpeg") > -1: try: nt.remove(nt.getcwd() + "\\cache\\" + files[i]) except: x = None
def server_thread(): global EXIT_CODE global HAS_EXITED import nt serverFile = path_combine(testpath.temporary_dir, "cp7451server.py") write_to_file(serverFile, server) EXIT_CODE = nt.system("%s %s" % (sys.executable, serverFile)) HAS_EXITED = True try: nt.remove(serverFile) except: pass
def test_remove_negative(self): import stat self.assertRaisesNumber(WindowsError, errno.ENOENT, lambda : nt.remove('some_file_that_does_not_exist')) 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) with open('some_test_file.txt', 'w+'): self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) finally: nt.chmod('some_test_file.txt', stat.S_IWRITE) nt.unlink('some_test_file.txt')
def test_remove_negative(): import stat AssertErrorWithNumber(WindowsError, errno.ENOENT, lambda : nt.remove('some_file_that_does_not_exist')) try: file('some_test_file.txt', 'w').close() nt.chmod('some_test_file.txt', stat.S_IREAD) AssertErrorWithNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) nt.chmod('some_test_file.txt', stat.S_IWRITE) f = file('some_test_file.txt', 'w+') AssertErrorWithNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) f.close() finally: nt.chmod('some_test_file.txt', stat.S_IWRITE) nt.unlink('some_test_file.txt')
def test_cp24720(): f = file(nt.getcwd() + "\\site.py", "w") f.write("import sys\nsys.foo = 456\n") f.close() orig_ipy_path = Environment.GetEnvironmentVariable("IRONPYTHONPATH") try: Environment.SetEnvironmentVariable("IRONPYTHONPATH", "") TestCommandLine(("-c", "import site;import sys;print hasattr(sys, 'foo')"), "False\n") Environment.SetEnvironmentVariable("IRONPYTHONPATH", ".") TestCommandLine(("-c", "import site;import sys;print hasattr(sys, 'foo')"), "True\n") finally: Environment.SetEnvironmentVariable("IRONPYTHONPATH", orig_ipy_path) nt.remove(nt.getcwd() + "\\site.py")
def test_remove_negative(): import stat AssertErrorWithNumber(WindowsError, errno.ENOENT, lambda : nt.remove('some_file_that_does_not_exist')) try: file('some_test_file.txt', 'w').close() nt.chmod('some_test_file.txt', stat.S_IREAD) AssertErrorWithNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) nt.chmod('some_test_file.txt', stat.S_IWRITE) f = file('some_test_file.txt', 'w+') AssertErrorWithNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt')) f.close() finally: nt.chmod('some_test_file.txt', stat.S_IWRITE) nt.unlink('some_test_file.txt')
def test_cp24720(): f = file(nt.getcwd() + "\\site.py", "w") f.write("import sys\nsys.foo = 456\n") f.close() orig_ipy_path = Environment.GetEnvironmentVariable("IRONPYTHONPATH") try: Environment.SetEnvironmentVariable("IRONPYTHONPATH", "") TestCommandLine(("-c", "import site;import sys;print hasattr(sys, 'foo')"), "False\n") Environment.SetEnvironmentVariable("IRONPYTHONPATH", ".") TestCommandLine(("-c", "import site;import sys;print hasattr(sys, 'foo')"), "True\n") finally: Environment.SetEnvironmentVariable("IRONPYTHONPATH", orig_ipy_path) nt.remove(nt.getcwd() + "\\site.py")
def test_remove(): # remove an existing file handler = open("create_test_file.txt","w") handler.close() path1 = nt.getcwd() nt.remove(path1+'\\create_test_file.txt') AreEqual(nt.listdir(nt.getcwd()).count('create_test_file.txt'), 0) AssertErrorWithNumber(OSError, 2, nt.remove, path1+'\\create_test_file2.txt') AssertErrorWithNumber(OSError, 2, nt.unlink, path1+'\\create_test_file2.txt') AssertErrorWithNumber(OSError, 22, nt.remove, path1+'\\create_test_file?.txt') AssertErrorWithNumber(OSError, 22, nt.unlink, path1+'\\create_test_file?.txt') # the path is a type other than string AssertError(TypeError, nt.remove, 1) AssertError(TypeError, nt.remove, True) AssertError(TypeError, nt.remove, None)
def Save(pObject, pEvent): CloseDialog(pObject) dict_Ships, dict_Systems = GetShipsAndSystems() sSaveFile = MISSIONS_SAVE_DIR + sCurSaveName + ".py" # delete the file if it does already exist try: nt.remove(sSaveFile) except: pass file = nt.open(sSaveFile, nt.O_CREAT | nt.O_RDWR) sSaveShip = string.replace(repr(dict_Ships), ", {", ",\\\n\t\t{") sSaveShip = string.replace(sSaveShip, "],", "],\\\n\t") nt.write(file, "Systems = " + repr(dict_Systems) + "\n") nt.write(file, "Ships = " + sSaveShip + "\n") nt.close(file)
def WriteSetup(filename): gBridge = g_sBridge gSystem = [] gShips = [] for system in g_pRegionListData: import strop s = strop.split(system, '.') gSystem.append([s[0], s[1], '']) keys = g_pShipListData.keys() for key in keys: gShips.append(g_pShipListData[key]) import nt try: if (filename == "QBSetup"): nt.remove("scripts\Custom\QuickBattleGame\\" + filename + ".py") else: nt.remove("scripts\Custom\QuickBattleGame\Missions\\" + filename + ".py") except OSError: pass import QuickBattle try: if (filename == "QBSetup"): # We have to save the mainsetup in the main directory file = nt.open('scripts\Custom\QuickBattleGame\\' + filename + '.py', nt.O_CREAT | nt.O_RDWR) else: # rest here. file = nt.open('scripts\Custom\QuickBattleGame\Missions\\' + filename + '.py', nt.O_CREAT | nt.O_RDWR) nt.write(file, "gVersion=" + repr(QuickBattle.g_version) + "\n") nt.write(file, "gSystem=" + repr(gSystem) + "\n") nt.write(file, "gBridge=" + repr(gBridge) + "\n") nt.write(file, "gShips=" + repr(gShips) + "\n") import plugin nt.write(file, "gPluginData=" + repr(plugin.gPluginData) + "\n") nt.close(file) except: return 0 return -1
def test_remove(): # remove an existing file handler = open("create_test_file.txt","w") handler.close() path1 = nt.getcwd() nt.remove(path1+'\\create_test_file.txt') AreEqual(nt.listdir(nt.getcwd()).count('create_test_file.txt'), 0) AssertErrorWithNumber(OSError, 2, nt.remove, path1+'\\create_test_file2.txt') AssertErrorWithNumber(OSError, 2, nt.unlink, path1+'\\create_test_file2.txt') AssertErrorWithNumber(OSError, 22, nt.remove, path1+'\\create_test_file?.txt') AssertErrorWithNumber(OSError, 22, nt.unlink, path1+'\\create_test_file?.txt') # the path is a type other than string AssertError(TypeError, nt.remove, 1) AssertError(TypeError, nt.remove, True) AssertError(TypeError, nt.remove, None)
def test_cp5609(): from nt import remove temp_name = "test_cp5609.txt" with open(temp_name, "w") as f: Assert(not f.closed) f.write("xyz") Assert(hasattr(f, "__enter__")) Assert(hasattr(f, "__exit__")) Assert(f.closed) with open(temp_name, "r") as f: Assert(not f.closed) AreEqual(f.readlines(), ["xyz"]) Assert(hasattr(f, "__enter__")) Assert(hasattr(f, "__exit__")) Assert(f.closed) remove(temp_name)
def test_cp5609(): from nt import remove temp_name = "test_cp5609.txt" with open(temp_name, "w") as f: Assert(not f.closed) f.write("xyz") Assert(hasattr(f, "__enter__")) Assert(hasattr(f, "__exit__")) Assert(f.closed) with open(temp_name, "r") as f: Assert(not f.closed) AreEqual(f.readlines(), ["xyz"]) Assert(hasattr(f, "__enter__")) Assert(hasattr(f, "__exit__")) Assert(f.closed) remove(temp_name)
def test_rename(): # normal test handler = open("oldnamefile.txt", "w") handler.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" nt.rename(str_old, dst) AreEqual(nt.listdir(nt.getcwd()).count(dst), 1) AreEqual(nt.listdir(nt.getcwd()).count(str_old), 0) nt.remove(dst) # the destination name is a directory handler = open("oldnamefile.txt", "w") handler.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" nt.mkdir(dst) AssertError(OSError, nt.rename, str_old, dst) nt.rmdir(dst) nt.remove(str_old) # the dst already exists handler1 = open("oldnamefile.txt", "w") handler1.close() handler2 = open("newnamefile.txt", "w") handler2.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" AssertError(OSError, nt.rename, str_old, dst) nt.remove(str_old) nt.remove(dst) # the source file specified does not exist str_old = "oldnamefile.txt" dst = "newnamefile.txt" AssertError(OSError, nt.rename, str_old, dst)
def test_rename(): # normal test handler = open("oldnamefile.txt","w") handler.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" nt.rename(str_old,dst) AreEqual(nt.listdir(nt.getcwd()).count(dst), 1) AreEqual(nt.listdir(nt.getcwd()).count(str_old), 0) nt.remove(dst) # the destination name is a directory handler = open("oldnamefile.txt","w") handler.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" nt.mkdir(dst) AssertError(OSError, nt.rename,str_old,dst) nt.rmdir(dst) nt.remove(str_old) # the dst already exists handler1 = open("oldnamefile.txt","w") handler1.close() handler2 = open("newnamefile.txt","w") handler2.close() str_old = "oldnamefile.txt" dst = "newnamefile.txt" AssertError(OSError, nt.rename,str_old,dst) nt.remove(str_old) nt.remove(dst) # the source file specified does not exist str_old = "oldnamefile.txt" dst = "newnamefile.txt" AssertError(OSError, nt.rename,str_old,dst)
def commandquit(): remove(appdatfile) exit(msgexitu)
def simpleTester(a, b, c): global SIMPLE_TEST global SIMPLE_TEST_COUNT import nt test_name = "clrusetest" + str(SIMPLE_TEST_COUNT) + ".py" SIMPLE_TEST_COUNT = SIMPLE_TEST_COUNT + 1 new_stdout_name = "new_stdout.log" expected_stdout = '''OK...\n''' #create the file test_text = SIMPLE_TEST % (str(a), str(c)) f = open(test_name, "w") f.writelines(test_text) f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use name = test_name.split(".py")[0] new_module = clr.Use(name) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run a few easy checks AreEqual(len(new_stdout_lines), 3) AreEqual(new_stdout_lines[0], expected_stdout) AreEqual(new_stdout_lines[2], "b= 42\n") AreEqual(new_module.A, a) AreEqual(new_module.aFunc(None), c) Assert(isinstance(new_module.K, new_module.Klass)) AreEqual(new_module.K.Member, 72) new_module.K.Member = "foo" AreEqual(new_module.K.Member, "foo") new_module.K.NewMember = 33 AreEqual(new_module.K.NewMember, 33) new_module.K = None AreEqual(new_module.K, None) #negative checks AssertError(TypeError, new_module.aFunc) AssertError(TypeError, new_module.aFunc, 1, 2) AssertError(TypeError, new_module.aFunc, 1, 2, 3) Assert(not hasattr(new_module, "a")) Assert(not hasattr(new_module, "simpleTester")) try: aFunc(7) raise "Should never get this far" except: pass #hard test real_module = __import__(test_name.split(".py")[0]) #for key in dir(real_module): AreEqual(real_module.__dict__[key], new_module.__dict__[key]) #cleanup nt.remove(test_name) nt.remove(new_stdout_name)
def test_fdopen(): fd_lambda = lambda x: nt.dup(x) # fd = 0 result = None result = nt.fdopen(fd_lambda(0), "r", 1024) Assert(result != None, "1,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0), "w", 2048) Assert(result != None, "2,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0), "a", 512) Assert(result != None, "3,The file object was not returned correctly") # fd = 1 result = None result = nt.fdopen(fd_lambda(1), "a", 1024) Assert(result != None, "4,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1), "r", 2048) Assert(result != None, "5,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1), "w", 512) Assert(result != None, "6,The file object was not returned correctly") # fd = 2 result = None result = nt.fdopen(fd_lambda(2), "r", 1024) Assert(result != None, "7,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2), "a", 2048) Assert(result != None, "8,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2), "w", 512) Assert(result != None, "9,The file object was not returned correctly") if not is_cli: result.close() # The file descriptor is not valid AssertError(OSError, nt.fdopen, 3000) AssertError(OSError, nt.fdopen, -1) AssertError(OSError, nt.fdopen, 3000, "w") AssertError(OSError, nt.fdopen, 3000, "w", 1024) # The file mode does not exist AssertError(ValueError, nt.fdopen, 0, "p") stuff = "\x00a\x01\x02b\x03 \x04 \x05\n\x06_\0xFE\0xFFxyz" name = "cp5633.txt" fd = nt.open(name, nt.O_CREAT | nt.O_BINARY | nt.O_TRUNC | nt.O_WRONLY) f = nt.fdopen(fd, 'wb') f.write(stuff) f.close() f = file(name, 'rb') try: AreEqual(stuff, f.read()) finally: f.close() nt.remove(name)
def test_fdopen(): # IronPython does not implement the nt.dup function if not is_cli: fd_lambda = lambda x: nt.dup(x) else: AssertError(AttributeError, lambda: nt.dup) fd_lambda = lambda x: x # fd = 0 result = None result = nt.fdopen(fd_lambda(0),"r",1024) Assert(result!=None,"1,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0),"w",2048) Assert(result!=None,"2,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0),"a",512) Assert(result!=None,"3,The file object was not returned correctly") # fd = 1 result = None result = nt.fdopen(fd_lambda(1),"a",1024) Assert(result!=None,"4,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1),"r",2048) Assert(result!=None,"5,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1),"w",512) Assert(result!=None,"6,The file object was not returned correctly") # fd = 2 result = None result = nt.fdopen(fd_lambda(2),"r",1024) Assert(result!=None,"7,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2),"a",2048) Assert(result!=None,"8,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2),"w",512) Assert(result!=None,"9,The file object was not returned correctly") if not is_cli: result.close() # The file descriptor is not valid AssertError(OSError,nt.fdopen,3000) AssertError(OSError,nt.fdopen,-1) AssertError(OSError,nt.fdopen,3000, "w") AssertError(OSError,nt.fdopen,3000, "w", 1024) # The file mode does not exist AssertError(ValueError,nt.fdopen,0,"p") stuff = "\x00a\x01\x02b\x03 \x04 \x05\n\x06_\0xFE\0xFFxyz" name = "cp5633.txt" fd = nt.open(name, nt.O_CREAT | nt.O_BINARY | nt.O_TRUNC | nt.O_WRONLY) f = nt.fdopen(fd, 'wb') f.write(stuff) f.close() f = file(name, 'rb') try: AreEqual(stuff, f.read()) finally: f.close() nt.remove(name)
def test_fdopen(self): fd_lambda = lambda x: nt.dup(x) # fd = 0 result = None result = nt.fdopen(fd_lambda(0),"r",1024) self.assertTrue(result!=None,"1,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0),"w",2048) self.assertTrue(result!=None,"2,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(0),"a",512) self.assertTrue(result!=None,"3,The file object was not returned correctly") # fd = 1 result = None result = nt.fdopen(fd_lambda(1),"a",1024) self.assertTrue(result!=None,"4,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1),"r",2048) self.assertTrue(result!=None,"5,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(1),"w",512) self.assertTrue(result!=None,"6,The file object was not returned correctly") # fd = 2 result = None result = nt.fdopen(fd_lambda(2),"r",1024) self.assertTrue(result!=None,"7,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2),"a",2048) self.assertTrue(result!=None,"8,The file object was not returned correctly") result = None result = nt.fdopen(fd_lambda(2),"w",512) self.assertTrue(result!=None,"9,The file object was not returned correctly") if not is_cli: result.close() # The file descriptor is not valid self.assertRaises(OSError,nt.fdopen,3000) self.assertRaises(OSError,nt.fdopen,-1) self.assertRaises(OSError,nt.fdopen,3000, "w") self.assertRaises(OSError,nt.fdopen,3000, "w", 1024) # The file mode does not exist self.assertRaises(ValueError,nt.fdopen,0,"p") stuff = "\x00a\x01\x02b\x03 \x04 \x05\n\x06_\0xFE\0xFFxyz" name = "cp5633.txt" fd = nt.open(name, nt.O_CREAT | nt.O_BINARY | nt.O_TRUNC | nt.O_WRONLY) f = nt.fdopen(fd, 'wb') f.write(stuff) f.close() f = file(name, 'rb') try: self.assertEqual(stuff, f.read()) finally: f.close() nt.remove(name)
def delete_files(*files): for f in files: try: nt.remove(f) except: pass
def delete_files(files): for f in files: nt.remove(f)
def delete_files(*files): for f in files: try: nt.remove(f) except: pass
def simpleTester(a, b, c): global SIMPLE_TEST global SIMPLE_TEST_COUNT import nt test_name = "clrusetest" + str(SIMPLE_TEST_COUNT) + ".py" SIMPLE_TEST_COUNT = SIMPLE_TEST_COUNT + 1 new_stdout_name = "new_stdout.log" expected_stdout = '''OK...\n''' #create the file test_text = SIMPLE_TEST % (str(a), str(c)) f = open(test_name, "w") f.writelines(test_text) f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use name = test_name.split(".py")[0] new_module = clr.Use(name) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run a few easy checks AreEqual(len(new_stdout_lines), 3) AreEqual(new_stdout_lines[0], expected_stdout) AreEqual(new_stdout_lines[2], "b= 42\n") AreEqual(new_module.A, a) AreEqual(new_module.aFunc(None), c) Assert(isinstance(new_module.K, new_module.Klass)) AreEqual(new_module.K.Member, 72) new_module.K.Member = "foo" AreEqual(new_module.K.Member, "foo") new_module.K.NewMember = 33 AreEqual(new_module.K.NewMember, 33) new_module.K = None AreEqual(new_module.K, None) #negative checks AssertError(TypeError, new_module.aFunc) AssertError(TypeError, new_module.aFunc, 1, 2) AssertError(TypeError, new_module.aFunc, 1, 2, 3) Assert(not hasattr(new_module, "a")) Assert(not hasattr(new_module, "simpleTester")) try: aFunc(7) raise "Should never get this far" except: pass #hard test real_module = __import__(test_name.split(".py")[0]) #for key in dir(real_module): AreEqual(real_module.__dict__[key], new_module.__dict__[key]) #cleanup nt.remove(test_name) nt.remove(new_stdout_name)
def test_fdopen(self): fd_lambda = lambda x: nt.dup(x) # fd = 0 result = None result = os.fdopen(fd_lambda(0),"r",1024) self.assertFalse(result is None,"1,The file object was not returned correctly") result = None result = os.fdopen(fd_lambda(0),"w",2048) self.assertFalse(result is None,"2,The file object was not returned correctly") with self.assertRaises(OSError): os.fdopen(fd_lambda(0),"a",512) # fd = 1 with self.assertRaises(OSError): os.fdopen(fd_lambda(1),"a",1024) result = None result = os.fdopen(fd_lambda(1),"r",2048) self.assertFalse(result is None,"5,The file object was not returned correctly") result = None result = os.fdopen(fd_lambda(1),"w",512) self.assertFalse(result is None,"6,The file object was not returned correctly") # fd = 2 result = None result = os.fdopen(fd_lambda(2),"r",1024) self.assertFalse(result is None,"7,The file object was not returned correctly") with self.assertRaises(OSError): os.fdopen(fd_lambda(2),"a",2048) result = None result = os.fdopen(fd_lambda(2),"w",512) self.assertFalse(result is None,"9,The file object was not returned correctly") if not is_cli: result.close() # The file descriptor is not valid self.assertRaises(OSError,os.fdopen,3000) self.assertRaises(ValueError,os.fdopen,-1) self.assertRaises(OSError,os.fdopen,3000, "w") self.assertRaises(OSError,os.fdopen,3000, "w", 1024) # The file mode does not exist self.assertRaises(ValueError,os.fdopen,0,"p") stuff = b"\x00a\x01\x02b\x03 \x04 \x05\n\x06_\0xFE\0xFFxyz" name = "cp5633.txt" fd = nt.open(name, nt.O_CREAT | nt.O_BINARY | nt.O_TRUNC | nt.O_WRONLY) f = os.fdopen(fd, 'wb') f.write(stuff) f.close() try: with open(name, 'rb') as f: self.assertEqual(stuff, f.read()) finally: nt.remove(name)