Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add umask context manager to shutil #128432

Closed
jb2170 opened this issue Jan 3, 2025 · 3 comments
Closed

Add umask context manager to shutil #128432

jb2170 opened this issue Jan 3, 2025 · 3 comments
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jb2170
Copy link
Contributor

jb2170 commented Jan 3, 2025

Feature or enhancement

Proposal:

PR Incoming. As discussed earlier.

We add a context manager which sets the process's umask to a specified value on entry, and resets to the old one on exit. It is reentrant safe.

We have discussed and chosen shutil as the module to add this to, not contextlib.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/add-umask-to-contextlib/75809/21

Linked PRs

@jb2170 jb2170 added the type-feature A feature request or enhancement label Jan 3, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jan 3, 2025
@erlend-aasland erlend-aasland added the pending The issue will be closed if no feedback is provided label Jan 7, 2025
@erlend-aasland
Copy link
Contributor

Based on the discussion over at DPO, I'm suggesting to close this. cc. @ncoghlan

@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 7, 2025

Unfortunately, despite my initial enthusiasm for the idea, I'm coming to the same conclusion as @erlend-aasland. Any addition to the standard library needs to address the mantra of "not every 3 line function needs to be a builtin" (or its far less snappy corollary, "not every 6 line class needs to be in the standard library").

My initial enthusiasm was based on considering this as a way to influence the file creation of libraries and subprocesses where it isn't feasible to alter their file creation mode directly, and it instead needs to be done indirectly.

However:

  • when it is possible to control the file and directory creation calls, setting the mode when creating the file is a better option than setting the process level umask
  • even on POSIX systems, if a directory has an ACL set, the process umask will be ignored, so this CM isn't a guaranteed filter (the files being created need to be going into a directory without an ACL set)
  • even though os.umask is technically available on Windows, it isn't a common approach to permissions management there (and we're not even sure it works the same way it does elsewhere, since every NTFS folder has an ACL set, so the umask legitimately won't have any effect)

This makes the proposed context manager far more use case specific than I originally thought it would be, which means it more appropriately belongs in an application or domain specific utility library than it does in the generally cross-platform and cross-domain standard library.

I did find the discussion valuable (and I genuinely appreciate your patience @jb2170), but I think the only lasting stdlib impact is going to be the pattern extraction exercise that resulted in #128574.

@ncoghlan ncoghlan closed this as completed Jan 7, 2025
@ncoghlan ncoghlan reopened this Jan 7, 2025
@ncoghlan ncoghlan closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
@jb2170
Copy link
Contributor Author

jb2170 commented Jan 8, 2025

Cancelled after realisation in the discussion that two separate, non-beginner-friendly, context managers would be required: one to restrict / increase the umask, and one to absolutely set it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants