Commit 40fee16
committed
[pathlib] Prevent Path.copy() from processing special files
Previously, `pathlib.Path.copy()` would indiscriminately attempt to open and read from the source path if it wasn't a directory or symlink. This caused severe issues with special files:
1. Blocking indefinitely when reading from a FIFO (named pipe).
2. Entering an infinite loop when reading from zero-generators like `/dev/zero`.
This commit modifies the copy logic to explicitly verify that the source is a regular file using `is_file()` before attempting to copy its content. If the source exists but is a special file, `io.UnsupportedOperation` is now raised. This change aligns `pathlib`'s behavior with safety expectations and prevents resource exhaustion or hanging processes.
Existing behavior for dangling symlinks (raising FileNotFoundError) is preserved.
Tests added:
- `test_copy_fifo`: Ensures copying a FIFO raises UnsupportedOperation.
- `test_copy_char_device`: Ensures copying a character device (e.g. /dev/null) raises UnsupportedOperation.1 parent 7e28ae5 commit 40fee16
2 files changed
Lines changed: 36 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1328 | 1328 | | |
1329 | 1329 | | |
1330 | 1330 | | |
1331 | | - | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
1332 | 1334 | | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
1333 | 1338 | | |
1334 | 1339 | | |
1335 | 1340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
172 | 202 | | |
173 | 203 | | |
174 | 204 | | |
0 commit comments