Gfort2py is a Python to Fortran interface library designed to make it easy to call modern Fortran from Python. Features including getting/setting module variables and calling procedures inside a module with both scalars and array (explicit, assumed shape, assumed size, allocatable, and assumed rank) as arguments and return values. It does this by using Gfortran’s .mod file to provide details of your codes API, meaning as a user you don’t need make any changes to your Fortran code or have to have any deep knowledge of how to convert something between Python and Fortran, as long as your code sits in a Fortran module.
New features include:
Support for gfortran 15 and above
Support for allocatable arrays of derived types
Support assumed rank arrays
Support unicode ‘ISO_10646’ (scalars and arrays)
Support quad precision arrays (scalars already supported)
Support type bound procedures (both PASS and NOPASS)
Note for quad precision support you require an extra package, pyQuadP (python -m pip install --upgrade pyquadp). As part of this version I also extracted the Gfortran module handling code into a separate library gfModParser (https://github.com/rjfarmer/gfModParser) which can be used standalone to explore Gfortran’s module files.
Bug reports and feature requests are welcome as Github issues.
Yes it can call fortran type bound methods. Assuming you have a type(x) with type bound methods then you can just do x.method(arg1,arg2…) and it handles passing the type where needed.
A program that uses this module is allowed to reference and modify that variable. So even if it is unused within the module, there is the possibility that it could be used elsewhere, so there are no compiler warnings printed for this variable declaration. If you were to add the private attribute to that variable, so that it can no longer be accessed from outside the module, then a compiler can see that the declaration is unnecessary:
At the moment there’s no simple way. Either don’t comment out the module level variable or you could write a Fortran subroutine that has a type(circle),intent(out) argument. Then call the subroutine with an empty dict and you’ll get back a fDT object which you can then use in Python.
function class_name()
character(:),allocatable::class_name
!character(255)::class_name
class_name="Circle!"
end function class_name
z=mod.class_name()
print(z.result)
Traceback (most recent call last):
File “F:\wxprogram\python program\oop2\circle_mod.py”, line 31, in
z=mod.class_name()
File “C:\Users\weixi\AppData\Roaming\Python\Python313\site-packages\gfort2py\procedures\procedures.py”, line 95, in call
self.result = self._proc(*all_args)
~~~~~~~~~~^^^^^^^^^^^
OSError: exception: access violation writing 0x0000000000000001
module add_mod
implicit none
contains
function add(x) result(res)
real(kind=8), intent(in) :: x(:)
!real(kind=8), intent(in) :: x(*)
real(kind=8) :: res
res = x(1)+x(2)
end function add
end module add_mod
I am also very interested in Gfort2py (so reading this thread), but I guess it would be useful to add a bit more “self-contained” information about the issue (e.g., like the “minimal working example (MWE)”, so that anyone can try it with their environment). For more detailed descriptions of the code, it might also be useful to use machine translation etc to translate non-English comments to English (using some “auto-translation” feature in the Discourse, if available?), so that anyone can understand them easily (e.g. like “返回当前实例对象”, possibly meaning “returns xxx of an instance”…?)