fix: initialise _file_base/_ext when file_name is None#308
Open
gaoflow wants to merge 1 commit into
Open
Conversation
File.__init__ only set _file_base and _ext inside the `if file_name is not None` branch, leaving them undefined for nameless uploads (e.g. application/octet-stream). Any subsequent call to _get_disk_file() with UPLOAD_KEEP_EXTENSIONS=True or UPLOAD_KEEP_FILENAME=True triggered an AttributeError. Initialise both attributes to empty bytes outside the conditional so they are always present, and cover the regression with two tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
File.__init__only assigned_file_baseand_extinside theif file_name is not Nonebranch. When aFileis created without afilename (the documented case for
application/octet-streamuploads,where
file_namedefaults toNone), those attributes are never set.Any call to
_get_disk_file()that reads them then raises anAttributeError:UPLOAD_KEEP_EXTENSIONS=True→self._ext.decode(...)→AttributeError: 'File' object has no attribute '_ext'UPLOAD_KEEP_FILENAME=Truewith 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
Nonebranch: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.