GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GraphicCollection.__del__ method can cause confusing tracebacks · Issue #393 · fastplotlib/fastplotlib

GraphicCollection.__del__ method can cause confusing tracebacks #393

New issue
Copy link
New issue
Copy link
Closed
#546
Closed
GraphicCollection.__del__ method can cause confusing tracebacks#393
#546
Copy link

Description

@tlambert03
tlambert03
opened on Dec 8, 2023
Issue body actions

If something fails during init of a GraphicCollection, an exception will be raised, but that exception will often be hidden higher up in the stack due to a failure during cleanup in the __del__ method. for example, if you pass the wrong length of cmaps to a LineCollection:

Traceback (most recent call last):
  File "/Users/talley/dev/forks/fastplotlib/examples/desktop/line_collection/line_stack.py", line 27, in <module>
    plot.add_line_stack(data, cmap=["jet"])
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/layouts/graphic_methods_mixin.py", line 314, in add_line_stack
    return self._create_graphic(LineStack, data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/layouts/graphic_methods_mixin.py", line 25, in _create_graphic
    graphic = graphic_class(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/line_collection.py", line 547, in __init__
    super(LineStack, self).__init__(
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/line_collection.py", line 125, in __init__
    raise ValueError(
ValueError: cmap argument must be a single cmap or a list of cmaps with the same length as the data
Exception ignored in: <function GraphicCollection.__del__ at 0x1212071a0>
Traceback (most recent call last):
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/_base.py", line 477, in __del__
    super().__del__()
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/_base.py", line 171, in __del__
    del WORLD_OBJECTS[self.loc]
        ~~~~~~~~~~~~~^^^^^^^^^^
KeyError: '0x16a217250'

It would be nice to either ignore errors in the __del__ method, or do a bit more careful cleanup depending on the state of the object:

    def __del__(self):
        with contextlib.suppress(Exception):
            self.world_object.clear()
            for loc in self._graphics:
                del COLLECTION_GRAPHICS[loc]

            super().__del__()

and then you'd see:

Traceback (most recent call last):
  File "/Users/talley/dev/forks/fastplotlib/examples/desktop/line_collection/line_stack.py", line 27, in <module>
    plot.add_line_stack(data, cmap=["jet"])
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/layouts/graphic_methods_mixin.py", line 314, in add_line_stack
    return self._create_graphic(LineStack, data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/layouts/graphic_methods_mixin.py", line 25, in _create_graphic
    graphic = graphic_class(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/line_collection.py", line 547, in __init__
    super(LineStack, self).__init__(
  File "/Users/talley/dev/forks/fastplotlib/fastplotlib/graphics/line_collection.py", line 125, in __init__
    raise ValueError(
ValueError: cmap argument must be a single cmap or a list of cmaps with the same length as the data

Activity

  1. kushalkolar commented on Dec 12, 2023

    @kushalkolar
    kushalkolar
    on Dec 12, 2023
    Member
    More actions

    Sorry for the late response again! We usually try to repond within 24 hours at the latest, this is just a really busy time.

    I guess I learned something fundamentally new today, thanks a lot for catching this and the other things here 😄 ! We will hopefully be pouring more resources into fastplotlib development next year and slowly bring this up to a polished library!

    https://www.algorithm.co.il/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/

  2. tlambert03 commented on Dec 12, 2023

    @tlambert03
    tlambert03
    on Dec 12, 2023
    ContributorAuthor
    More actions

    Aha! Yes indeed. __new__ is the real constructor (inside of which an exception would prevent object creation). __init__ is there as a way to initialize a newly constructed object. Tricky! :)

  3. clewis7
    closed this as completedin #546on Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.