CodeSampleX

示例

greenlet 3.5.4: greenlet.parent

已验证示例 — pypi greenlet 3.5.4: greenlet.parent. contract 在 python 3.12 · linux alpine/x64 · docker 上运行并通过: assert greenlet.greenlet default parent is the…

sha256:3cd72ec2230ef46dee673e23871388b26db13461dc4455c9b9c6dd1a3634da3d

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 python 3.12 linux 24 · alpine · musl x64 python 3.12 python pip 24

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-alpine@sha256:d09d15e60962…
2026-09-01

案例

HOW
目标
verify greenlet.greenlet.parent in pkg:pypi/greenlet@3.5.4
符号
  • greenlet.greenlet.parent
环境
python 3.12
创建时间
2026-09-01T12:13:59Z

契约

  1. assert greenlet.greenlet default parent is the creating greenlet from greenlet.getcurrent()
  2. assert greenlet.greenlet accepts explicit parent argument in constructor and returns to specified parent on completion
  3. assert greenlet.greenlet.parent attribute is mutable and redirects completion control flow to new parent
  4. assert main greenlet parent is None and modifying main greenlet parent raises AttributeError or ValueError
  5. assert setting cyclic greenlet parent hierarchy raises ValueError
  6. assert setting non-greenlet or None parent raises TypeError or ValueError
  7. assert uncaught exceptions in child greenlet propagate to its parent greenlet

文件

  • PROMPT.md
  • csx.json
  • pyproject.toml
  • requirements.txt
  • spec.json
  • test/contract.py

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify greenlet.greenlet.parent in pkg:pypi/greenlet@3.5.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/greenlet@3.5.4
Demonstrate these symbols/APIs:
  - greenlet.greenlet.parent
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:0e8c6060db78a3dd2d9110228861758997e7370c7d808e238593d96935488e18","constraints":{"executionContext":"python"},"contract":["assert greenlet.greenlet default parent is the creating greenlet from greenlet.getcurrent()","assert greenlet.greenlet accepts explicit parent argument in constructor and returns to specified parent on completion","assert greenlet.greenlet.parent attribute is mutable and redirects completion control flow to new parent","assert main greenlet parent is None and modifying main greenlet parent raises AttributeError or ValueError","assert setting cyclic greenlet parent hierarchy raises ValueError","assert setting non-greenlet or None parent raises TypeError or ValueError","assert uncaught exceptions in child greenlet propagate to its parent greenlet"],"goal":"verify greenlet.greenlet.parent in pkg:pypi/greenlet@3.5.4","kind":"HOW","packages":["pkg:pypi/greenlet@3.5.4"],"schemaVersion":1,"symbols":["greenlet.greenlet.parent"]},"contractCommand":["python3","test/contract.py"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"pypi","executionContext":"python","language":"python","libc":"musl","moduleSystem":"standard","os":"linux","osVersionBucket":"24","packageManager":"pip","packageManagerVersion":"24.0","runtime":"python","runtimeVersion":"3.12","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/greenlet@3.5.4"],"schemaVersion":1,"subject":"pkg:pypi/greenlet@3.5.4","symbols":["greenlet.greenlet.parent"],"verifierAdapter":"python@1"}
pyproject.toml
[project]
name = "verify-greenlet-parent"
version = "0.1.0"
dependencies = [
    "greenlet==3.5.4",
]
requirements.txt
greenlet==3.5.4
spec.json
{
  "schemaVersion": 1,
  "goal": "verify greenlet.greenlet.parent in pkg:pypi/greenlet@3.5.4",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/greenlet@3.5.4"
  ],
  "symbols": [
    "greenlet.greenlet.parent"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.py
import greenlet


def test_default_parent_is_current():
    current = greenlet.getcurrent()

    def worker():
        return 42

    g = greenlet.greenlet(worker)
    assert g.parent is current
    res = g.switch()
    assert res == 42
    assert g.dead is True


def test_explicit_parent_constructor():
    events = []

    def p_func(*args):
        events.append("p_start")
        c_res = child.switch()
        events.append(f"p_end:{c_res}")
        return "p_done"

    def c_func():
        events.append("c_run")
        return "c_done"

    parent_g = greenlet.greenlet(p_func)
    child = greenlet.greenlet(c_func, parent=parent_g)
    assert child.parent is parent_g

    val = parent_g.switch()
    assert val == "p_done"
    assert events == ["p_start", "c_run", "p_end:c_done"]
    assert child.dead is True
    assert parent_g.dead is True


def test_reassign_parent_redirects_control():
    events = []

    def worker():
        events.append("worker_done")
        return "from_worker"

    def alt_parent(arg=None):
        events.append(f"alt_received:{arg}")
        return "alt_done"

    g = greenlet.greenlet(worker)
    alt_g = greenlet.greenlet(alt_parent)

    g.parent = alt_g
    assert g.parent is alt_g

    res = g.switch()
    assert res == "alt_done"
    assert events == ["worker_done", "alt_received:from_worker"]
    assert g.dead is True
    assert alt_g.dead is True


def test_main_greenlet_parent_is_none_and_immutable():
    main = greenlet.getcurrent()
    assert main.parent is None

    g = greenlet.greenlet(lambda: None)
    try:
        main.parent = g
        assert False, "Should have raised AttributeError/ValueError when setting parent of main greenlet"
    except (AttributeError, ValueError) as exc:
        assert isinstance(exc, (AttributeError, ValueError))


def test_cyclic_parent_raises_value_error():
    g1 = greenlet.greenlet(lambda: None)
    g2 = greenlet.greenlet(lambda: None)

    try:
        g1.parent = g1
        assert False, "Should have raised ValueError for self-referential parent"
    except ValueError as exc:
        assert isinstance(exc, ValueError)

    g2.parent = g1
    try:
        g1.parent = g2
        assert False, "Should have raised ValueError for cyclic parent chain"
    except ValueError as exc:
        assert isinstance(exc, ValueError)


def test_invalid_parent_type_raises_type_error():
    g = greenlet.greenlet(lambda: None)

    try:
        g.parent = "not_a_greenlet"
        assert False, "Should have raised TypeError for non-greenlet parent"
    except TypeError as exc:
        assert isinstance(exc, TypeError)

    try:
        g.parent = None
        assert False, "Should have raised TypeError/ValueError for None parent"
    except (TypeError, ValueError) as exc:
        assert isinstance(exc, (TypeError, ValueError))


def test_exception_propagates_to_parent():
    events = []

    class CustomError(Exception):
        pass

    def faulty_child():
        events.append("child_raising")
        raise CustomError("boom")

    child = greenlet.greenlet(faulty_child)
    assert child.parent is greenlet.getcurrent()

    try:
        child.switch()
        assert False, "Exception should have propagated to parent"
    except CustomError as exc:
        events.append("parent_caught")
        assert str(exc) == "boom"

    assert events == ["child_raising", "parent_caught"]
    assert child.dead is True


def main():
    test_default_parent_is_current()
    test_explicit_parent_constructor()
    test_reassign_parent_redirects_control()
    test_main_greenlet_parent_is_none_and_immutable()
    test_cyclic_parent_raises_value_error()
    test_invalid_parent_type_raises_type_error()
    test_exception_propagates_to_parent()
    print("All greenlet.greenlet.parent contract assertions passed.")


if __name__ == "__main__":
    main()

原始种子者

匿名