GH-127381: pathlib ABCs: remove PathBase.samefile() and rarer is_*() (#127709)

Remove `PathBase.samefile()`, which is fairly specific to the local FS, and
relies on `stat()`, which we're aiming to remove from `PathBase`.

Also remove `PathBase.is_mount()`, `is_junction()`, `is_block_device()`,
`is_char_device()`, `is_fifo()` and `is_socket()`. These rely on POSIX
file type numbers that we're aiming to remove from the `PathBase` API.
This commit is contained in:
Barney Gale 2024-12-11 00:09:55 +00:00 committed by GitHub
parent 51216857ca
commit 12b4f1a5a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 141 additions and 171 deletions

View file

@ -1786,13 +1786,31 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
st = p.stat()
self.assertEqual(st, p.lstat())
def test_is_junction(self):
def test_is_junction_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_junction())
self.assertFalse((P / 'dirA').is_junction())
self.assertFalse((P / 'non-existing').is_junction())
self.assertFalse((P / 'fileA' / 'bah').is_junction())
self.assertFalse((P / 'fileA\udfff').is_junction())
self.assertFalse((P / 'fileA\x00').is_junction())
def test_is_junction_true(self):
P = self.cls(self.base)
with mock.patch.object(P.parser, 'isjunction'):
self.assertEqual(P.is_junction(), P.parser.isjunction.return_value)
P.parser.isjunction.assert_called_once_with(P)
def test_is_fifo_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_fifo())
self.assertFalse((P / 'dirA').is_fifo())
self.assertFalse((P / 'non-existing').is_fifo())
self.assertFalse((P / 'fileA' / 'bah').is_fifo())
self.assertIs((P / 'fileA\udfff').is_fifo(), False)
self.assertIs((P / 'fileA\x00').is_fifo(), False)
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
@unittest.skipIf(sys.platform == "vxworks",
"fifo requires special path on VxWorks")
@ -1808,6 +1826,15 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertIs(self.cls(self.base, 'myfifo\udfff').is_fifo(), False)
self.assertIs(self.cls(self.base, 'myfifo\x00').is_fifo(), False)
def test_is_socket_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_socket())
self.assertFalse((P / 'dirA').is_socket())
self.assertFalse((P / 'non-existing').is_socket())
self.assertFalse((P / 'fileA' / 'bah').is_socket())
self.assertIs((P / 'fileA\udfff').is_socket(), False)
self.assertIs((P / 'fileA\x00').is_socket(), False)
@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required")
@unittest.skipIf(
is_emscripten, "Unix sockets are not implemented on Emscripten."
@ -1831,6 +1858,24 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertIs(self.cls(self.base, 'mysock\udfff').is_socket(), False)
self.assertIs(self.cls(self.base, 'mysock\x00').is_socket(), False)
def test_is_block_device_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_block_device())
self.assertFalse((P / 'dirA').is_block_device())
self.assertFalse((P / 'non-existing').is_block_device())
self.assertFalse((P / 'fileA' / 'bah').is_block_device())
self.assertIs((P / 'fileA\udfff').is_block_device(), False)
self.assertIs((P / 'fileA\x00').is_block_device(), False)
def test_is_char_device_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_char_device())
self.assertFalse((P / 'dirA').is_char_device())
self.assertFalse((P / 'non-existing').is_char_device())
self.assertFalse((P / 'fileA' / 'bah').is_char_device())
self.assertIs((P / 'fileA\udfff').is_char_device(), False)
self.assertIs((P / 'fileA\x00').is_char_device(), False)
def test_is_char_device_true(self):
# os.devnull should generally be a char device.
P = self.cls(os.devnull)
@ -1842,7 +1887,14 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertIs(self.cls(f'{os.devnull}\udfff').is_char_device(), False)
self.assertIs(self.cls(f'{os.devnull}\x00').is_char_device(), False)
def test_is_mount_root(self):
def test_is_mount(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_mount())
self.assertFalse((P / 'dirA').is_mount())
self.assertFalse((P / 'non-existing').is_mount())
self.assertFalse((P / 'fileA' / 'bah').is_mount())
if self.can_symlink:
self.assertFalse((P / 'linkA').is_mount())
if os.name == 'nt':
R = self.cls('c:\\')
else:
@ -1850,6 +1902,27 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertTrue(R.is_mount())
self.assertFalse((R / '\udfff').is_mount())
def test_samefile(self):
parser = self.parser
fileA_path = parser.join(self.base, 'fileA')
fileB_path = parser.join(self.base, 'dirB', 'fileB')
p = self.cls(fileA_path)
pp = self.cls(fileA_path)
q = self.cls(fileB_path)
self.assertTrue(p.samefile(fileA_path))
self.assertTrue(p.samefile(pp))
self.assertFalse(p.samefile(fileB_path))
self.assertFalse(p.samefile(q))
# Test the non-existent file case
non_existent = parser.join(self.base, 'foo')
r = self.cls(non_existent)
self.assertRaises(FileNotFoundError, p.samefile, r)
self.assertRaises(FileNotFoundError, p.samefile, non_existent)
self.assertRaises(FileNotFoundError, r.samefile, p)
self.assertRaises(FileNotFoundError, r.samefile, non_existent)
self.assertRaises(FileNotFoundError, r.samefile, r)
self.assertRaises(FileNotFoundError, r.samefile, non_existent)
def test_passing_kwargs_errors(self):
with self.assertRaises(TypeError):
self.cls(foo="bar")