[BUG] `TypeError: 'ContentFile' object is not subscriptable` in mod_upload when creating GitHub issue

未关闭
#1,261 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

@garv-sanveria 已经在做这个了。

开始于 2026年11月5日。

  • #1262 来自 @garv-sanveria —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CCExtractor/sample-platform 的其他 Issue

查看 CCExtractor/sample-platform 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。