0 4. Tips & Tricks for working with Jython
jomo edited this page 2015-08-12 23:57:44 +02:00

Debugging

Debugging can be a bit special with jython/PyPluginLoader.

When something goes wrong, you probably see a weird traceback that's not telling you shit.
...unless you take a closer look.

You will not see a direct traceback of the python methods.
Instead, you'll probably see a bunch of java, bukkit and python things, because it tries to translate python into java, line per line.
Take a closer look at the method names, they might tell you what it's trying to do and where it's coming from.

Watch out for something like org.python.pycode._pyx5.horribleCode$23(/path/to/badcode.py:214) ~[?:?]

  1. In this case, _pyx5 is our module.
  2. horribleCode is the method that was called
  3. /path/to/badcode.py is the actual file of our module
  4. :214 is the line in which the error occured.

Please note that the line may not be accurate. You'll often get the start of a loop, or the end of a method - when the actual error was somewhere in there.

In many cases, this is enough to find your bug. If you still cannot find it,try to catch exceptions in your code, as follows:

Catching exceptions

If you want to catch all exceptions (e.g. for debugging), do not except Exception.
Since we're using jython, this will not catch Java exceptions.
Instead, use except and print the traceback. This will give you some more deatails:

# bad
try:
  #code
except Exception, e: # will not catch Java Exceptions
  print(e)

# good
from traceback import format_exc as trace
try:
  #code
except: # everything
  print(trace())