ORM: Add from_bytes
classmethod
to orm.SinglefileData
#6653
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.
As noted together with @elinscott and @mikibonacci:
orm.SinglefileData
supports getting its content as bytes viaget_content
, but did not support setting its contents from bytes directly. This is now made possible with thefrom_bytes
classmethod
. A few notes on the implementation:from_string
method, and adapt the behavior depending on the type of the content being passed. However, I feel like the namefrom_string
, in that case, is misleading. It would be more suitable if the method were calledfrom_content
(or similar, more generic), however, that is not the case, and we cannot modify it due to backwards-compatibility reasons..decode('utf-8')
themselves on the bytes object they want to store before calling.from_string()
, but it might be more convenient to allow directly passing the bytes object viafrom_bytes
, as to not have the burden of conversion on the user.test_get_content
function is now already being tested in the (already existing)test_from_string
and the (new)test_from_bytes
functions, which check the content of theSinglefileData
node after storing. However, I still left it for completeness.