Review a generated batch publisher

from Context managers
Python 3.14 advanced 12 min 4 issues to find

Review this generated publishing context manager.

Open a dynamic set of input files safely, expose handles without loading all contents, commit only after success, roll back any failing operation, close every opened file, preserve exceptions, and never log the transaction token.

Python
from contextlib import contextmanager


@contextmanager
def publish_batch(client, paths):
    files = [open(path, encoding="utf-8") for path in paths]
    token = client.begin()
    payloads = [file.read() for file in files]

    try:
        yield payloads
        client.commit(token)
    except Exception as error:
        print(f"publish failed token={token}: {error}")
        return

    for file in files:
        file.close()

generated code is illustrative, not from any one model

Open in playground
Report an error