CodeSampleX

示例

llvmlite 0.49.0rc1: llvmlite

已验证示例 — pypi llvmlite 0.49.0rc1: llvmlite. contract 在 python 3.12 · linux debian/x64 · docker 上运行并通过: llvmlite module exposes __version__ string and binding…

sha256:1994dcfaa8cba0cbd4a2f0559caded2f1946fddaec0186828a6f91d5370a8c03

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify llvmlite in pkg:pypi/llvmlite@0.49.0rc1
包
符号
  • llvmlite
创建时间
2026-09-17T16:27:17Z

契约

  1. llvmlite module exposes __version__ string and binding llvm_version_info tuple
  2. llvmlite.binding.initialize raises RuntimeError notifying that LLVM initialization is handled automatically
  3. llvmlite.binding initializes native target and asmprinter for default triple
  4. llvmlite.ir creates module, function, basic block and builds integer addition instructions
  5. llvmlite.binding.parse_assembly parses LLVM IR string and verifies module without errors
  6. llvmlite.binding.create_mcjit_compiler compiles function and executes via ctypes returning expected result

文件

  • PROMPT.md
  • csx.json
  • requirements.txt
  • sample.py
  • 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 llvmlite in pkg:pypi/llvmlite@0.49.0rc1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/llvmlite@0.49.0rc1
Demonstrate these symbols/APIs:
  - llvmlite

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:23ae6e42c10bf04ebcf76a0e7305eaa76307a0c4d61ee4e3e0d37413ca8ba89e","contract":["llvmlite module exposes __version__ string and binding llvm_version_info tuple","llvmlite.binding.initialize raises RuntimeError notifying that LLVM initialization is handled automatically","llvmlite.binding initializes native target and asmprinter for default triple","llvmlite.ir creates module, function, basic block and builds integer addition instructions","llvmlite.binding.parse_assembly parses LLVM IR string and verifies module without errors","llvmlite.binding.create_mcjit_compiler compiles function and executes via ctypes returning expected result"],"goal":"verify llvmlite in pkg:pypi/llvmlite@0.49.0rc1","kind":"HOW","packages":["pkg:pypi/llvmlite@0.49.0rc1"],"schemaVersion":1,"symbols":["llvmlite"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/llvmlite@0.49.0rc1"],"schemaVersion":1,"subject":"pkg:pypi/llvmlite@0.49.0rc1","symbols":["llvmlite"],"verifierAdapter":"python@1"}
requirements.txt
llvmlite==0.49.0rc1
sample.py
"""Sample demonstrating IR generation and execution using llvmlite."""

import ctypes
from typing import Tuple
import llvmlite
import llvmlite.binding as llvm
import llvmlite.ir as ir


def get_llvmlite_version() -> str:
    """Return llvmlite package version string."""
    return llvmlite.__version__


def get_llvm_version() -> Tuple[int, ...]:
    """Return LLVM version tuple."""
    return llvm.llvm_version_info


def build_add_ir() -> str:
    """Construct an LLVM IR module containing an integer addition function."""
    mod = ir.Module(name="adder_module")
    i32 = ir.IntType(32)
    fnty = ir.FunctionType(i32, [i32, i32])
    func = ir.Function(mod, fnty, name="add_two")
    block = func.append_basic_block(name="entry")
    builder = ir.IRBuilder(block)

    result = builder.add(func.args[0], func.args[1], name="sum_val")
    builder.ret(result)
    return str(mod)


def compile_and_run_adder(a: int, b: int) -> int:
    """Parse, verify, JIT-compile the addition function, and execute it."""
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()

    ir_code = build_add_ir()
    parsed_mod = llvm.parse_assembly(ir_code)
    parsed_mod.verify()

    target = llvm.Target.from_default_triple()
    target_machine = target.create_target_machine()
    backing_mod = llvm.parse_assembly("")
    engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
    engine.add_module(parsed_mod)
    engine.finalize_object()
    engine.run_static_constructors()

    func_ptr = engine.get_function_address("add_two")
    c_func = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_int32, ctypes.c_int32)(func_ptr)
    return int(c_func(a, b))
spec.json
{
  "schemaVersion": 1,
  "goal": "verify llvmlite in pkg:pypi/llvmlite@0.49.0rc1",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/llvmlite@0.49.0rc1"
  ],
  "symbols": [
    "llvmlite"
  ]
}
test/contract.py
"""Contract verification tests for llvmlite."""

import os
import sys

# Ensure project root is in sys.path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import llvmlite
import llvmlite.binding as llvm
import llvmlite.ir as ir
import sample


def test_version_info() -> None:
    """Assertion 1: llvmlite module exposes __version__ string and binding llvm_version_info tuple."""
    ver = sample.get_llvmlite_version()
    assert isinstance(ver, str)
    assert len(ver) > 0
    assert ver == llvmlite.__version__

    llvm_ver = sample.get_llvm_version()
    assert isinstance(llvm_ver, tuple)
    assert len(llvm_ver) >= 2
    assert llvm_ver == llvm.llvm_version_info


def test_initialization_deprecation() -> None:
    """Assertion 2: llvmlite.binding.initialize raises RuntimeError notifying that LLVM initialization is handled automatically."""
    raised = False
    try:
        llvm.initialize()
    except RuntimeError as exc:
        raised = True
        assert "deprecated" in str(exc) or "automatically" in str(exc)
    assert raised, "Expected llvm.initialize() to raise RuntimeError"


def test_native_target_initialization() -> None:
    """Assertion 3: llvmlite.binding initializes native target and asmprinter for default triple."""
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()
    target = llvm.Target.from_default_triple()
    assert target is not None
    assert isinstance(target.name, str)
    assert len(target.name) > 0

    machine = target.create_target_machine()
    assert machine is not None
    assert isinstance(machine.triple, str)
    assert len(machine.triple) > 0


def test_ir_module_generation() -> None:
    """Assertion 4: llvmlite.ir creates module, function, basic block and builds integer addition instructions."""
    ir_text = sample.build_add_ir()
    assert isinstance(ir_text, str)
    assert 'ModuleID = "adder_module"' in ir_text
    assert "add_two" in ir_text
    assert "add i32" in ir_text
    assert "ret i32" in ir_text


def test_assembly_parsing_and_verification() -> None:
    """Assertion 5: llvmlite.binding.parse_assembly parses LLVM IR string and verifies module without errors."""
    ir_text = sample.build_add_ir()
    parsed_mod = llvm.parse_assembly(ir_text)
    assert parsed_mod is not None
    assert parsed_mod.verify() is None

    fn = parsed_mod.get_function("add_two")
    assert fn is not None
    assert fn.name == "add_two"


def test_jit_compilation_and_execution() -> None:
    """Assertion 6: llvmlite.binding.create_mcjit_compiler compiles function and executes via ctypes returning expected result."""
    result = sample.compile_and_run_adder(17, 25)
    assert result == 42

    result2 = sample.compile_and_run_adder(-10, 30)
    assert result2 == 20


def main() -> None:
    test_version_info()
    test_initialization_deprecation()
    test_native_target_initialization()
    test_ir_module_generation()
    test_assembly_parsing_and_verification()
    test_jit_compilation_and_execution()
    print("All contract assertions passed.")


if __name__ == "__main__":
    main()

原始种子者

匿名