Next / Previous / Contents / Shipman's homepage

6.10. PathInfo.ownerCanRead(), etc: Predicates for permission testing

All nine of the routines that test read, write, and execute permissions are structurally identical. In each case, one of the mode bits is 1 if the path has that permission, or 0 otherwise.

To test this bit while ignoring the other bits, we use a mask that has a 1 bit in that position and 0 bits elsewhere. Applying a Boolean “and” operation on the mask and the mode bits gives us a word that is all zeroes (false) if the relevant bit is 0, or a word that is not zero (true) if the relevant bit is not zero.

The first method illustrates the pattern. The mask for the owner read permission bit comes from the stat module, and is called S_IRUSR.
# - - -  P a t h I n f o . { o w n e r } C a n { R e a d   } - - -
#                          { g r o u p }       { W r i t e }
#                          { w o r l d }       { E x e c   }

    def ownerCanRead ( self ):
        return self.mode & stat.S_IRUSR

The remaining routines are identical except for the names of the masks they use from the stat module.
    def ownerCanWrite ( self ):
        return self.mode & stat.S_IWUSR

    def ownerCanExec ( self ):
        return self.mode & stat.S_IXUSR

    def groupCanRead ( self ):
        return self.mode & stat.S_IRGRP

    def groupCanWrite ( self ):
        return self.mode & stat.S_IWGRP

    def groupCanExec ( self ):
        return self.mode & stat.S_IXGRP

    def worldCanRead ( self ):
        return self.mode & stat.S_IROTH

    def worldCanWrite ( self ):
        return self.mode & stat.S_IWOTH

    def worldCanExec ( self ):
        return self.mode & stat.S_IXOTH