Merge ~mateus-morais/lazr.restfulclient:fix-python-3.12 into lazr.restfulclient:main

Proposed by Mateus Rodrigues de Morais
Status: Merged
Merged at revision: 285acf59c7a9d9c843627b8724a94be90f3fed3f
Proposed branch: ~mateus-morais/lazr.restfulclient:fix-python-3.12
Merge into: lazr.restfulclient:main
Diff against target: 16 lines (+4/-1)
1 file modified
src/lazr/restfulclient/authorize/oauth.py (+4/-1)
Reviewer Review Type Date Requested Status
Simone Pelosi Approve
Review via email: mp+456978@code.launchpad.net

Description of the change

Test for read_file before falling back to readfp using hasattr.

Since the fallback parameter in getattr() isn't lazily loaded, we get an AttributeError when readfp() isn't available (e.g. Python 3.12).

With hasattr(), we are able to check for the presence of read_file(). If not there, we safely fallback to readfp() instead.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

Thanks for the merge request. A colleague will look into this shortly.

Revision history for this message
Simone Pelosi (pelpsi) wrote :

LGTM! Thank you Mateus for your contribution!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lazr/restfulclient/authorize/oauth.py b/src/lazr/restfulclient/authorize/oauth.py
2index 09abf0b..c8dfc6b 100644
3--- a/src/lazr/restfulclient/authorize/oauth.py
4+++ b/src/lazr/restfulclient/authorize/oauth.py
5@@ -191,7 +191,10 @@ class OAuthAuthorizer(HttpAuthorizer):
6 """
7 # Attempt to load the access token from the file.
8 parser = SafeConfigParser()
9- reader = getattr(parser, "read_file", parser.readfp)
10+ if hasattr(parser, "read_file"):
11+ reader = parser.read_file
12+ else:
13+ reader = parser.readfp
14 reader(readable_file)
15 # Check the version number and extract the access token and
16 # secret. Then convert these to the appropriate instances.

Subscribers

People subscribed via source and target branches