8000 bpo-39895: Move `pathlib.Path.touch()` implementation into the path a… · python/cpython@17e3af2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17e3af2

Browse files
committed
bpo-39895: Move pathlib.Path.touch() implementation into the path accessor.
1 parent 6713e86 commit 17e3af2

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

Lib/pathlib.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,23 @@ def symlink(a, b, target_is_directory):
444444
def symlink(a, b, target_is_directory):
445445
return os.symlink(a, b)
446446

447-
utime = os.utime
447+
def touch(self, path, mode=0o666, exist_ok=True):
448+
if exist_ok:
449+
# First try to bump modification time
450+
# Implementation note: GNU touch uses the UTIME_NOW option of
451+
# the utimensat() / futimens() functions.
452+
try:
453+
os.utime(path, None)
454+
except OSError:
455+
# Avoid exception chaining
456+
pass
457+
else:
458+
return
459+
flags = os.O_CREAT | os.O_WRONLY
460+
if not exist_ok:
461+
flags |= os.O_EXCL
462+
fd = os.open(path, flags, mode)
463+
os.close(fd)
448464

449465
# Helper for resolve()
450466
def readlink(self, path):
@@ -1111,13 +1127,6 @@ def _opener(self, name, flags, mode=0o666):
11111127
# A stub for the opener argument to built-in open()
11121128
return self._accessor.open(self, flags, mode)
11131129

1114-
def _raw_open(self, flags, mode=0o777):
1115-
"""
1116-
Open the file pointed by this path and return a file descriptor,
1117-
as os.open() does.
1118-
"""
1119-
return self._accessor.open(self, flags, mode)
1120-
11211130
# Public API
11221131

11231132
@classmethod
@@ -1290,22 +1299,7 @@ def touch(self, mode=0o666, exist_ok=True):
12901299
"""
12911300
Create this file with the given access mode, if it doesn't exist.
12921301
"""
1293-
if exist_ok:
1294-
# First try to bump modification time
1295-
# Implementation note: GNU touch uses the UTIME_NOW option of
1296-
# the utimensat() / futimens() functions.
1297-
try:
1298-
self._accessor.utime(self, None)
1299-
except OSError:
1300-
# Avoid exception chaining
1301-
pass
1302-
else:
1303-
return
1304-
flags = os.O_CREAT | os.O_WRONLY
1305-
if not exist_ok:
1306-
flags |= os.O_EXCL
1307-
fd = self._raw_open(flags, mode)
1308-
os.close(fd)
1302+
self._accessor.touch(self, mode, exist_ok)
13091303

13101304
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
13111305
"""

0 commit comments

Comments
 (0)
0