This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author YoSTEALTH
Recipients YoSTEALTH
Date 2020-07-23.15:07:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1595516840.65.0.992254225297.issue41375@roundup.psfhosted.org>
In-reply-to
Content
import os
import stat
import os.path


def problem(tmp_path):
    # result:
    # -------
    # check: False
    # mode: 416

    # create temp file
    fd = os.open(tmp_path, os.O_CREAT, 0o660)
    os.close(fd)

    # Directory is effected as well
    # os.mkdir(tmp_path, 0o660)


def solution(tmp_path):
    # result:
    # -------
    # check: True
    # mode: 432

    old_umask = os.umask(0)

    # create temp file
    fd = os.open(tmp_path, os.O_CREAT, 0o660)
    os.close(fd)

    # create temp dir
    # os.mkdir(tmp_path, 0o660)

    os.umask(old_umask)


def main():
    tmp_path = '_testing-chmod'

    problem(tmp_path)
    # solution(tmp_path)

    try:
        s = os.stat(tmp_path)
        mode = stat.S_IMODE(s.st_mode)
        print('check:', mode == 0o660)
        print('mode:', mode)  # this should be: 432
    finally:
        # delete temp file
        try:
            os.unlink(tmp_path)
        except IsADirectoryError:
            os.rmdir(tmp_path)


if __name__ == '__main__':
    main()


This result is not same for all os and distro, on multiple linux system for example the results will be different. I think Python should account for such behavior by default as it can lead to file/dir creation with security issues.
History
Date User Action Args
2020-07-23 15:07:20YoSTEALTHsetrecipients: + YoSTEALTH
2020-07-23 15:07:20YoSTEALTHsetmessageid: <1595516840.65.0.992254225297.issue41375@roundup.psfhosted.org>
2020-07-23 15:07:20YoSTEALTHlinkissue41375 messages
2020-07-23 15:07:20YoSTEALTHcreate