首页 > 解决方案 > Jython 给出“无法创建具有非字节值的 PyString”错误

问题描述

我的代码:

from javax.swing import JFrame

frame = JFrame("Hello")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setLocation(100,100)
frame.setSize(300,200)
frame.setVisible(True)

但是我无法在 cmd 中打开 jython 它给出了这个错误,我知道为什么会这样。当我用 jython(eclipse) 在 java 中做同样的事情时,它的工作原理。

C:\>jython banana.py
Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.python.core.PySystemState.<clinit>(PySystemState.java:73)
        at org.python.util.jython.main(jython.java:533)
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.core.Py.newString(Py.java:643)
        at org.python.core.PyJavaType.init(PyJavaType.java:543)
        at org.python.core.PyType$Registry.createType(PyType.java:477)
        at org.python.core.PyType$Registry.addFromClass(PyType.java:426)
        at org.python.core.PyType$Registry.resolveType(PyType.java:352)
        at org.python.core.PyType$Registry$1.computeValue(PyType.java:208)
        at org.python.core.PyType$Registry$1.computeValue(PyType.java:202)
        at java.base/java.lang.ClassValue.getFromHashMap(ClassValue.java:226)
        at java.base/java.lang.ClassValue.getFromBackup(ClassValue.java:208)
        at java.base/java.lang.ClassValue.get(ClassValue.java:114)
        at org.python.core.PyType.fromClass(PyType.java:2137)
        at org.python.core.PyObject.<init>(PyObject.java:85)
        at org.python.core.PySingleton.<init>(PySingleton.java:9)
        at org.python.core.PyNotImplemented.<init>(PyNotImplemented.java:10)
        at org.python.core.Py.<clinit>(Py.java:66)
        ... 2 more

jython --version运行良好,jython 给出了相同的错误输出。我卡住了

标签: javapythonjythonillegalargumentexceptionjython-2.7

解决方案


这似乎是某些 Windows 10 Pro 实例的问题(请参阅:https ://bugs.jython.org/issue2890 )。

至于解决方案,您可以尝试附加调试器或将以下 println 添加到 PyJavaType.java 以打印出反映的类:

    // Fill in the __module__ attribute of PyReflectedFunctions.
    if (reflectedFuncs.size() > 0) {
        if (nameSpecified == null) {
            System.out.println(name);
            nameSpecified = Py.newString(name);
            
        }
        for (PyReflectedFunction func : reflectedFuncs) {
            func.__module__ = nameSpecified;
        }
    }

样本输出:

C:\java -jar jython-standalone-2.7.2.jar
reflectedconstructor
reflectedfield
reflectedfunction
beanproperty
java.lang.Object
java.lang.reflect.AnnotatedElement
java.lang.reflect.GenericDeclaration
java.lang.reflect.Type
java.lang.Class
singleton
notimplemented
javapackage
org.python.core.JavaImporter
.
.
.

我怀疑这与“未实现”类(来自堆栈跟踪)有关。我无法在 Win 10 Pro 上重现。


推荐阅读