Mixing PySide Slot with other decorators

Hi.
For some time now I’m using PySide with *.ui files from Designer. I’m doing all the signal/slot logic in the Designer, and everything works just fine.
However I would like to make another decorator that would catch all exceptions from my slot functions. And here’s the problem - if I add another decorator the auto slot binding stops to work.

Anyone has any idea how to make it so that it works?

Cheers.

I DID IT!!!

You create your decorator like this:


def traced(f):
    @functools.wraps(f)
    def wrapper(self, *args, **kwargs):
        # noinspection PyBroadException
        try:
            f(self, *args, **kwargs)
        except Exception, e:
            self.show_error("Unhandled exception!", "There was an unhandled exception in the code!", True)
            raise e
    return wrapper

and then define slots like this:


@traced
@Slot()
def do_grab(self):
    sel = pm.ls(sl=True, type="transform")

    if len(sel) != 2:
        self.show_error("Wrong selection", "Please select two transform objects: first parent, second child.")
        return
    ...

One quick note, use as e, instead of , e to bind the exception to a name. (Unless your stuck on python 2.5)


# Works, but isn't great
except Exception, e:
# much better:
except Exception as e:

The newer syntax is a bit more clear, and lets you catch multiple types of exceptions, and still bind to a name, whereas the old syntax will just break.


# Works
except TypeError, AttributeErrror as e:
# Doesn't work, SyntaxError
except TypeError, AttributeError, e:

I didn’t realize that syntax was possible, neat.

I have always done:

except (TypeError, ValueError), e:

Thanks guys. Another thing learned :slight_smile: