Reject *path* if its size exceeds the configured graph-file cap. Protects callers from memory bombs by failing fast before a multi-GiB graph.json is read into memory and JSON-parsed. Silently returns when ``path.stat()`` cannot be read — the caller's own existence/path check is expe
(path: Path)
| 354 | |
| 355 | |
| 356 | def check_graph_file_size_cap(path: Path) -> None: |
| 357 | """Reject *path* if its size exceeds the configured graph-file cap. |
| 358 | |
| 359 | Protects callers from memory bombs by failing fast before a multi-GiB |
| 360 | graph.json is read into memory and JSON-parsed. Silently returns when |
| 361 | ``path.stat()`` cannot be read — the caller's own existence/path check |
| 362 | is expected to surface a clearer error in that case. |
| 363 | |
| 364 | The cap is resolved on every call via :func:`_max_graph_file_bytes`, so the |
| 365 | ``GRAPHIFY_MAX_GRAPH_BYTES`` env var can be set before import and still |
| 366 | apply. |
| 367 | |
| 368 | Raises: |
| 369 | ValueError - file size exceeds the cap. The message includes the |
| 370 | observed size, the cap, and how to raise the limit. |
| 371 | """ |
| 372 | cap = _max_graph_file_bytes() |
| 373 | try: |
| 374 | size = path.stat().st_size |
| 375 | except OSError: |
| 376 | return |
| 377 | if size > cap: |
| 378 | raise ValueError( |
| 379 | f"graph file {path} is {size:_d} bytes, exceeds {cap:_d}-byte cap\n" |
| 380 | f"(set GRAPHIFY_MAX_GRAPH_BYTES=<bytes> or " |
| 381 | f"GRAPHIFY_MAX_GRAPH_BYTES=<N>GB to raise the limit)" |
| 382 | ) |
| 383 | |
| 384 | |
| 385 | # --------------------------------------------------------------------------- |