Derived types containing allocatable character arrays
Borrdom opened this issue · comments
Hey,
i wanted to share my observations when using derived types that contain allocatable character arrays.
something like this:
type :: chartype
CHARACTER(7), ALLOCATABLE :: names(:)
end type chartype
I attached a full example as a zip file. With source .f90 files, a setup.py for compiling/wrapping as well as a test.py file.
Say chartype%names is allocated in fortran to become an array of dimension 2 storing the words "default" twice. Then, within python, chartype.names can be accessed as a numpy array of size (7,2), where the 7 additional elements result from the character being split into each individual letter (as discussed in a previous issue #138 ).
The expected result:
print(chartype.names) =>[['d','e','f'','a','u','l','t'],['d','e','f'','a','u','l','t']]
The result i got:
print(chartype.names) => [[100,101,102,97,117,108,116],[100,101,102,97,117,108,116]]
The observation:
These numbers are in fact decimal numbers directly corresponding their individual ASCII character.
So 'd'=>100 'e'=>100 and so on.
This conversion works the other way around, so I constructed a workaround for the wrapper on the python side.
Say from the python side we have the array ["william","michael"]. We can convert this into an array chartype.names= [[119,105,108,108,105,97,109],[109,105,99,104,97,101,108]] which can then be correctly interpreted in fortran.