Opened 49 minutes ago
Last modified 41 minutes ago
#37557 assigned Bug
InMemoryStorage raises ValueError instead of FileExistsError for paths nested below a file
| Reported by: | Manoj | Owned by: | Manoj |
|---|---|---|---|
| Component: | File uploads/storage | Version: | dev |
| Severity: | Normal | Keywords: | storage |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
InMemoryStorage raises FileExistsError when a path traverses a file node (see test_file_node_cannot_have_children), but only when the file is the second-to-last path component:
s = InMemoryStorage() s.save("file.txt", ContentFile("x")) s.exists("file.txt/a") # FileExistsError s.exists("file.txt/a/b") # ValueError: tuple.index(x): x not in tuple
InMemoryDirNode.resolve() builds the error path from os.path.split(path), which returns a (head, tail) pair rather than the path segments, then calls .index() on it. This only works when the offending segment is the last component of the path. listdir(), save() and open() go through the same code path. The same code has been there since InMemoryStorage was added in 4.2.
I have a patch and will open a pull request.
Separately, and not addressed by the patch: FileSystemStorage.exists() returns False for these paths, while InMemoryStorage.exists() raises FileExistsError. Happy to open a separate ticket if that is worth discussing.
Pull request: https://github.com/django/django/pull/22379