Sunday, April 24, 2016

Python exceptions

When writing Python code it's import to trap errors :) And MMA does this, fairly well.

But, I just had some fun with the plugin loader. I kept getting a weird error message which didn't make a lot sense. After a bit (well, actually quite a bit!) of head scratching I finally figured it out.

In the regplug.py module I have a bit of code which looked a bit like this:

    try:
        e = importlib.import_module(modName, package=None)
    except ImportError:
         error( ... report error ... )

this was meant to trap/report a problem loading the plugin module. If the module just didn't exist or was unreadable it worked fine.

But, what if the module was there and there was an error in it which caused a problem when loading? If it was something dumb like too many '(' or ')'s python reports the appropriate error as a crash. Okay, that's probably just fine. Writers of plugins should make sure that they work!

But, what if the module being loaded has a "import xxx" line? Well, since we're catching import errors, we catch it. I don't think we can set up a try/except which only works a one level.

The solution is simply to change our code to:

    try:
        e = importlib.import_module(modName, package=None)
    except ImportError, err:
        error("Plugin: Error loading module '%s'. Python is reporting '%s'. "
               "Most likely there is an Import statement in the module which is not working."% (modName, str(err)))

and now MMA reports a more useful message.


Thursday, April 14, 2016

New test version

I finally got a crappy-weather-day here. Ideal to do some needed updating to the plugin routines! Ignazio wrote a nice little module (included in the MMA module directory) with some standard functions which should make writing a plugin easier (well, more consistent). So, that along with his docs, have been added to the official tree.

I really hope some of you are taking advantage of the plugin facility. Possibilities are endless!

So you can enjoy the latest I've put a new developer version up on the website. You can get it at: http://mellowood.ca/mma/downloads.html#developer

Unless we find some serious bugs, this will be the next stable release. Probably version 16.06. But who knows?

 I've been pretty neglectful in keeping this blog up-to-date. Lots of excuses ... but, I'll try to do a bit better! There is a new b...