gaphor / gaphor

Gaphor is the simple modeling tool
https://gaphor.org
1.87k stars 200 forks source link

Simplify attribute and enumeration lookup (do not throw exceptions) #997

Closed sz332 closed 3 years ago

sz332 commented 3 years ago

Describe the bug

When starting the application in debug mode, creating a new fork node, and enabling stop on uncaught/raised exceptions, a huge amount of exception is caught by VSCode.

To Reproduce

Expected behavior

There should be no uncaught exceptions in the code.

Screenshots

image

OS

Version

Version of Gaphor: 2.5.1

Additional information

Add any other context about the problem here.

amolenaar commented 3 years ago

What really interests me: why did we implement it this way and not with getattr and a default parameter :). Anyhow, this calls for some improvement.

I did a little test, comparing the exception handling vs the getattr function.

Old (exception handling):

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 9955    0.007    0.000    0.017    0.000 properties.py:352(_get)

0,017 ÷ 9955 = 0,000001708

New (getattr with default):

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
14723    0.010    0.000    0.022    0.000 properties.py:348(_get)

0,022 ÷ 14723 = 0,0000014943

I'll change it to a simple getattr. That'll do the trick.

amolenaar commented 3 years ago

I pushed a fix.

I checked the other try/except blocks in properties.py. They all seem legit to me. In Python it's quite common to code for the general case and handle the special case in an except clause, as is done for derived.