-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use memory management functions #22
base: master
Are you sure you want to change the base?
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
We were not freeing memory after use. This PR tries to fix this.