CodeSampleX

示例

urllib3 2.7.0: exceptions.URLSchemeUnknown

已验证示例 — pypi urllib3 2.7.0: exceptions.URLSchemeUnknown. contract 在 python 3.12 · linux debian/x64 · docker 上运行并通过: assert URLSchemeUnknown inherits from…

sha256:5fbd9caacb21de3e2bbfae278c52f7de787ab8d59ff7a97ad019d2d03491c1f3

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-06

案例

HOW
目标
verify urllib3.exceptions.URLSchemeUnknown in pkg:pypi/urllib3@2.7.0
包
符号
  • urllib3.exceptions.URLSchemeUnknown
创建时间
2026-09-06T00:39:28Z

契约

  1. assert URLSchemeUnknown inherits from LocationValueError, ValueError, and HTTPError
  2. assert URLSchemeUnknown stores the unsupported scheme string in its scheme attribute
  3. assert URLSchemeUnknown string representation formats as Not supported URL scheme followed by scheme
  4. assert PoolManager.connection_from_url raises URLSchemeUnknown when given an unsupported scheme
  5. assert PoolManager.connection_from_context raises URLSchemeUnknown when request context contains an unsupported scheme
  6. assert urllib3.request raises URLSchemeUnknown when called with an unsupported scheme URL

文件

  • PROMPT.md
  • csx.json
  • pyproject.toml
  • 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 urllib3.exceptions.URLSchemeUnknown in pkg:pypi/urllib3@2.7.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/urllib3@2.7.0
Demonstrate these symbols/APIs:
  - urllib3.exceptions.URLSchemeUnknown

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:083f2e6905da0dfd5e1d814f65dc0e09b34f2b6f6b9acd4603721dba76436b18","contract":["assert URLSchemeUnknown inherits from LocationValueError, ValueError, and HTTPError","assert URLSchemeUnknown stores the unsupported scheme string in its scheme attribute","assert URLSchemeUnknown string representation formats as Not supported URL scheme followed by scheme","assert PoolManager.connection_from_url raises URLSchemeUnknown when given an unsupported scheme","assert PoolManager.connection_from_context raises URLSchemeUnknown when request context contains an unsupported scheme","assert urllib3.request raises URLSchemeUnknown when called with an unsupported scheme URL"],"goal":"verify urllib3.exceptions.URLSchemeUnknown in pkg:pypi/urllib3@2.7.0","kind":"HOW","packages":["pkg:pypi/urllib3@2.7.0"],"schemaVersion":1,"symbols":["urllib3.exceptions.URLSchemeUnknown"]},"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/urllib3@2.7.0"],"schemaVersion":1,"subject":"pkg:pypi/urllib3@2.7.0","symbols":["urllib3.exceptions.URLSchemeUnknown"],"verifierAdapter":"python@1"}
pyproject.toml
[project]
name = "verify-urllib3-urlschemeunknown"
version = "0.1.0"
dependencies = [
    "urllib3==2.7.0",
]
requirements.txt
urllib3==2.7.0
sample.py
"""Demonstration of urllib3 URL scheme validation and URLSchemeUnknown exception handling."""

from typing import Any, Dict, Optional
import urllib3
from urllib3.exceptions import HTTPError, LocationValueError, URLSchemeUnknown


def get_connection_pool_for_url(
    pool_manager: urllib3.PoolManager,
    url: str,
) -> urllib3.connectionpool.HTTPConnectionPool:
    """Acquire a connection pool from PoolManager for the specified URL.

    Raises URLSchemeUnknown if the URL has an unsupported scheme.
    """
    return pool_manager.connection_from_url(url)


def get_connection_pool_for_context(
    pool_manager: urllib3.PoolManager,
    request_context: Dict[str, Any],
) -> urllib3.connectionpool.HTTPConnectionPool:
    """Acquire a connection pool based on request context dictionary.

    Raises URLSchemeUnknown if the scheme in request_context is not supported.
    """
    return pool_manager.connection_from_context(request_context)


def dispatch_request(method: str, url: str) -> Optional[urllib3.response.BaseHTTPResponse]:
    """Dispatch an HTTP request via urllib3.request.

    Raises URLSchemeUnknown if the target URL has an unsupported scheme.
    """
    return urllib3.request(method, url)


def is_url_scheme_unknown_error(exc: Exception) -> bool:
    """Check if an exception is an instance of URLSchemeUnknown and its expected base classes."""
    return (
        isinstance(exc, URLSchemeUnknown)
        and isinstance(exc, LocationValueError)
        and isinstance(exc, ValueError)
        and isinstance(exc, HTTPError)
    )
