Tracking down a weird bug in Jython code

Just thought I’d post this here in case someone else runs into this …

I was seeing a strange run-time error in my logs for my jsr223 jython code:

2022-09-23 20:54:49.083 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job '<unknown>' failed and stopped
org.python.core.PyException: TypeError: 'bool' object is not callable
	at org.python.core.Py.TypeError( ~[?:?]
	at org.python.core.PyObject.__call__( ~[?:?]
	at org.python.core.PyObject.__call__( ~[?:?]
	at org.python.core.PyObject.__call__( ~[?:?]
	at org.python.pycode._pyx2333.f$4(/etc/openhab/automation/jsr223/python/personal/ ~[?:?]
	at org.python.pycode._pyx2333.call_function(/etc/openhab/automation/jsr223/python/personal/ ~[?:?]
	at ~[?:?]
	at ~[?:?]
	at org.python.core.PyFunction.__call__( ~[?:?]
	at org.python.core.PyFunction.__call__( ~[?:?]
	at org.python.core.PyFunction.invoke( ~[?:?]
	at com.sun.proxy.$Proxy616.apply(Unknown Source) ~[?:?]
	at org.openhab.core.model.script.actions.ScriptExecution.lambda$0( ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12( ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1( ~[?:?]
	at java.util.concurrent.Executors$ [?:?]
	at [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:?]
	at java.util.concurrent.ThreadPoolExecutor$ [?:?]
	at [?:?]

It was being generated by a ScriptExecution timer expiring … there wasn’t much info in the log to help, except that it seemed to be trying to execute a boolean when it should have been a function.

After scratching my head for a while in trying to reproduce it, I started to compare the code with an older version I had. I found that I had recently added some new code to test for some event and used a global boolean variable that had the same name as the lambda timer function :roll_eyes: … I was surprised that the compiler doesn’t catch this, or at least generate a warning… anyway, my mistake.

So lesson learned, always make sure your variables (especially global ones) don’t accidentally have the same name as one of your functions!

That’s because it was syntactically correct, just not logically correct. Weakly typed and typeless interpreted languages like Python don’t really get much testing/checking done at compile time. That’s why it’s vital to test the code to know whether or not it’s correct. The compiler will allow all sorts of nonsense to pass by.

1 Like

Yep exactly. I have been using variable naming conventions to help keep things more clear, I didn’t do that in this case. lesson learned :slight_smile: