CodeSampleX

示例

idna 3.11: decode

已验证示例 — pypi idna 3.11: decode. contract 在 python 3.12 · linux alpine/x64 · docker 上运行并通过: idna.decode(s) decodes ASCII-compatible encoded (ACE/Punycode)…

sha256:cf2476b91a1ff3ec7d8954be7b24d834de5079fe2c19fbde9bdd891d403b9030

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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-08-26
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-07

案例

HOW
目标
verify idna.decode in pkg:pypi/idna@3.11
包
符号
  • idna.decode
创建时间
2026-08-26T04:36:23Z

契约

  1. idna.decode(s) decodes ASCII-compatible encoded (ACE/Punycode) domain name strings to Unicode
  2. idna.decode(s) accepts bytes and bytearray input representations
  3. idna.decode(s) returns plain ASCII domain names unchanged
  4. idna.decode(s) preserves trailing root domain dot
  5. idna.decode(s) decodes multiple internationalized domain labels separated by dots
  6. idna.decode(s) raises idna.IDNAError on malformed Punycode labels or leading dots

文件

  • PROMPT.md
  • csx.json
  • requirements.txt
  • spec.json
  • src/domain_decoder.py
  • 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 idna.decode in pkg:pypi/idna@3.11
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/idna@3.11
Demonstrate these symbols/APIs:
  - idna.decode

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:51b83d55bd7c99d46ace6e1da3dd0fcac8ce3e38b18bab3668fb7dba5a67994c","contract":["idna.decode(s) decodes ASCII-compatible encoded (ACE/Punycode) domain name strings to Unicode","idna.decode(s) accepts bytes and bytearray input representations","idna.decode(s) returns plain ASCII domain names unchanged","idna.decode(s) preserves trailing root domain dot","idna.decode(s) decodes multiple internationalized domain labels separated by dots","idna.decode(s) raises idna.IDNAError on malformed Punycode labels or leading dots"],"goal":"verify idna.decode in pkg:pypi/idna@3.11","kind":"HOW","packages":["pkg:pypi/idna@3.11"],"schemaVersion":1,"symbols":["idna.decode"]},"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/idna@3.11"],"schemaVersion":1,"subject":"pkg:pypi/idna@3.11","symbols":["idna.decode"],"verifierAdapter":"python@1"}
requirements.txt
idna==3.11
spec.json
{
  "schemaVersion": 1,
  "goal": "verify idna.decode in pkg:pypi/idna@3.11",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/idna@3.11"
  ],
  "symbols": [
    "idna.decode"
  ]
}
src/domain_decoder.py
"""Domain name decoding module demonstrating idna.decode."""

from typing import Union
import idna


def decode_domain(domain: Union[str, bytes, bytearray], uts46: bool = False, strict: bool = False) -> str:
    """Decode an ASCII-compatible encoded (ACE/Punycode) domain name to Unicode.

    Args:
        domain: Domain name as str, bytes, or bytearray.
        uts46: Enable UTS#46 compatibility processing.
        strict: Enforce strict IDNA2008 validation rules.

    Returns:
        Decoded Unicode domain name.

    Raises:
        idna.IDNAError: If domain contains invalid Punycode or violates IDNA rules.
    """
    return idna.decode(domain, uts46=uts46, strict=strict)
test/contract.py
"""Contract verification test for idna.decode."""

import os
import sys

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

import idna
from src.domain_decoder import decode_domain


def test_decode_punycode_str():
    # idna.decode(s) decodes ASCII-compatible encoded (ACE/Punycode) domain name strings to Unicode
    ace_domain = "xn--bcher-kva.example"
    expected = "bücher.example"
    result = idna.decode(ace_domain)
    assert result == expected, f"Expected {expected}, got {result}"

    wrapped = decode_domain(ace_domain)
    assert wrapped == expected, f"Expected {expected}, got {wrapped}"


def test_decode_bytes_and_bytearray():
    # idna.decode(s) accepts bytes and bytearray input representations
    expected = "bücher.example"

    bytes_input = b"xn--bcher-kva.example"
    result_bytes = idna.decode(bytes_input)
    assert result_bytes == expected, f"Expected {expected}, got {result_bytes}"

    bytearray_input = bytearray(b"xn--bcher-kva.example")
    result_bytearray = idna.decode(bytearray_input)
    assert result_bytearray == expected, f"Expected {expected}, got {result_bytearray}"

    assert decode_domain(bytes_input) == expected
    assert decode_domain(bytearray_input) == expected


def test_decode_plain_ascii():
    # idna.decode(s) returns plain ASCII domain names unchanged
    ascii_domain = "example.com"
    result = idna.decode(ascii_domain)
    assert result == "example.com", f"Expected 'example.com', got {result}"
    assert decode_domain(ascii_domain) == "example.com"


def test_decode_preserve_trailing_dot():
    # idna.decode(s) preserves trailing root domain dot
    trailing = "xn--bcher-kva.example."
    assert idna.decode(trailing) == "bücher.example."
    assert decode_domain(trailing) == "bücher.example."


def test_decode_multiple_labels():
    # idna.decode(s) decodes multiple internationalized domain labels separated by dots
    multi_label = "xn--mnchen-3ya.xn--bcher-kva.de"
    expected = "münchen.bücher.de"
    result = idna.decode(multi_label)
    assert result == expected, f"Expected {expected}, got {result}"
    assert decode_domain(multi_label) == expected


def test_decode_malformed_punycode_raises_idna_error():
    # idna.decode(s) raises idna.IDNAError on malformed Punycode labels or leading dots
    invalid_inputs = ["xn--", "xn--invalid!label", "..", ".xn--bcher-kva.example"]
    for item in invalid_inputs:
        try:
            idna.decode(item)
            assert False, f"Expected idna.IDNAError for {item!r}"
        except idna.IDNAError:
            pass

        try:
            decode_domain(item)
            assert False, f"Expected idna.IDNAError for decode_domain({item!r})"
        except idna.IDNAError:
            pass


def main():
    test_decode_punycode_str()
    test_decode_bytes_and_bytearray()
    test_decode_plain_ascii()
    test_decode_preserve_trailing_dot()
    test_decode_multiple_labels()
    test_decode_malformed_punycode_raises_idna_error()
    print("Contract verification for idna.decode passed.")
    return 0


if __name__ == "__main__":
    sys.exit(main())

原始种子者

匿名