Author: Ronan Lamy <[email protected]>
Branch: follow_symlinks
Changeset: r83623:af34944fdc16
Date: 2016-04-12 17:49 +0100
http://bitbucket.org/pypy/pypy/changeset/af34944fdc16/

Log:    Enable fd support in utime()

diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -120,7 +120,7 @@
         self.as_bytes = bytes
         self.as_unicode = unicode
 
-class path_or_fd(Unwrapper):
+class _PathOrFd(Unwrapper):
     def unwrap(self, space, w_value):
         if _WIN32:
             try:
@@ -139,6 +139,23 @@
         fd = unwrap_fd(space, w_value)
         return Path(fd, None, None)
 
+class _JustPath(Unwrapper):
+    def unwrap(self, space, w_value):
+        if _WIN32:
+            try:
+                path_u = space.unicode_w(w_value)
+                return Path(-1, None, path_u)
+            except OperationError:
+                pass
+        try:
+            path_b = space.fsencode_w(w_value)
+            return Path(-1, path_b, None)
+        except OperationError:
+            raise oefmt(space.w_TypeError, "illegal type for path parameter")
+
+def path_or_fd(allow_fd=True):
+    return _PathOrFd if allow_fd else _JustPath
+
 
 if hasattr(rposix, 'AT_FDCWD'):
     DEFAULT_DIR_FD = rposix.AT_FDCWD
@@ -1303,7 +1320,7 @@
 
 
 @unwrap_spec(
-    path=path_or_fd,
+    path=path_or_fd(allow_fd=rposix.HAVE_FUTIMENS),
     w_times=WrappedDefault(None), w_ns=kwonly(WrappedDefault(None)),
     dir_fd=DirFD(rposix.HAVE_UTIMENSAT), follow_symlinks=kwonly(bool))
 def utime(space, path, w_times, w_ns, dir_fd=DEFAULT_DIR_FD, 
follow_symlinks=True):
@@ -1356,6 +1373,21 @@
         atime_s, atime_ns = convert_ns(space, args_w[0])
         mtime_s, mtime_ns = convert_ns(space, args_w[1])
 
+    if path.as_fd != -1:
+        if dir_fd != DEFAULT_DIR_FD:
+            raise oefmt(space.w_ValueError,
+                        "utime: can't specify both dir_fd and fd")
+        if not follow_symlinks:
+            raise oefmt(space.w_ValueError,
+                        "utime: cannot use fd and follow_symlinks together")
+        if utime_now:
+            atime_ns = mtime_ns = rposix.UTIME_NOW
+        try:
+            rposix.futimens(path.as_fd, atime_s, atime_ns, mtime_s, mtime_ns)
+            return
+        except OSError as e:
+            raise wrap_oserror(space, e)
+
     if rposix.HAVE_UTIMENSAT:
         path_b = path.as_bytes
         if path_b is None:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to