Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f6d09c0
GH-73991: Add `pathlib.Path.move()`
barneygale Jul 21, 2024
1dc0bfb
Simplify slightly
barneygale Jul 21, 2024
f488ff8
Test existing files/directories as destinations.
barneygale Jul 21, 2024
ff7746f
rename --> replace
barneygale Jul 21, 2024
29e51f5
Windows test fixes
barneygale Jul 21, 2024
36955ab
Revert "Windows test fixes"
barneygale Jul 21, 2024
d595047
Handle existing targets more consistently
barneygale Jul 21, 2024
5de963e
Use generic implementation on ERROR_ACCESS_DENIED.
barneygale Jul 21, 2024
e1c0d9e
Expand test coverage, lean into the Windows differences a bit.
barneygale Jul 22, 2024
a98aed4
Loosen tests for OSError types.
barneygale Jul 22, 2024
0af6396
Revert "Loosen tests for OSError types."
barneygale Jul 22, 2024
348cabd
Tweaks
barneygale Jul 22, 2024
aca70d1
Relax expectations once again.
barneygale Jul 22, 2024
aa27f48
skip tests affected by apparent os.replace() inconsistency
barneygale Jul 22, 2024
04b115a
More compatibility experiments.
barneygale Jul 22, 2024
be48d2a
Cunningly avoid specifying the problematic cases.
barneygale Jul 22, 2024
a5ee60a
Fix moving a file/directory to itself.
barneygale Jul 22, 2024
f9219ee
Undo unnecessary change
barneygale Jul 22, 2024
5ad2c1d
Ensure we exit from copytree() when copying directory over itself.
barneygale Jul 27, 2024
46b1fc2
Merge branch 'main' into path-move
barneygale Aug 7, 2024
d889dff
Merge branch 'main' into path-move
barneygale Aug 11, 2024
9d92108
Docs tweak
barneygale Aug 11, 2024
92cc785
Set `filename` and `filename2` in error messages.
barneygale Aug 11, 2024
b8372aa
Simplify patch
barneygale Aug 11, 2024
8199fd5
Undo changes from GH-122924.
barneygale Aug 13, 2024
175d687
Merge branch 'main' into path-move
barneygale Aug 23, 2024
8182a88
Merge branch 'main' into path-move
barneygale Aug 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand test coverage, lean into the Windows differences a bit.
  • Loading branch information
barneygale committed Jul 22, 2024
commit e1c0d9e7d1f5fc6a678a20c3a997c310335c52a7
6 changes: 1 addition & 5 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,7 @@ def move(self, target):
if not isinstance(target, PathBase):
raise
except OSError as err:
if err.errno == errno.EXDEV:
pass # target is on another filesystem.
elif os.name == 'nt' and err.winerror == 5: # ERROR_ACCESS_DENIED
pass # target may have the wrong type.
else:
if err.errno != errno.EXDEV:
raise
return PathBase.move(self, target)

Expand Down
64 changes: 64 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@
{os.open, os.stat, os.unlink, os.rmdir} <= os.supports_dir_fd and
os.listdir in os.supports_fd and os.stat in os.supports_follow_symlinks)

def patch_replace(old_test):
def new_replace(self, target):
raise OSError(errno.EXDEV, "Cross-device link", self, target)

def new_test(self):
old_replace = self.cls.replace
self.cls.replace = new_replace
try:
old_test(self)
finally:
self.cls.replace = old_replace
return new_test

#
# Tests for the pure classes.
#
Expand Down Expand Up @@ -751,6 +764,57 @@ def test_copytree_preserve_metadata_xattrs(self):
target_file = target.joinpath('dirD', 'fileD')
self.assertEqual(os.getxattr(target_file, b'user.foo'), b'42')

@patch_replace
def test_move_file_other_fs(self):
self.test_move_file()

@patch_replace
def test_move_file_to_file_other_fs(self):
self.test_move_file_to_file()

@patch_replace
def test_move_file_to_dir_other_fs(self):
self.test_move_file_to_dir()

@patch_replace
def test_move_file_to_empty_dir_other_fs(self):
self.test_move_file_to_empty_dir()

@patch_replace
def test_move_dir_other_fs(self):
self.test_move_dir()

@patch_replace
def test_move_dir_to_file_other_fs(self):
self.test_move_dir_to_file()

@patch_replace
def test_move_dir_to_dir_other_fs(self):
self.test_move_dir_to_dir()

@patch_replace
def test_move_dir_to_empty_dir_other_fs(self):
self.test_move_dir_to_empty_dir()

@patch_replace
def test_move_dir_into_itself_other_fs(self):
self.test_move_dir_into_itself()

@needs_symlinks
@patch_replace
def test_move_file_symlink_other_fs(self):
self.test_move_file_symlink()

@needs_symlinks
@patch_replace
def test_move_dir_symlink_other_fs(self):
self.test_move_dir_symlink()

@needs_symlinks
@patch_replace
def test_move_dangling_symlink_other_fs(self):
self.test_move_dangling_symlink()

def test_resolve_nonexist_relative_issue38671(self):
p = self.cls('non', 'exist')

Expand Down
23 changes: 19 additions & 4 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,14 +2008,22 @@ def test_move_file_to_dir(self):
base = self.cls(self.base)
source = base / 'fileA'
target = base / 'dirB'
self.assertRaises(IsADirectoryError, source.move, target)
if self.cls.parser is posixpath:
exc_type = IsADirectoryError
else:
exc_type = PermissionError
self.assertRaises(exc_type, source.move, target)

def test_move_file_to_empty_dir(self):
base = self.cls(self.base)
source = base / 'fileA'
target = base / 'fileA_moved'
target.mkdir()
self.assertRaises(IsADirectoryError, source.move, target)
if self.cls.parser is posixpath:
exc_type = IsADirectoryError
else:
exc_type = PermissionError
self.assertRaises(exc_type, source.move, target)

def test_move_dir(self):
base = self.cls(self.base)
Expand All @@ -2037,15 +2045,22 @@ def test_move_dir_to_file(self):
base = self.cls(self.base)
source = base / 'dirB'
target = base / 'fileA'
self.assertRaises(NotADirectoryError, source.move, target)
if self.cls.parser is posixpath:
exc_type = NotADirectoryError
else:
exc_type = PermissionError
self.assertRaises(exc_type, source.move, target)

def test_move_dir_to_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirB'
with self.assertRaises(OSError) as cm:
source.move(target)
self.assertEqual(cm.exception.errno, errno.ENOTEMPTY)
if self.cls.parser is posixpath:
self.assertEqual(cm.exception.errno, errno.ENOTEMPTY)
else:
self.assertEqual(cm.exception.winerror, 5) # ERROR_ACCESS_DENIED

def test_move_dir_to_empty_dir(self):
base = self.cls(self.base)
Expand Down