[BUG] `TypeError: 'ContentFile' object is not subscriptable` in mod_upload when creating GitHub issue
維護者通常 4 天內回覆
評估
研究方向
Read mod_upload/controllers.py around lines 259–267 and the existing test in tests/test_upload/test_controllers.py around lines 80–83. Reproduce the failure with PyGithub’s ContentFile return type, then update the handling and test it for both supported encodings. Done when reporting a GitHub issue completes without a 500 error; pull request #1262 is already open.
由索引模型根據 Issue 內容生成。
描述
In mod_upload/controllers.py, when a user completes an upload with the option to report a GitHub issue (form.report.data == 'y'), the platform attempts to fetch .github/ISSUE_TEMPLATE.md via PyGithub:
# mod_upload/controllers.py:259-264
response = repository.get_contents(".github/ISSUE_TEMPLATE.md")
encoded_data = response['content']
if response['encoding'] == 'base64':
data = base64.b64decode(encoded_data).decode()
Since the migration to PyGithub in commit 74ef5e2 (#774), repository.get_contents(...) returns an instance of github.ContentFile.ContentFile rather than a dictionary.
Because ContentFile does not implement __getitem__, indexing response['content'] raises an unhandled TypeError: 'ContentFile' object is not subscriptable, causing the HTTP request to abort with a 500 Internal Server Error.
In addition, if response.encoding != 'base64', data is left unassigned, resulting in an UnboundLocalError on line 267 (data.replace(...)).
Why Existing Unit Tests Did Not Catch This
In tests/test_upload/test_controllers.py (lines 80–83), the test mocks get_contents to return a plain Python dictionary:
mock_github.return_value.get_repo.return_value.get_contents = mock.MagicMock(return_value={
"content": base64.b64encode("test".encode("ascii")).decode("ascii"),
"encoding": 'base64'
})
Because the mock returned a dictionary instead of a ContentFile object, the unit test passes even though real execution against PyGithub fails.
Minimal Reproduction Script
Run the following against PyGithub (as installed from requirements.txt):
from unittest.mock import MagicMock
from github.ContentFile import ContentFile
# PyGithub's actual return type for get_contents()
cf = ContentFile(
requester=MagicMock(),
headers={},
attributes={"content": "dGVzdA==", "encoding": "base64"},
completed=True
)
# Line 260 of mod_upload/controllers.py:
encoded_data = cf['content']
Output:
Traceback (most recent call last):
File "repro.py", line 12, in <module>
encoded_data = cf['content']
~~^^^^^^^^^^^
TypeError: 'ContentFile' object is not subscriptable
Proposed Fix
PyGithub's ContentFile provides a decoded_content property that automatically handles decoding:
response = repository.get_contents(".github/ISSUE_TEMPLATE.md")
data = response.decoded_content.decode('utf-8')
I would be happy to submit a pull request with the fix and an updated unit test using a proper ContentFile mock if this looks good to maintainers.
- 主要語言
- Python
- 星號
- 31
- 分支
- 92
- 平均合併
- 4 天 9 小時
- 30 天內合併 PR
- 29
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
CCExtractor/sample-platform 的其他 Issue
-
[BUG] Unhandled AttributeError on empty database queries for GeneralData and CCExtractorVersion可能已有人在做 @Aspirant200715 於 9 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 35/100
CCExtractor/sample-platform#1275 · 1 則留言 ·
維護者通常 4 天內回覆
-
難度 3/5 1-2 天 新手友好度 72/100
CCExtractor/sample-platform#1273 · 1 則留言 ·
維護者通常 4 天內回覆
-
[BUG] Missing timeouts on 5 outbound HTTP calls outside mod_ci (follow-up to #943)可能已有人在做 @devnishantt 於 54 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 84/100
CCExtractor/sample-platform#1192 · 2 則留言 ·
維護者通常 4 天內回覆
-
[Feature Request] Improve comparison algorithm to handle frame offsets intelligently (GSoC Qualification)可能重新可做 關聯的 PR 已關閉且未合併。 未關閉
難度 4/5 3-5 天 新手友好度 48/100
CCExtractor/sample-platform#1079 ·
維護者通常 4 天內回覆
-
[BUG] never-worked baseline handling is not fully integrated into trusted CI reporting可能重新可做 關聯的 PR 已關閉且未合併。 未關閉
難度 4/5 3-5 天 新手友好度 42/100
CCExtractor/sample-platform#1057 · 2 則留言 ·
維護者通常 4 天內回覆
查看 CCExtractor/sample-platform 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 72/100
pathintegral-institute/mcpm.sh#568 ·
維護者通常 21 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
invarislabs/invaris-agentsec#98 ·
維護者通常 1 天內回覆
-
5-minutes beginner-friendly documentation good first issue hacktoberfest help wanted no-code-needed python
難度 1/5 1 小時以內 新手友好度 65/100
Huerte/algo-solutions-python#364 ·
-
難度 1/5 1 小時以內 新手友好度 85/100
Becksteinlab/imdclient#161 ·
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