Skip to content

Fix Drawing.undo() crash on empty or single-element drawings#99

Closed
leofernandezg wants to merge 1 commit intocdelker:masterfrom
leofernandezg:fix/undo-empty-drawing
Closed

Fix Drawing.undo() crash on empty or single-element drawings#99
leofernandezg wants to merge 1 commit intocdelker:masterfrom
leofernandezg:fix/undo-empty-drawing

Conversation

@leofernandezg
Copy link
Copy Markdown

Summary

  • Fixes crashes in Drawing.undo() when the drawing has zero or one element

Problem

undo() had three failure modes:

  1. Empty drawing: self.elements.pop(-1) raises IndexError
  2. Single element: after pop, self.elements[-1].absdrop raises IndexError on empty list
  3. Non-interactive mode: self.fig.clear() raises AttributeError because fig is None

Changes

  • schemdraw/schemdraw.py: Guard against empty element list (early return), reset position to origin when all elements are removed, skip figure operations when fig is None
  • test/test_undo.py: New test file covering empty drawing, single element, and undo-then-add scenarios

Test results

Without fix: 3 of 3 tests fail
With fix: 3 of 3 tests pass

Fixes #94

undo() crashed with IndexError when the drawing had only one element
(self.elements[-1] on empty list after pop), and with pop() error on
empty drawings. Also crashed with AttributeError when fig was None
(non-interactive mode).

Guard against empty element list and None figure. Reset position to
origin when all elements are removed. Added test/test_undo.py.

Fixes cdelker#94
@leofernandezg
Copy link
Copy Markdown
Author

Test suite results

Full test suite (py.test --nbval-lax test/) run on this branch produces the same results as master:

Branch Failed Passed
master (baseline) 38 432
this branch 38 435

The 38 pre-existing failures are in test_parselogic.ipynb and test_timing.ipynb (unrelated to this change).

@leofernandezg
Copy link
Copy Markdown
Author

Update: Re-ran the full test suite with all optional dependencies installed (matplotlib, ziamath, pyparsing).

Branch Passed Failed
master 470 0
fix/undo-empty-drawing 473 0

No regressions introduced.

@cdelker cdelker closed this Apr 11, 2026
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.

Drawing.undo() crashes with IndexError when only one element remains

2 participants