I am attempting to wrap some fortran using python. This will ultimately involve the manipulation of strings and character arrays, so I thought I would try practicing on some simple stuff.
I am using gfortran 4.8.1, numpy 1.9.2, and python 3.4.3 on Windows 8
I created the following fortran code.
SUBROUTINE TESTER(my_string,a,b,c,d)
integer a,b,c,d
character(LEN=1000) :: my_string
C stuff for f2py.
Cf2py character, intent(in, out) :: my_string
Cf2py integer, intent(in, out) :: a
Cf2py integer, intent(in, out) :: b
Cf2py integer, intent(in, out) :: c
Cf2py integer, intent(in, out) :: d
a = 1
b = 2
c = 3
d = 4
write(6,*) 'my_string = ',TRIM(my_string)
my_string = TRIM(my_string)//'. I was added!'
write(6,*) 'my_string = ',TRIM(my_string)
end
SUBROUTINE TESTER2(charac)
implicit none
character(LEN=100), allocatable :: charac(:)
C stuff for f2py.
Cf2py character(LEN=100), allocatable, intent(in, out) :: charac(:)
ALLOCATE (charac(4))
charac(4) = 'I was added!'
end
The following is my python call.
import test
text = "my_string"
a = b = c = d = 0
print(text, a, b, c, d)
text, a, b, c, d = test.tester(text, a, b, c, d)
print(a, b, c, d)
print("text = ", text)
text2 = ["first", "second", "third"]
print(text2)
text2 = test.tester2(text2)
print(text2)
I have run into two problems.
1) When I pass the string text
to test.tester
, my expected output is my_string. I was added!
. However, I get b'my_string. I was added!
. Where is the b'
coming from and how do I get rid of it?
2) test.tester2
won't compile with f2py. I keep getting the error Tpye mismatch in argument 'charac': passed CHARACTER(1) to REAL(4)
. What have I done to cause this and how do I correct it?
Note: This is related to another question of mine Why does f2py not include all arguments?. I've been told what I'm trying to do here may correct that problem.