spec.json
{
  "schemaVersion": 1,
  "goal": "verify urllib3.exceptions.URLSchemeUnknown in pkg:pypi/urllib3@2.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/urllib3@2.7.0"
  ],
  "symbols": [
    "urllib3.exceptions.URLSchemeUnknown"
  ]
}
test/contract.py
"""Contract verification test for urllib3.exceptions.URLSchemeUnknown in pkg:pypi/urllib3@2.7.0."""

import os
import sys

# Ensure project root is on sys.path for local module imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import urllib3
from urllib3.exceptions import HTTPError, LocationValueError, URLSchemeUnknown
from sample import (
    dispatch_request,
    get_connection_pool_for_context,
    get_connection_pool_for_url,
    is_url_scheme_unknown_error,
)


def test_inheritance_hierarchy() -> None:
    """Assertion 1: assert URLSchemeUnknown inherits from LocationValueError, ValueError, and HTTPError."""
    assert issubclass(URLSchemeUnknown, LocationValueError), "URLSchemeUnknown must inherit from LocationValueError"
    assert issubclass(URLSchemeUnknown, ValueError), "URLSchemeUnknown must inherit from ValueError"
    assert issubclass(URLSchemeUnknown, HTTPError), "URLSchemeUnknown must inherit from HTTPError"


def test_scheme_attribute() -> None:
    """Assertion 2: assert URLSchemeUnknown stores the unsupported scheme string in its scheme attribute."""
    unsupported_scheme = "custom"
    err = URLSchemeUnknown(unsupported_scheme)
    assert err.scheme == unsupported_scheme, f"Expected scheme '{unsupported_scheme}', got '{err.scheme}'"
    assert is_url_scheme_unknown_error(err), "err must be recognized by hierarchy check"


def test_string_representation() -> None:
    """Assertion 3: assert URLSchemeUnknown string representation formats as Not supported URL scheme followed by scheme."""
    unsupported_scheme = "ftp"
    err = URLSchemeUnknown(unsupported_scheme)
    expected_message = f"Not supported URL scheme {unsupported_scheme}"
    assert str(err) == expected_message, f"Expected '{expected_message}', got '{str(err)}'"


def test_pool_manager_connection_from_url() -> None:
    """Assertion 4: assert PoolManager.connection_from_url raises URLSchemeUnknown when given an unsupported scheme."""
    pm = urllib3.PoolManager()
    unsupported_url = "ftp://example.com/resource"
    raised = False
    try:
        get_connection_pool_for_url(pm, unsupported_url)
    except URLSchemeUnknown as exc:
        raised = True
        assert exc.scheme == "ftp", f"Expected scheme 'ftp', got '{exc.scheme}'"
        assert is_url_scheme_unknown_error(exc), "Exception must satisfy inheritance checks"

    assert raised, "PoolManager.connection_from_url should have raised URLSchemeUnknown"


def test_pool_manager_connection_from_context() -> None:
    """Assertion 5: assert PoolManager.connection_from_context raises URLSchemeUnknown when request context contains an unsupported scheme."""
    pm = urllib3.PoolManager()
    context = {"scheme": "gopher", "host": "example.com"}
    raised = False
    try:
        get_connection_pool_for_context(pm, context)
    except URLSchemeUnknown as exc:
        raised = True
        assert exc.scheme == "gopher", f"Expected scheme 'gopher', got '{exc.scheme}'"
        assert is_url_scheme_unknown_error(exc), "Exception must satisfy inheritance checks"

    assert raised, "PoolManager.connection_from_context should have raised URLSchemeUnknown"


def test_urllib3_request_unsupported_scheme() -> None:
    """Assertion 6: assert urllib3.request raises URLSchemeUnknown when called with an unsupported scheme URL."""
    unsupported_url = "ssh://example.com/target"
    raised = False
    try:
        dispatch_request("GET", unsupported_url)
    except URLSchemeUnknown as exc:
        raised = True
        assert exc.scheme == "ssh", f"Expected scheme 'ssh', got '{exc.scheme}'"
        assert is_url_scheme_unknown_error(exc), "Exception must satisfy inheritance checks"

    assert raised, "urllib3.request should have raised URLSchemeUnknown"


def main() -> None:
    test_inheritance_hierarchy()
    test_scheme_attribute()
    test_string_representation()
    test_pool_manager_connection_from_url()
    test_pool_manager_connection_from_context()
    test_urllib3_request_unsupported_scheme()
    print("All contract assertions passed.")


if __name__ == "__main__":
    main()

原始种子者

匿名