fix: initialise _file_base/_ext when file_name is None

Closed
BI
1mo ago
+30-2

Summary

File.__init__ only assigned _file_base and _ext inside the
if file_name is not None branch. When a File is created without a
filename (the documented case for application/octet-stream uploads,
where file_name defaults to None), those attributes are never set.
Any call to _get_disk_file() that reads them then raises an
AttributeError:

  • UPLOAD_KEEP_EXTENSIONS=True → self._ext.decode(...) → AttributeError: 'File' object has no attribute '_ext'
  • UPLOAD_KEEP_FILENAME=True with a dir → self._file_base + self._ext → AttributeError: 'File' object has no attribute '_file_base'

Both paths are reachable when the memory threshold is exceeded
(MAX_MEMORY_FILE_SIZE) during a nameless upload.

Fix

Move the assignments outside the conditional and supply empty-bytes
defaults for the None branch:

if file_name is not None:
    basename = os.path.basename(file_name)
    base, ext = os.path.splitext(basename)
else:
    base, ext = b"", b""
self._file_base = base
self._ext = ext

Two regression tests are added to tests/test_file.py.


This pull request was prepared with the assistance of AI, under my direction and review.