Hidden python import errors

OnmyojiOmn

Prince
Joined
Aug 4, 2006
Messages
371
Hi,

I just converted a mod for BUG's modular python system, and doing so took much longer than it should have because the lookupModule function in BugUtil discards ImportErrors:

Code:
def lookupModule(module, log=True):
	if log:
		debug("BugUtil - looking up %s", module)
	try:
		return __import__(module)
	except ImportError:
		raise ConfigError("No such module '%s'", module)

I was able to figure out what I'd done wrong (incorrect imports) very quickly once I took out the try/except block. I don't know much about Python errors or whether you can wrap them, but ConfigError should at least include the original error message. "No such module" is probably the least likely cause of an ImportError being thrown here.

Somewhat related: speaking of python in BUG, we still need autoloading modules as of BUG 4.2. This business with init.xml is very crufty, and requiring users to edit XML to install a module is the difference between easy drag n' drop and a relatively complex installation process. Just load everything in contrib at startup and call it a day.

In any case, thank you for BUG. <3
 
AFAIK ImportError is only thrown when the module cannot be found. SyntaxError, IndentationError, and other causes of failing to load a found module have their own exceptions. And the reason I throw BUG's own ConfigError is because the module name comes from a configuration XML file, and BUG catches this exception so it can write the XML file and line number where it found the problem module.

If this isn't working or you have found that other problems are being caught by this as ImportErrors, please give me an example so I can fix it.

As for auto-loading, I agree it's a huge item. Work is quick heavy for me right now, and it's tough to knock out something that big in a weekend. I hope to get to it for the next release after the one coming up in the next couple weeks.
 
Top Bottom