Antoine Pitrou avatar Antoine Pitrou committed b88d80f

Add Path.replace()

Comments (0)

Files changed (4)

 
 - Add PurePath.name and PurePath.anchor
 - Add Path.owner and Path.group
+- Add Path.replace()
 
 Version 0.7
 ^^^^^^^^^^^
       'some text'
 
 
+.. method:: Path.replace(target)
+
+   Rename this file or directory to the given *target*.  If *target* points
+   to an existing file or directory, it will be unconditionally replaced.
+
+   This method is only available with Python 3.3; it will raise
+   :exc:`NotImplementedError` on previous Python versions.
+
+
 .. method:: Path.resolve()
 
    Make the path absolute, resolving any symlinks.  A new path object is
 
         rename = _wrap_binary_atfunc(os.rename)
 
+        if sys.version_info >= (3, 3):
+            replace = _wrap_binary_atfunc(os.replace)
+
         def symlink(self, target, pathobj, target_is_directory):
             parent_fd, name = _fdnamepair(pathobj)
             os.symlink(str(target), name, dir_fd=parent_fd)
 
     rename = _wrap_binary_strfunc(os.rename)
 
+    if sys.version_info >= (3, 3):
+        replace = _wrap_binary_strfunc(os.replace)
+
     if nt:
         if supports_symlinks:
             symlink = _wrap_binary_strfunc(os.symlink)
             self._raise_closed()
         self._accessor.rename(self, target)
 
+    def replace(self, target):
+        """
+        Rename this path to the given path, clobbering the existing
+        destination if it exists.
+        """
+        if sys.version_info < (3, 3):
+            raise NotImplementedError("replace() is only available "
+                                      "with Python 3.3 and later")
+        if self._closed:
+            self._raise_closed()
+        self._accessor.replace(self, target)
+
     def symlink_to(self, target, target_is_directory=False):
         """
         Make this path a symlink pointing to the given path.
         self.assertEqual(os.stat(r).st_size, size)
         self.assertFileNotFound(q.restat)
 
+    def test_replace(self):
+        P = self.cls(BASE)
+        p = P['fileA']
+        if sys.version_info < (3, 3):
+            self.assertRaises(NotImplementedError, p.replace, p)
+            return
+        size = p.stat().st_size
+        # Replacing a non-existing path
+        q = P['dirA', 'fileAA']
+        p.rename(q)
+        self.assertEqual(q.stat().st_size, size)
+        self.assertFileNotFound(p.restat)
+        # Replacing another (existing) path
+        r = rel_join('dirB', 'fileB')
+        q.rename(r)
+        self.assertEqual(os.stat(r).st_size, size)
+        self.assertFileNotFound(q.restat)
+
     def test_touch(self):
         P = self.cls(BASE)
         p = P['newfileA']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.