Conversation
thepaul
reviewed
Oct 27, 2022
uplink_python/access.py
Outdated
| raise _storj_exception(encryption_key_result.error.contents.code, | ||
| encryption_key_result.error.contents.message.decode("utf-8")) | ||
| return encryption_key_result.encryption_key | ||
| error_code = encryption_key_result.error.contents.code |
There was a problem hiding this comment.
It looks like this is a pretty common pattern here. Also, though, it looks kind of fragile. Even if we are very careful this time, the next person to work on this code could easily mess something up.
It might be worth isolating this pattern to a few utility functions. Something like:
def unwrap_libuplink_result(result_object, finalizer, attribute_name):
if bool(result_object.error):
error_code = result_object.error.contents.code
error_msg = result_object.error.contents.message.decode("utf-8")
finalizer(result_object)
raise _storj_exception(error_code, error_msg)
result = getattr(result_object, attribute_name)
finalizer(result_object)
return result
def unwrap_encryption_key_result(result_object, uplink_handle):
return unwrap_libuplink_result(result_object, uplink_handle.m_libuplink.uplink_free_encryption_key_result, 'encryption_key')
def unwrap_project_result(result_object, uplink_handle):
return unwrap_libuplink_result(result_object, uplink_handle.m_libuplink.uplink_free_project_result, 'project')
# (and similar methods for StringResult, AccessResult, etc)Then in this function you could replace this whole ending (everything from line 81 on) with:
return unwrap_encryption_key_result(encryption_key_result, self.uplink)The preexisting code in this repo is still pretty messy, though, so that could only help so much.
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.
We were not freeing memory after use. This PR tries to fix this.