-
Notifications
You must be signed in to change notification settings - Fork 116
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
Safe qt importer #804
base: master
Are you sure you want to change the base?
Safe qt importer #804
Conversation
update from upstream
@@ -55,35 +55,35 @@ def QtCore(self): | |||
""" | |||
:returns: QtCore module, if available. | |||
""" | |||
return self._modules["QtCore"] if self._modules else None | |||
return self._modules.get("QtCore") |
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.
The previous syntax implies self._modules
may not be set when called so you may need this instead:
return self._modules.get("QtCore") | |
return self._modules.get("QtCore") if self._modules else None |
This covers QtCore or the module not being present in the dict and the dict itself not being set.
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.
If the _modules dict is initialised to an empty dict, get should still be safe, i.e.
>>> d = {}
>>> print(d.get('foobar'))
None
If the issue is covering a scenario where _modules is not an attr of the object at all, I don't think we'd need to do that as it's in the constructor?
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.
I would prefer the suggestion by @halil3d above, instead of initializing self._modules
in the constructor to an empty dictionary, because for a couple of minor reasons:
- If
_modules
is None, this clearly indicates that the import failed for the requested Qt interface. An empty dict may suggest a different error during the import. - The
modules
property returns the the value of_modules
- so we would be changing the return value fromNone
to{}
if the import failed. Which means if callers are checking specifically forNone
, this would break their 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.
LGTM - just one minor comment.
@@ -55,35 +55,35 @@ def QtCore(self): | |||
""" | |||
:returns: QtCore module, if available. | |||
""" | |||
return self._modules["QtCore"] if self._modules else None | |||
return self._modules.get("QtCore") |
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.
I would prefer the suggestion by @halil3d above, instead of initializing self._modules
in the constructor to an empty dictionary, because for a couple of minor reasons:
- If
_modules
is None, this clearly indicates that the import failed for the requested Qt interface. An empty dict may suggest a different error during the import. - The
modules
property returns the the value of_modules
- so we would be changing the return value fromNone
to{}
if the import failed. Which means if callers are checking specifically forNone
, this would break their code.
If the tank authentication is used in a headless mode, it will raise an unhandled exception as shown below.
Using the
get
method to retrieve modules in qt_importer.py will avoid the index error and is also just simpler.