Importing time module bug

I wanted to post this here because I would like to check if anyone else is having this same issue within the community.

A couple of days ago a user reported they were getting a “cryptic” error that read “Cannot create PyString with non-byte value”.

At first I could not replicate the error on my computer, so after doing some remote assistance I finally found the issue, and it relied on a harmless line of code; an import of the time module.

The only way I was able to replicate this behavior on my machine running Windows 10 I had to switch my system language to Čeština (a.k.a. Czech), run the Designer, and open the Script Console.

This is the code I executed on the Script Console:

import time

print time.__name__

And this is the console output:

Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java11.0.5

>>> 
Java Traceback:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
	at org.python.core.PyObject.__call__(PyObject.java:450)
	at org.python.core.__builtin__.__import__(__builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

	at org.python.core.Py.JavaError(Py.java:552)
	at org.python.core.Py.JavaError(Py.java:543)
	at org.python.core.PyJavaType.init(PyJavaType.java:578)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
	at org.python.core.PyObject.__call__(PyObject.java:450)
	at org.python.core.__builtin__.__import__(__builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	... 29 more
Traceback (most recent call last):
  File "<input>", line 1, in <module>
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
	at org.python.core.PyObject.__call__(PyObject.java:450)
	at org.python.core.__builtin__.__import__(__builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

This error was being thrown on a function I created where I Temporarily enable DB connection, and between retries I “sleep” the process for a second, and the only way I could squash this bug was to remove that sleep and replace it with a while loop.

I am aware this may sound a bit weird, but it is the behavior we are noticing.

Thanks!

Additional info.

The error goes away on the third execution. As in I click the Execute button for the third time on the Script Console.

Code:

from java.lang import Exception as JException

try:
    import time
    print time.__name__
except JException, e:
    print e.message

Output:

Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java11.0.5

>>> 
Cannot create PyString with non-byte value
>>> 
Cannot create PyString with non-byte value
>>> 
Time
>>> 

Python’s time module is tailored for and expects a CPython environment. Don’t use it. Use java.lang.Date and java.lang.Calendar instead.

1 Like

Apparently this is a known bug in Jython 2.7.1 when trying to print unicode. Like Phil said, if you need date strings in a locale that will go beyond 8-bit ASCII, you’ll have to change to Java’s libraries.

I tested the following code, and hereby I confirm that switching to java.lang.Thread solves the problem experienced with Python’s time module.

import system.date

from java.lang import Exception as JException, Thread

def java_sleep():
    print 'Thread.sleep'
    print system.date.now()
    Thread.sleep(5000)
    print system.date.now()

def py_sleep():
    print 'time.sleep'
    import time
    print system.date.now()
    time.sleep(5)
    print system.date.now()


try:
    java_sleep()
    py_sleep()
except JException, e:
    print e.message

Output:

>>>>
Thread.sleep
Tue Apr 14 22:01:02 CEST 2020
Tue Apr 14 22:01:07 CEST 2020
time.sleep
Cannot create PyString with non-byte value
>>>>

But still mind-boggling that after multiple attempts calling Python’s time.sleep does work without throwing an error; and after one successful execution the error occurs no more.

I’ll be making the switch to Thread.sleep.

Thanks, @KathyApplebaum and @pturmel.

3 Likes

Thanks