Ticket #3123: filepath.open.flags.patch

File filepath.open.flags.patch, 2.0 KB (added by cyli, 7 years ago)
  • python/filepath.py

     
    362362    def siblingExtension(self, ext): 
    363363        return self.clonePath(self.path+ext) 
    364364 
    365     def open(self, mode='r'): 
    366         if self.alwaysCreate: 
    367             assert 'a' not in mode, "Appending not supported when alwaysCreate == True" 
    368             return self.create() 
    369         return open(self.path, mode+'b') 
     365    def open(self, mode='r', flags=None): 
     366        """ 
     367        Opens self with given mode.  If option flags is provided (such as 
     368        os.O_RDONLY, os.O_CREAT, etc or-ed together - see os module  
     369        documentation), ignores mode completely.  
    370370 
     371        @returns: file handle to self 
     372        """ 
     373        if flags is None: 
     374            if self.alwaysCreate: 
     375                assert 'a' not in mode, "Appending not supported when alwaysCreate == True" 
     376                return self.create() 
     377            return open(self.path, mode+'b') 
     378        else: 
     379            return os.open(self.path, flags) 
     380 
    371381    # stat methods below 
    372382 
    373383    def restat(self, reraise=True): 
  • test/test_paths.py

     
    543543        del nonexistent 
    544544        self.assertRaises((OSError, IOError), existent.open) 
    545545 
     546    def testOpenWithFlags(self): 
     547        """ 
     548        Verify that opening with flags works - no need to test flags, because 
     549        they are just passed straight to os.open 
     550        """ 
     551        # Opening a file for reading when it does not already exist is an error 
     552        create = self.path.child('createdFile') 
     553        f = create.open(flags=(os.O_RDWR | os.O_CREAT )) 
     554        self.failUnless(create.exists()) 
     555        self.assertRaises(OSError, create.open,  
     556                          flags=(os.O_RDWR | os.O_CREAT | os.O_EXCL)) 
    546557 
    547558    def test_existsCache(self): 
    548559        """