pyscg-0051 should use a context manager

未关闭 适合新手
#1 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@s19110 已经在做这个了。

开始于 2026年11月6日。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
68/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
security

调研方向

在 repository 中找到 pyscg-0051 指南及其合规代码示例。阅读相关规则,并检查其中如何说明资源处理;issue 中的上下文管理器示例展示了预期方向。更新指南,使资源在发生异常时得到释放,并确认修订后的示例和说明与该行为一致。

由索引模型根据 Issue 内容生成。

描述

bug

pyscg-0051 compliant solution will not release the resources if there is an exception between the resource creation and release. Most libraries (sqlalchemy, aiohttp, requests) uses context manager for that

for example:

"""Compliant Code Example"""

from contextlib import contextmanager


@contextmanager
def resource_pool():
    pool = []

    def append_resource(name):
        print(f"Allocating resource {name}")
        pool.append({"name": name, "active": True})  # Simulated resource

    try:
        yield pool, append_resource
    finally:
        # Properly release resources on exit
        for resource in pool:
            resource["active"] = False
        pool.clear()


with resource_pool() as (my_list, append_resource):
    append_resource("A")
    append_resource("B")

# Outside the context, resources are released
if not any(resource["active"] for resource in my_list):
    print("All resources released.")
主要语言
Python
星标
2
派生
2
平均合并
11 分钟
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

ossf/python-secure-coding-guide 的其他 Issue

查看 ossf/python-secure-coding-guide 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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