【问题标题】:Ctypes support in JythonJython 中的 Ctypes 支持
【发布时间】:2015-03-10 04:15:26
【问题描述】:

我对 Java 和 python 还很陌生。我有几个需要从 Java 应用程序调用的 python 脚本。目前我正在使用 Jython 2.7 beta 3 版本来执行此操作。我的 python 脚本使用来自 Ctypes(create_string_buffer) 的方法。当我从 Java 运行此脚本时,它会引发以下错误。

'module'对象没有属性'create_string_buffer'

我的java代码如下:

import org.python.core.PyInstance;  
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;  
import org.python.core.PyString;


public class Sample  
{  

   PythonInterpreter interpreter = null;  


   public Sample()  
   {  
      PythonInterpreter.initialize(System.getProperties(),  
                                   System.getProperties(), new String[0]);  

      this.interpreter = new PythonInterpreter();  
   }  

   void execfile( final String fileName )  
   {  
      this.interpreter.execfile(fileName);  
   } 

   void eval( final String className, final String opts )  
   {  
       this.interpreter.eval(className + "(" + opts + ")");    
   } 

   PyInstance createClass( final String className, final String opts )  
   {  
      return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");  
   }  

   public static void main( String gargs[] )  
   {  
      Sample ie = new Sample();  

      ie.execfile("C:/Users/trail.py");   

   }  
}  

请让我知道我哪里出错了,或者我错过了什么。

【问题讨论】:

  • 基本上,trail.py 是一个与硬件对话并返回一些信息的脚本。由于保密问题,我不能透露这一点。我也无权修改脚本。该脚本使用了许多 Ctypes 功能和其他本机库。我的 Java 应用程序需要运行此脚本并使用输出。我最近做了很多谷歌搜索,但找不到合适的方法。目前,我最终使用 Runtime.getRuntime().exec() 来运行脚本,但这似乎不是一个理想的方法。任何人都可以帮助我在这种情况下使用最佳方法吗?
  • @mzjn 当我使用 Jython 从 java 运行以下 python 脚本时出现此错误: import ctypes p=ctypes.create_string_buffer('Hello') print p.value 如果您需要任何内容​​,请告诉我更多信息。我无法用任何方法解决这个问题。
  • 建议:编辑问题而不是在cmets中添加信息。

标签: java python jython


【解决方案1】:

CPython (2.7) 中的 ctypes 模块定义了一个create_string_buffer 函数。 Jython (2.7 beta 3) 中的 ctypes 模块没有。

我真的不知道更多,但似乎有理由得出结论,Jython 中对 ctypes 的支持是不完整的。另见:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
相关资源
最近更新 更多