CodeSampleX

サンプル

python-dotenv 1.2.2: Pin python-dotenv string parsing: escape sequences expand only in double quotes, and inline comments require preceding whitespace only when unquoted.

検証済みサンプル — pypi python-dotenv 1.2.2: Pin python-dotenv string parsing: escape sequences expand only in double quotes, and inline comments require preceding…

sha256:fbecb11147dfb5e3583e9f299a4f2e4b8dd343ed86435313168cd2309772519a

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 python linux x64 python python pip

検証実行環境

環境 コントラクト ステージ 実行日
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-17
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18

ケース

HOW
ゴール
Pin python-dotenv string parsing: escape sequences expand only in double quotes, and inline comments require preceding whitespace only when unquoted.
パッケージ
シンボル
  • dotenv_values
  • load_dotenv
環境
python
作成日
2026-08-17T19:28:07Z

コントラクト

  1. dotenv_values expands \n to an actual newline only inside double quotes, and treats # as a comment only if it follows a space (when unquoted) or immediately follows a closing quote.
  2. assert unquoted and single-quoted \n parse as a literal backslash followed by n
  3. assert unquoted # without preceding space is parsed as part of the string value (val#3)
  4. assert unquoted # with preceding space is parsed as a comment (val #3 -> val)
  5. assert # immediately following a closing quote is parsed as a comment, even without preceding space

ファイル

  • NOTES.md
  • csx.json
  • requirements.txt
  • test/contract.py
  • test_dotenv.py

ソースアーティファクトをダウンロード (tar.gz)

ソース

NOTES.md
# python-dotenv String Parsing & Inline Comments

## 1. Search Result
A search for known solutions using `search_known_solution` returned a sample (`sha256:6032931e56a20b51b652c9f7c77e9a8fb979e1513bcd63e107668998f1f1f80f`) covering return semantics, `dotenv_values` isolation, and interpolation precedence. This sample covers a completely different behavior: string literal parsing, escape sequences, and inline comments.

## 2. What a Model Would Have Written Instead
A model would confidently expect that single quotes process escape sequences identically to double quotes (expanding `\n` to a newline). It would also assume that `#` consistently starts an inline comment, either always requiring a preceding space, or never requiring it, regardless of whether the preceding value is quoted.

## 3. How the Wrong Version Fails
If a model writes `VAR='\n'` expecting a newline, it fails silently with a green build because the application receives a literal backslash followed by an 'n' instead of a newline. Similarly, if a model expects `VAR=val#3` to strip `#3` as a comment (as it does in bash after a space), it fails because the `#` is parsed as part of the literal string. Conversely, assuming `VAR="val"#3` includes `#3` as part of the string (since it lacks a space) fails because `#` directly following a closing quote is always parsed as a comment.
csx.json
{"case":{"believed":"A model would expect single quotes to expand escape sequences identically to double quotes, and for inline comments to consistently require or not require preceding whitespace.","caseId":"case:sha256:85a72ad988b237a94747e82386bcd1647c856030502bf7f2498d16f3192c40c2","contract":["dotenv_values expands \\n to an actual newline only inside double quotes, and treats # as a comment only if it follows a space (when unquoted) or immediately follows a closing quote.","assert unquoted and single-quoted \\n parse as a literal backslash followed by n","assert unquoted # without preceding space is parsed as part of the string value (val#3)","assert unquoted # with preceding space is parsed as a comment (val #3 -\u003e val)","assert # immediately following a closing quote is parsed as a comment, even without preceding space"],"goal":"Pin python-dotenv string parsing: escape sequences expand only in double quotes, and inline comments require preceding whitespace only when unquoted.","kind":"HOW","packages":["pkg:pypi/python-dotenv@1.2.2"],"schemaVersion":1,"symbols":["dotenv_values","load_dotenv"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/python-dotenv@1.2.2"],"schemaVersion":1,"symbols":["dotenv_values","load_dotenv"],"verifierAdapter":"python@1"}
requirements.txt
python-dotenv==1.2.2
test/contract.py
import io
import dotenv

def test_parsing_rules():
    env_content = (
        "VAR_UNQUOTED=\\n\n"
        "VAR_SINGLE='\\n'\n"
        "VAR_DOUBLE=\"\\n\"\n"
        "VAR_UNQUOTED_HASH=val#3\n"
        "VAR_UNQUOTED_SPACE_HASH=val #3\n"
        "VAR_QUOTED_HASH=\"val\"#3\n"
    )
    parsed = dotenv.dotenv_values(stream=io.StringIO(env_content))

    # Escape sequence parsing
    assert parsed.get("VAR_UNQUOTED") == "\\n", "assert unquoted \\n is literal backslash-n"
    assert parsed.get("VAR_SINGLE") == "\\n", "assert single-quoted \\n is literal backslash-n"
    assert parsed.get("VAR_DOUBLE") == "\n", "assert double-quoted \\n is actual newline"

    # Inline comment parsing
    assert parsed.get("VAR_UNQUOTED_HASH") == "val#3", "assert unquoted # without preceding space is part of the string"
    assert parsed.get("VAR_UNQUOTED_SPACE_HASH") == "val", "assert unquoted # with preceding space is a comment"
    assert parsed.get("VAR_QUOTED_HASH") == "val", "assert # immediately following a closing quote is a comment"

if __name__ == "__main__":
    test_parsing_rules()
    print("CONTRACT PASS: python-dotenv escape sequences and comments verified")
test_dotenv.py
import io
import dotenv

env_content = """
# Unquoted escape sequences
VAR1=\\n
# Single quoted escape sequences
VAR2='\\n'
# Double quoted escape sequences
VAR3="\\n"

# Export prefix
export VAR4=val4

# Inline comments
VAR5=val5 # This is a comment
VAR6="val6 # This is NOT a comment"

# Spaces around equals
VAR7 = val7
"""

parsed = dotenv.dotenv_values(stream=io.StringIO(env_content))
for k, v in parsed.items():
    print(f"{k}: {repr(v)}")

オリジンシーダー

csx-seed