示例#1
0
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)
示例#2
0
    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)
示例#3
0
文件: nt_test.py 项目: 89sos98/main
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)
示例#4
0
 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)