示例
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 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
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
契约
- 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 -> val)
- 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
源代码
# 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.
{"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"}
python-dotenv==1.2.2
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")
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)}")