从 ctypes 访问公共块变量

Access Common Block variables from ctypes

我正在尝试从 Python 脚本访问存储在 Fortran 77 公共块中的数据。问题是我不知道这些数据存储在哪里。 我正在开发的 Python 应用程序使用了不同的库。这些库包含具有以下指令的函数:

#include <tcsisc_common.inc>

公共块包含:

C
      INTEGER*4 IDEBUG
C
C.... ARRAY DIMENSIONS
      DIMENSION IDEBUG(10)
C
C.... COMMON BLOCK
      COMMON /TCSD/ IDEBUG
C

在 Python 部分(在我使用 iPython 的示例中),我加载库:

In [1]: import ctypes
In [2]: _libtcsisc= /home/jfreixa/project/bin/libtcsisc.so
In [3]: _tcsisc   = ctypes.CDLL(_libtcsisc, ctypes.RTLD_GLOBAL)

问题是我不知道如何获取IDEBUG。我尝试了以下方法,但我只是将 tcsd 作为 c_long 初始化为 0.

In [4]: tcsd = ctypes.c_int.in_dll(_tcsisc, "TCSD_")
In [5]: tcsd
Out[5]: c_long(0)
In [6]: idebug = ctypes.c_int.in_dll(_tcsisc, "IDEBUG_")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ee5018286275> in <module>()

----> 1 idebug = ctypes.c_int.in_dll(_tcsisc,'IDEBUG_')

ValueError: ld.so.1: python2.7: fatal: IDEBUG_: can't find symbol

知道正确获取变量的方法吗?

根据 this page (particularly how to access Fortran common blocks from C) and some about how to access C struct from Python,我们似乎可以如下访问公共块(虽然这可能不是很便携,见下文):

mylib.f90

subroutine fortsub()
    implicit none
    integer n
    common /mycom/ n
    print *, "fortsub> current /mycom/ n = ", n
end

编译:

$ gfortran -shared -fPIC -o mylib.so mylib.f90

test.py

from __future__ import print_function
import ctypes

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "n", ctypes.c_int ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

print( " python> modifying /mycom/ n to 777" )

mycom.n = 777

fortsub = mylib.fortsub_
fortsub()

测试:

 $ python test.py 
 python> modifying /mycom/ n to 777
 fortsub> current /mycom/ n =          777

这里,请注意公共块的名称(这里,mycom)是小写的,并附有一个下划线(假设gfortran)。由于此约定依赖于编译器,因此可能更 robust/portable 为公共块中的 setting/getting 值编写新的 Fortran 例程(特别是在 iso_c_binding 的帮助下)并从 Python(正如@innoSPG 在第一条评论中所建议的那样)。


包含不同类型和数组的另一个示例可能如下所示:

mylib.f90

subroutine initcom()
    implicit none
    integer          n( 2 ), w  !! assumed to be compatible with c_int
    real             f( 2 )     !!                      ... with c_float
    double precision d( 2 )     !!                      ... with c_double
    common /mycom/ n, f, d, w

    print *, "(fort) initializing /mycom/"
    n(:) = [ 1, 2 ]
    f(:) = [ 3.0, 4.0 ]
    d(:) = [ 5.0d0, 6.0d0 ]
    w    = 7
    call printcom()
end

subroutine printcom()
    implicit none
    integer          n( 2 ), w
    real             f( 2 )
    double precision d( 2 )
    common /mycom/ n, f, d, w

    print *, "(fort) current /mycom/"
    print *, "       n = ", n
    print *, "       f = ", f
    print *, "       d = ", d
    print *, "       w = ", w
end

test.py

from __future__ import print_function
import ctypes

N = 2

class Mycom( ctypes.Structure ):
    _fields_ = [ ( "x", ctypes.c_int    * N ),
                 ( "y", ctypes.c_float  * N ),
                 ( "z", ctypes.c_double * N ),
                 ( "w", ctypes.c_int        ) ]

mylib = ctypes.CDLL( "./mylib.so" )

mycom = Mycom.in_dll( mylib, "mycom_" )

initcom = mylib.initcom_
initcom()

print( " (python) current /mycom/" )
print( "          x = ", mycom.x[:] )
print( "          y = ", mycom.y[:] )
print( "          z = ", mycom.z[:] )
print( "          w = ", mycom.w    )

print( " (python) modifying /mycom/ ..." )
for i in range( N ):
    mycom.x[ i ] = (i + 1) * 10
    mycom.y[ i ] = (i + 1) * 100
    mycom.z[ i ] = (i + 1) * 0.1
mycom.w = 777

printcom = mylib.printcom_
printcom()

测试:

 $ python test.py

 (fort) initializing /mycom/
 (fort) current /mycom/
        n =            1           2
        f =    3.0000000       4.0000000    
        d =    5.0000000000000000        6.0000000000000000     
        w =            7
 (python) current /mycom/
          x =  [1, 2]
          y =  [3.0, 4.0]
          z =  [5.0, 6.0]
          w =  7
 (python) modifying /mycom/ ...
 (fort) current /mycom/
        n =           10          20
        f =    100.00000       200.00000    
        d =   0.10000000000000001       0.20000000000000001     
        w =          777