Skip to content
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

docs(example): Avoid UnboundLocalError for get_object when exception #1442

Merged
merged 2 commits into from
Oct 19, 2024

Conversation

100gle
Copy link
Contributor

@100gle 100gle commented Sep 13, 2024

I used get_object as example shown:

# Get data of an object.
try:
    response = client.get_object("my-bucket", "my-object")
    # Read data from response.
finally:
    response.close()
    response.release_conn()

But I got the UnboundLocalError when encountered exception in get_object.

UnboundLocalError: local variable 'response' referenced before assignment

However, the fget_object api has handled this situation, so I think get_object should do so.

+response = None
try:
    response = client.get_object("my-bucket", "my-object")
    # Read data from response.
except Exception:
    # User can handle exception and don't worry about the `UnboundLocalError`.
finally:
+    if response:
        response.close()
        response.release_conn()

@harshavardhana harshavardhana merged commit adb2409 into minio:master Oct 19, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants