Skip to content

Commit 45fb826

Browse files
committed
Add shutil.umask context manager implementation
1 parent 8eebe4e commit 45fb826

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Lib/shutil.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import fnmatch
1111
import collections
1212
import errno
13+
from contextlib import AbstractContextManager
1314

1415
try:
1516
import zlib
@@ -61,7 +62,7 @@
6162
"get_unpack_formats", "register_unpack_format",
6263
"unregister_unpack_format", "unpack_archive",
6364
"ignore_patterns", "chown", "which", "get_terminal_size",
64-
"SameFileError"]
65+
"SameFileError", "umask"]
6566
# disk_usage is added later, if available on the platform
6667

6768
class Error(OSError):
@@ -1581,6 +1582,21 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
15811582
return name
15821583
return None
15831584

1585+
1586+
class umask(AbstractContextManager):
1587+
"""Non thread-safe context manager to change the process's umask."""
1588+
1589+
def __init__(self, mask):
1590+
self.mask = mask
1591+
self._old_mask = []
1592+
1593+
def __enter__(self):
1594+
self._old_mask.append(os.umask(self.mask))
1595+
1596+
def __exit__(self, *excinfo):
1597+
os.umask(self._old_mask.pop())
1598+
1599+
15841600
def __getattr__(name):
15851601
if name == "ExecError":
15861602
import warnings

0 commit comments

Comments
 (0)