Skip to content

Stop on broken preconditions; return run-time failures in status_t - #87

Merged
certik merged 1 commit into
mainfrom
split-3-status
Nov 22, 2026
Merged

certik merged 1 commit into
mainfrom
split-3-status

Conversation

@certik

@certik certik commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Contract stops and status_t

fplotlib now tells two kinds of failure apart.

A call that breaks a documented precondition stops. Examples:

  • drawing before open_canvas;
  • a nonfinite coordinate reaching a renderer;
  • an unknown fill rule, or bytes before close_canvas;
  • savefig of an unknown format, or xlim with an infinite limit.

contract_error in the new fplotlib_status writes fplotlib: <rule> to stderr and stops with exit code 1. Every stop in the library goes through it, in place of the error stops and prints there were.

A failure that a correct program can still meet at run time is returned in a status_t. There are two:

  • ERROR_IO: an output file can't be opened, written or closed;
  • ERROR_SIZE: a PNG cropped with bbox_inches="tight" comes out with more than MAX_PNG_PIXELS pixels.

savefig, save_animation, show and the render_* functions take an optional status. Without one they stop with the message and exit code 2, except show, which only reports.

The renderer contract is checked once, for every backend. renderer_t's public methods are non_overridable wrappers that check their arguments and the call order. The backends implement deferred *_impl methods, and encoded_renderer_t adds bytes for the backends that encode a file.

Limits. These are listed in docs/api/fplotlib_status.md:

  • coordinates and sizes: at most MAX_COORD (1e15 points);
  • a round- or square-ended dashed pen: at most MAX_PEN_PER_DASH (100) times its pattern;
  • a PNG canvas: at most MAX_PNG_PIXELS (1e8);
  • an animation's frames: at most MAX_REEL_BYTES, counted as they encode at worst.

tests/test_contract.f90 runs the test binary once per broken call (22 here) and checks the exit code and the message.

This is part of a stack of five PRs that together are exactly #83 (the final tree of the last one is byte for byte #83's), split so that each can be reviewed and merged on its own. Each one builds and passes the whole test suite with gfortran, gfortran -O3 -march=native, flang, flang -O3 -march=native and LFortran, and the docs build.

  1. PNG rasterizer: exact and bounded on far-off geometry #85 PNG rasterizer: exact and bounded on far-off geometry
  2. Leave missing data out of the picture; clip far-off geometry #86 Missing data and far-off geometry in the plotting layer
  3. Stop on broken preconditions; return run-time failures in status_t #87 Contract stops and status_t (the error API)
  4. Check every plotting argument from one generated table #88 Argument checks from one generated table
  5. Keep far-off data finite in ticks, dates, contours and axis limits #89 Far-off data in ticks, dates, contours and axis limits; sanitizer CI job

Merge them in order; each PR's base is the one before it. #83 went through 30 rounds of independent review, the last with no blockers.

fplotlib now tells two kinds of failure apart.

A call that breaks a documented precondition is a bug in the calling
code: drawing before open_canvas, a nonfinite coordinate reaching a
renderer, an alpha outside [0, 1], an unknown fill rule, bytes before
close_canvas, savefig of an unknown format, xlim with an infinite limit.
contract_error in the new fplotlib_status writes "fplotlib: <rule>" to
standard error and stops with exit code 1; every stop in the library
goes through it, in place of the error stops and prints there were.

A failure a correct program can still meet at run time is returned
instead, in a status_t: an output file that cannot be opened, written or
closed (ERROR_IO), or a PNG cropped with bbox_inches="tight" to more than
MAX_PNG_PIXELS pixels (ERROR_SIZE). savefig, save_animation, show and
the render_* functions take an optional status; without one they stop
with the message and exit code 2, except show, which only reports.

renderer_t checks its contract once, for every backend: its public
methods are non_overridable wrappers that check their arguments and the
call order, and the backends implement deferred *_impl methods.
encoded_renderer_t adds bytes for the backends that encode a file.
Coordinates and sizes are bounded by MAX_COORD (1e15 points), a round-
or square-ended dashed pen by MAX_PEN_PER_DASH (100) times its pattern,
a PNG canvas by MAX_PNG_PIXELS (1e8), and an animation's frames by
MAX_REEL_BYTES, counted as they encode at worst. docs/api/fplotlib_status.md
lists the limits.

tests/test_contract.f90 runs the test binary once per broken call and
checks the exit code and the message.
@certik
certik merged commit c83aea3 into main Nov 22, 2026
10 checks passed
@certik
certik deleted the split-3-status branch November 22, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant