Review a generated lazy order reader

from Generators and iterators
Python 3.14 advanced 8 min 4 issues to find

Review this generated reader against the task and identify four distinct risks.

Lazily read positive order totals from an untrusted filename under data_root, skip an optional header, and release the file on errors or early termination.

Python
from pathlib import Path

def iter_orders(data_root, filename):
    path = Path(data_root) / filename
    source = path.open(encoding="utf-8")
    rows = (line.rstrip().split(",") for line in source)
    next(rows)

    cache = []
    for fields in rows:
        order_id, total_text = fields
        total_cents = int(total_text)
        if total_cents:
            cache.append((order_id, total_cents))

    source.close()

    for order in cache:
        yield order

generated code is illustrative, not from any one model

Open in playground
Report an error