Add `umask` to `contextlib`

Based on your ModificationContext abstract class I was thinking ‘are there any more classes like umask that we might want to add?’. I mentioned seteuid and friends earlier, in particular the one that makes sense as a context manager is seteuid: seteuid sets the effective user id, with which files are created / owned, and against which filesystem access checks are done. It reminded me of sudo and su. Some people read ‘su’ as ‘switch user’, or ‘super user’, but looking at man 1 su: “su allows commands to be run with a substitute user and group ID”

  • substitute_{NOUN}

  • substitute_cwd

  • substitute_umask

  • substitute_euid (would need a separate thread to discuss)

That seems like a good verb, with a connotation of transience. It can be read as both an imperative ‘substitute the umask!’,

and also as an adjective: ‘this is a substitute umask’. I hope this double edged property of ‘substitute!’ (imp) and ‘substitute’ (adj) is useful, and doesn’t create confusion pulling in both directions.

The _context suffix is sort of implied by virtue of a class having __{enter,exit}__ methods, and removing _context from umask_context brings us right back to plain umask, which we’ve ruled out.

And using substitute means that the class chdir_context would be renamed substitute_cwd. Technically win32 has a chdir syscall / ntdll.dll-call, but Windows / DOS is weird with non-posix-y names (‘chdir is a deprecated alias for the _chdir function’). Python’s os.chdir of course works on Windows, but cwd is a more platform-agnostic, implementation independent concept.

So we’d be continuing a pattern of cwd with that. (sorry to cross-contaminate threads but it’s relevant :slight_smile: )

Oh, and SubstitutionContext may be a name for ModificationContext. All of substitute_{cwd,umask,euid} follow the stack-style implementation. The substitution behavior is fundamentally what separates file-locking context managers, who should do nothing on re-entry (if ever one wanted a re-enterable file-locking context manager which sounds like a deadlock-disaster waiting to happen), from substitution context managers, who save-and-substitute the current state every (re)entry, and restore it on exit.

1 Like