From ef833ec64b971ec601f4361812af58762037cd93 Mon Sep 17 00:00:00 2001 From: jomo Date: Wed, 17 Dec 2014 16:11:22 -0800 Subject: [PATCH] Created Tips & Tricks for working with Jython (markdown) --- Tips-&-Tricks-for-working-with-Jython.md | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Tips-&-Tricks-for-working-with-Jython.md diff --git a/Tips-&-Tricks-for-working-with-Jython.md b/Tips-&-Tricks-for-working-with-Jython.md new file mode 100644 index 0000000..646b373 --- /dev/null +++ b/Tips-&-Tricks-for-working-with-Jython.md @@ -0,0 +1,34 @@ +## 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) ~[?:?]` + +0. In this case, `_pyx5` is our module. +0. `horribleCode` is the method that was called +0. `/path/to/badcode.py` is the actual file of our module +0. `: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: +```python +from traceback import format_exc as trace +try: + #code +except: # everything + print(trace()) +``` \ No newline at end of file