bpo-33635: Handling Bad file descriptor in Path.is_file and related. … · python/cpython@2cf3316 · GitHub
Skip to content

Commit 2cf3316

Browse files
bpo-33635: Handling Bad file descriptor in Path.is_file and related. (GH-8542)
(cherry picked from commit 216b745) Co-authored-by: Przemysław Spodymek <przemyslaw@spodymek.com>
1 parent fa3fd4c commit 2cf3316

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

Lib/pathlib.py

Lines changed: 19 additions & 10 deletions

Lib/test/test_pathlib.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections.abc
22
import io
33
import os
4+
import sys
45
import errno
56
import pathlib
67
import pickle
@@ -2176,6 +2177,29 @@ def test_expanduser(self):
21762177
self.assertEqual(p6.expanduser(), p6)
21772178
self.assertRaises(RuntimeError, p7.expanduser)
21782179

2180+
@unittest.skipIf(sys.platform != "darwin",
2181+
"Bad file descriptor in /dev/fd affects only macOS")
2182+
def test_handling_bad_descriptor(self):
2183+
try:
2184+
file_descriptors = list(pathlib.Path('/dev/fd').rglob("*"))[3:]
2185+
if not file_descriptors:
2186+
self.skipTest("no file descriptors - issue was not reproduced")
2187+
# Checking all file descriptors because there is no guarantee
2188+
# which one will fail.
2189+
for f in file_descriptors:
2190+
f.exists()
2191+
f.is_dir()
2192+
f.is_file()
2193+
f.is_symlink()
2194+
f.is_block_device()
2195+
f.is_char_device()
2196+
f.is_fifo()
2197+
f.is_socket()
2198+
except OSError as e:
2199+
if e.errno == errno.EBADF:
2200+
self.fail("Bad file descriptor not handled.")
2201+
raise
2202+
21792203

21802204
@only_nt
21812205
class WindowsPathTest(_BasePathTest, unittest.TestCase):
Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)