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

Fixed issue for AttributeError: 'TLSConnection' object has no attribu… #252

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ucsmsdk/ucsdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def connect(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.options |= ssl.OP_NO_SSLv2
ssl_context.options |= ssl.OP_NO_SSLv3
if self.key_file and self.cert_file:
if getattr(self, "key_file", None) and getattr(self, "cert_file", None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide context in PR description or Code comments around when key_file and/or cert_file won't be present?

Copy link
Contributor Author

@dhapati2 dhapati2 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before getattr() implement case: We are just checked the value of cert_file and key_file is present or not. i.e, it is throwing error with python3.12 and works with python3.11 (reference UCSM issue #250)
AttributeError: 'TLSConnection' object has no attribute 'key_file'

After we used getattr () -> to get the value of an object's attribute and if no attribute of that object is found, default value is returned, so error resolved works on both python versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test with out input for key_file ? if None is return how the rest of the code handles it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If key_file or cert_file is not defined or their values are None, the getattr() function will return None for that attribute. So, if key_file or cert_file is not set or set to None the condition in the if statement will evaluate to False, and the subsequent code block will not execute.
In our case both are defined are None so, the code block is not skipped.
So, we did fix to avoid any AttributeError in both conditions.

ssl_context.load_cert_chain(keyfile=self.key_file,
certfile=self.cert_file)
self.sock = ssl_context.wrap_socket(sock)
Expand Down
Loading