FAILURE MAP
← Case archive

FA-5171 / Context managers / Open access

Context enter binding: Calls __enter__ again while constructing the with-body binding · case 01

Calls __enter__ again while constructing the with-body binding and changes the required Python-language result.

Verified by executionVariant 1 · 5 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

Calls __enter__ again while constructing the with-body binding. The executable reproducer isolates this operation from the rest of the contract.

VERIFIED REPAIR

Bind the exact __enter__ return value to the with target, including false values. A failing __enter__ prevents body and __exit__; body exceptions still call __exit__.

Unsuccessful approach: The alternative still fails because it calls __enter__ manually and loses guaranteed exit on body failure.

Case contract

Bind the exact __enter__ return value to the with target, including false values. A failing __enter__ prevents body and __exit__; body exceptions still call __exit__. Inputs use the finite Python types shown in the fixtures. An error string denotes the specified caught exception, not an unhandled process failure.

Why this case matters

Runs Python standard-library language operations in a deterministic local reproducer. Literal expected answers are authored independently. Related defects share an evaluation group to avoid benchmark split leakage.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(payload, fail, body_error):
    log=[]
    class Manager:
        def __init__(self): self.value='manager'
        def __enter__(self):
            log.append('enter')
            if fail: raise ValueError('enter failed')
            return payload
        def __exit__(self,typ,exc,tb): log.append('exit'); return False
    manager=Manager()
    try:
        with manager as bound:
            log.append(['body',manager.__enter__()])
            if body_error: raise KeyError('body failed')
    except Exception as exc: log.append(type(exc).__name__)
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('ordinary binding', solve('value', False, False), ['enter', ['body', 'value'], 'exit'])
check('false return remains bound', solve(0, False, False), ['enter', ['body', 0], 'exit'])
check('None return remains bound', solve(None, False, False), ['enter', ['body', None], 'exit'])
check('enter failure skips exit', solve('value', True, False), ['enter', 'ValueError'])
check('body failure still exits', solve('value', False, True), ['enter', ['body', 'value'], 'exit', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
ordinary binding['enter', 'enter', ['body', 'value'], 'exit']['enter', ['body', 'value'], 'exit']Failed
false return remains bound['enter', 'enter', ['body', 0], 'exit']['enter', ['body', 0], 'exit']Failed
None return remains bound['enter', 'enter', ['body', None], 'exit']['enter', ['body', None], 'exit']Failed
enter failure skips exit['enter', 'ValueError']['enter', 'ValueError']Passed
body failure still exits['enter', 'enter', ['body', 'value'], 'exit', 'KeyError']['enter', ['body', 'value'], 'exit', 'KeyError']Failed

SHA-256 / 800f626b8c5e75bb97f4acdc203cd48a7dac0e942000074da36eb6fc4f0d1991

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(payload, fail, body_error):
    log=[]
    try:
        log.append('enter')
        if fail: raise ValueError('enter failed')
        log.append(['body',payload])
        if body_error: raise KeyError('body failed')
        log.append('exit')
    except Exception as exc: log.append(type(exc).__name__)
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('ordinary binding', solve('value', False, False), ['enter', ['body', 'value'], 'exit'])
check('false return remains bound', solve(0, False, False), ['enter', ['body', 0], 'exit'])
check('None return remains bound', solve(None, False, False), ['enter', ['body', None], 'exit'])
check('enter failure skips exit', solve('value', True, False), ['enter', 'ValueError'])
check('body failure still exits', solve('value', False, True), ['enter', ['body', 'value'], 'exit', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
ordinary binding['enter', ['body', 'value'], 'exit']['enter', ['body', 'value'], 'exit']Passed
false return remains bound['enter', ['body', 0], 'exit']['enter', ['body', 0], 'exit']Passed
None return remains bound['enter', ['body', None], 'exit']['enter', ['body', None], 'exit']Passed
enter failure skips exit['enter', 'ValueError']['enter', 'ValueError']Passed
body failure still exits['enter', ['body', 'value'], 'KeyError']['enter', ['body', 'value'], 'exit', 'KeyError']Failed

SHA-256 / f1a3f9b26aeae6a0b7a9b4649d4a4daa84645e7c7d69269056945f0fe4a6ed25

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(payload, fail, body_error):
    log=[]
    class Manager:
        def __init__(self): self.value='manager'
        def __enter__(self):
            log.append('enter')
            if fail: raise ValueError('enter failed')
            return payload
        def __exit__(self,typ,exc,tb): log.append('exit'); return False
    manager=Manager()
    try:
        with manager as bound:
            log.append(['body',bound])
            if body_error: raise KeyError('body failed')
    except Exception as exc: log.append(type(exc).__name__)
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('ordinary binding', solve('value', False, False), ['enter', ['body', 'value'], 'exit'])
check('false return remains bound', solve(0, False, False), ['enter', ['body', 0], 'exit'])
check('None return remains bound', solve(None, False, False), ['enter', ['body', None], 'exit'])
check('enter failure skips exit', solve('value', True, False), ['enter', 'ValueError'])
check('body failure still exits', solve('value', False, True), ['enter', ['body', 'value'], 'exit', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
ordinary binding['enter', ['body', 'value'], 'exit']['enter', ['body', 'value'], 'exit']Passed
false return remains bound['enter', ['body', 0], 'exit']['enter', ['body', 0], 'exit']Passed
None return remains bound['enter', ['body', None], 'exit']['enter', ['body', None], 'exit']Passed
enter failure skips exit['enter', 'ValueError']['enter', 'ValueError']Passed
body failure still exits['enter', ['body', 'value'], 'exit', 'KeyError']['enter', ['body', 'value'], 'exit', 'KeyError']Passed

SHA-256 / 73b8659395ff0179c5440f6dec07c2cc5f47e44c7f652f79e33d9410b3729621

Verification & scope

This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:37:46.583891+00:00.

Case digest / fa9fc020581d75da01db83c235e7b466b5ea390dd3f0b0a0cba5099146e0f613