Skip to content

优化:inno路由文件读取对损坏行增加容错处理#142

Open
Kailigithub wants to merge 1 commit into
datawhalechina:mainfrom
Kailigithub:optimize/inno-file-read-parse-error-tolerance
Open

优化:inno路由文件读取对损坏行增加容错处理#142
Kailigithub wants to merge 1 commit into
datawhalechina:mainfrom
Kailigithub:optimize/inno-file-read-parse-error-tolerance

Conversation

@Kailigithub

Copy link
Copy Markdown
Collaborator

优化说明

/api/inno/get_tm/{user_id}/api/inno/get_pr/{user_id} 在读取 static/tm/t{userid}.txtstatic/tm/f{userid}.txt 时使用裸的 [ast.literal_eval(x) for x in lines]。如果文件中有任何一行因写入中断、并发覆盖或编码错误导致 ast.literal_evalValueError / SyntaxError,整个接口会返回 500,用户的时间管理数据就完全无法访问。

add_study_time 已经使用相同的容错模式(line 121)。本次改动把另外三处裸调用统一收敛到新增的 _safe_literal_eval_lines 辅助函数:解析失败的行通过 logger.warning 记录后跳过,合法行正常返回。

行为差异

  • 合法文件:与原行为完全一致
  • 含损坏行的文件:跳过损坏行,返回剩余合法行(之前会 500)
  • 空文件 / 全损坏:返回 [](之前会 500)

验证

py_compile 通过。/tmp/test_inno_safe_parse.py 覆盖五种情况:

  1. 全部合法输入全部解析
  2. 损坏行被跳过,合法行保留
  3. 空输入返回空列表
  4. 全部损坏返回空列表
  5. 合法输入与原裸 ast.literal_eval 行为等价

并通过 git-stash 三步验证:原代码在损坏行上抛 SyntaxError,新代码正常返回。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant