[PR #991] [MERGED] Changed cache_handler.py to utilize Python's Context Management Protocol #1113

Closed
opened 2026-02-28 00:03:40 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/spotipy-dev/spotipy/pull/991
Author: @508chris
Created: 6/12/2023
Status: Merged
Merged: 1/22/2025
Merged by: @stephanebruckert

Base: masterHead: 508chris-branch


📝 Commits (10+)

  • 87e4c65 Changed CacheHandler methods get_cached_token and save_token_to_cache to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
  • cf9d89d Updated changelog
  • 53adef2 Merge branch 'master' into 508chris-branch
  • 78449ba Back to IOError
  • 73ce65f Update docs for current_user_top_artists (#1171)
  • 5799ba3 Changed CacheHandler methods get_cached_token and save_token_to_cache to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
  • 5505f33 Updated changelog
  • d1c1294 Fixed cachehandler mocking in unit tests.
  • ee47dad Merge branch 'master' into 508chris-branch
  • e830607 pep8

📊 Changes

7 files changed (+121 additions, -64 deletions)

View changed files

📝 .github/workflows/integration_tests.yml (+0 -1)
📝 .github/workflows/unit_tests.yml (+0 -1)
📝 CHANGELOG.md (+51 -28)
examples (+0 -1)
📝 spotipy/cache_handler.py (+8 -8)
📝 spotipy/util.py (+1 -0)
📝 tests/unit/test_oauth.py (+61 -25)

📄 Description

I changed two methods within the CacheHandler class, get_cached_token and save_token_to_cache. Instead of opening and closing files manually, I updated these methods to use Python's "with" statement to keep up with good practice in Python programming.
This will also alleviate any errors that occur before the file is closed which would result in allocated resources.
Using the with statement, it simplifies the code as well as provides more reliability to the code base.

I also added an except clause to the get_cached_token method to handle any json decode errors.

Let me know if you have any questions at all.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/spotipy-dev/spotipy/pull/991 **Author:** [@508chris](https://github.com/508chris) **Created:** 6/12/2023 **Status:** ✅ Merged **Merged:** 1/22/2025 **Merged by:** [@stephanebruckert](https://github.com/stephanebruckert) **Base:** `master` ← **Head:** `508chris-branch` --- ### 📝 Commits (10+) - [`87e4c65`](https://github.com/spotipy-dev/spotipy/commit/87e4c65ca41cd9d80470ab84b1fdc3a98d4f12c0) Changed CacheHandler methods get_cached_token and save_token_to_cache to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request. - [`cf9d89d`](https://github.com/spotipy-dev/spotipy/commit/cf9d89de7f5010be5a8920c0f5ba1c99c9a98701) Updated changelog - [`53adef2`](https://github.com/spotipy-dev/spotipy/commit/53adef2312be19d6f9fcc02b90084a35275ed484) Merge branch 'master' into 508chris-branch - [`78449ba`](https://github.com/spotipy-dev/spotipy/commit/78449ba166a9e7e3b5363d812bffad6ca88f4401) Back to IOError - [`73ce65f`](https://github.com/spotipy-dev/spotipy/commit/73ce65f5a912b969a10fc06109d24f472aeb986a) Update docs for `current_user_top_artists` (#1171) - [`5799ba3`](https://github.com/spotipy-dev/spotipy/commit/5799ba3b5da7f5c0c2bbf6a3af1e2174d7e6ec45) Changed CacheHandler methods get_cached_token and save_token_to_cache to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request. - [`5505f33`](https://github.com/spotipy-dev/spotipy/commit/5505f3334f525491ac5789bb757bec70cc167af2) Updated changelog - [`d1c1294`](https://github.com/spotipy-dev/spotipy/commit/d1c129449b9d9e01e3e5ba108cfefbf89135bc85) Fixed cachehandler mocking in unit tests. - [`ee47dad`](https://github.com/spotipy-dev/spotipy/commit/ee47dad6eaf2c02bb53447caf90a2c1289b07de4) Merge branch 'master' into 508chris-branch - [`e830607`](https://github.com/spotipy-dev/spotipy/commit/e830607bbf832e31d5bd3e597e18ef4dba2be45a) pep8 ### 📊 Changes **7 files changed** (+121 additions, -64 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/integration_tests.yml` (+0 -1) 📝 `.github/workflows/unit_tests.yml` (+0 -1) 📝 `CHANGELOG.md` (+51 -28) ➖ `examples` (+0 -1) 📝 `spotipy/cache_handler.py` (+8 -8) 📝 `spotipy/util.py` (+1 -0) 📝 `tests/unit/test_oauth.py` (+61 -25) </details> ### 📄 Description I changed two methods within the CacheHandler class, get_cached_token and save_token_to_cache. Instead of opening and closing files manually, I updated these methods to use Python's "with" statement to keep up with good practice in Python programming. This will also alleviate any errors that occur before the file is closed which would result in allocated resources. Using the with statement, it simplifies the code as well as provides more reliability to the code base. I also added an except clause to the get_cached_token method to handle any json decode errors. Let me know if you have any questions at all. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 00:03:40 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#1113
No description provided.