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.

classification
标题: os.makedirs('dir1/dir2', 0) always fails
类型: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: serhiy.storchaka 抄送列表: gregory.p.smith, loewis, martin.panter, pitrou, serhiy.storchaka, vajrasky
优先级: normal 关键字: patch

Created on 2013-12-08 11:09 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
os_makedirs_mode.patch serhiy.storchaka, 2013-12-08 11:09 review
os_makedirs_mode_2.patch serhiy.storchaka, 2013-12-08 16:38 review
Pull Requests
URL Status Linked Edit
PR 799 merged serhiy.storchaka, 2017-03-24 09:52
Messages (7)
msg205543 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2013-12-08 11:09
os.makedirs() can't create a directory with cleared write or list permission bits for owner when parent directories aren't created. This is because for parent directories same mode is used as for final directory.

Note that the mkdir utility creates parent directories with default mode (0o777 & ~umask).

$ mkdir -p -m 0 t1/t2/t3
$ ls -l -d t1 t1/t2 t1/t2/t3
drwxrwxr-x 3 serhiy serhiy 4096 Dec  7 22:30 t1/
drwxrwxr-x 3 serhiy serhiy 4096 Dec  7 22:30 t1/t2/
d--------- 2 serhiy serhiy 4096 Dec  7 22:30 t1/t2/t3/

The proposed patch emulates the mkdir utility.

See also issue19921.
msg205570 - (view) Author: Vajrasky Kok (vajrasky) * 日期: 2013-12-08 15:42
Fails on Windows Vista.

======================================================================
FAIL: test_mode (__main__.MakedirTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Lib\test\test_os.py", line 907, in test_mode
    self.assertEqual(stat.S_IMODE(os.stat(parent).st_mode), 0o775)
AssertionError: 511 != 509

----------------------------------------------------------------------
Ran 157 tests in 1.865s

FAILED (failures=1, skipped=61)
Traceback (most recent call last):
  File "Lib\test\test_os.py", line 2511, in <module>
    test_main()
  File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1831,
 in decorator
    return func(*args)
  File "Lib\test\test_os.py", line 2507, in test_main
    FDInheritanceTests,
  File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1719,
 in run_unittest
    _run_suite(suite)
  File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1694,
 in _run_suite
    raise TestFailed(err)
test.support.TestFailed: Traceback (most recent call last):
  File "Lib\test\test_os.py", line 907, in test_mode
    self.assertEqual(stat.S_IMODE(os.stat(parent).st_mode), 0o775)
AssertionError: 511 != 509

The permission of directory on Windows no matter what mode you give or umask you give to support.temp_umask, is always 0o777 (or 511). I think this test does not make sense in Windows.

>>> os.mkdir('cutecat', 0o555)
>>> os.mkdir('cutecat2', 0o777)
>>> os.stat('cutecat')
os.stat_result(st_mode=16895, st_ino=3940649674207852, st_dev=3960548439, st_nli
nk=1, st_uid=0, st_gid=0, st_size=0, st_atime=1386517061, st_mtime=1386517061, s
t_ctime=1386517061)
>>> os.stat('cutecat2')
os.stat_result(st_mode=16895, st_ino=5066549581050708, st_dev=3960548439, st_nli
nk=1, st_uid=0, st_gid=0, st_size=0, st_atime=1386517067, st_mtime=1386517067, s
t_ctime=1386517067)

Either that, or I am missing something.
msg205581 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2013-12-08 16:38
Thank you Vajrasky. Now this check is skipped on Windows.
msg268093 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2016-06-10 06:07
I’ve never considered this sort of scenario properly, so I don’t know if leaving the default mode for the parent directories is the best way or not. The obvious but more complicated alternative would be to call chmod() on all the new directories in a second step.

Anyway, if we are going to make any change, it should be documented. And I would say the current patch changes behaviour, so is probably not applicable as a bug fix.
msg268101 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2016-06-10 07:29
> The obvious but more complicated alternative would be to call chmod() on all the new directories in a second step.

This is dangerous, because if you create read-only or unlistable directory, you couldn't remove it without changing the permission of parent directory. shutil.rmtree() would fail.
msg290082 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2017-03-24 11:27
New changeset e304e33c16e060932d1e2cc8a030d42b02b429b5 by Serhiy Storchaka in branch 'master':
bpo-19930: The mode argument of os.makedirs() no longer affects the file (#799)
https://github.com/python/cpython/commit/e304e33c16e060932d1e2cc8a030d42b02b429b5
msg381083 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) 日期: 2020-11-16 09:55
This API change was not strictly a bugfix, it removed a feature existing code was relying on.

https://bugs.python.org/issue42367 opened to reconcile the two.
历史
日期 用户 动作 参数
2022-04-11 14:57:55admin修改github: 64129
2020-11-16 09:55:44gregory.p.smith修改抄送: + gregory.p.smith
消息: + msg381083
2017-03-24 11:28:49serhiy.storchaka修改状态: open -> closed
assignee: serhiy.storchaka
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.7, - Python 2.7, Python 3.3, Python 3.4
2017-03-24 11:27:44serhiy.storchaka修改消息: + msg290082
2017-03-24 09:52:21serhiy.storchaka修改pull_requests: + pull_request702
2016-06-10 07:29:59serhiy.storchaka修改消息: + msg268101
2016-06-10 06:07:57martin.panter修改抄送: + martin.panter
消息: + msg268093
2013-12-16 19:28:37serhiy.storchaka修改抄送: + pitrou
2013-12-08 16:38:03serhiy.storchaka修改文件: + os_makedirs_mode_2.patch

消息: + msg205581
2013-12-08 15:42:19vajrasky修改抄送: + vajrasky
消息: + msg205570
2013-12-08 11:09:53serhiy.storchaka修改文件: + os_makedirs_mode.patch
抄送: + loewis
keywords: + patch
2013-12-08 11:09:05serhiy.storchaka创建