Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/IronPython.Modules/nt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,10 @@ public static object urandom(object? n)
private static readonly object _umaskKey = new object();

public static int umask(CodeContext/*!*/ context, int mask) {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
return umaskUnix(mask & 0x1FF);
}

mask &= 0x180;
object oldMask = context.LanguageContext.GetSetModuleState(_umaskKey, mask);
if (oldMask == null) {
Expand Down
6 changes: 6 additions & 0 deletions src/core/IronPython.Modules/posix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ private static void chmodUnix(string path, int mode) {
}


[SupportedOSPlatform("linux")]
[SupportedOSPlatform("macos")]
private static int umaskUnix(int mask)
=> (int)NativeConvert.FromFilePermissions(Syscall.umask(NativeConvert.ToFilePermissions((uint)mask)));


[SupportedOSPlatform("linux")]
[SupportedOSPlatform("macos")]
private static void linkUnix(string src, string dst) {
Expand Down
18 changes: 18 additions & 0 deletions tests/suite/modules/system_related/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# See the LICENSE file in the project root for more information.

import os
import unittest

from iptest import IronPythonTestCase, is_osx, is_linux, is_windows, run_test

Expand Down Expand Up @@ -55,4 +56,21 @@ def test_open_abplus(self):
finally:
os.close(fd)

@unittest.skipIf(is_windows, "umask is emulated on Windows")
def test_umask(self):
old = os.umask(0o022)
try:
self.assertEqual(os.umask(0o077), 0o022)
self.assertEqual(os.umask(0o022), 0o077)

os.umask(0o027)
try:
fd = os.open(self.temp_file, os.O_CREAT | os.O_WRONLY, 0o666)
os.close(fd)
self.assertEqual(os.stat(self.temp_file).st_mode & 0o777, 0o640)
finally:
self.delete_files(self.temp_file)
finally:
os.umask(old)

run_test(__name__)
Loading