10000 BUG: f2py in numpy 2.0.1 does not expose variables to Python in modules containing only assignments. · Issue #27167 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: f2py in numpy 2.0.1 does not expose variables to Python in modules containing only assignments. #27167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for G 8000 itHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
P-Kaempf opened this issue Aug 10, 2024 · 9 comments · Fixed by #27730

Comments

@P-Kaempf
Copy link
Contributor
P-Kaempf commented Aug 10, 2024

Describe the issue:

Take this F90 module:

MODULE F_GLOBALS
     IMPLICIT NONE
     INTEGER, PARAMETER :: N_MAX = 16
     INTEGER, PARAMETER :: I_MAX = 18
     INTEGER, PARAMETER :: J_MAX = 72
     ...
END MODULE F_GLOBALS

When compiled with f2py 2.0.1 and included as "f_test" in a Python script, this:

import f_test as f
maxN = f.f_globals.n_max
maxI = f.f_globals.i_max
maxJ = f.f_globals.j_max

will cause this error message:

AttributeError: module 'f_test' has no attribute 'f_globals'

Reproduce the code example:

> f2py -c -m f_test F_GLOBALS.f90

import f_test as f
maxN = f.f_globals.n_max
maxI = f.f_globals.i_max
maxJ = f.f_globals.j_max

Error message:

AttributeError: module 'f_test' has no attribute 'f_globals'

Python and NumPy Versions:

Python 3.12.4
numpy 2.0.1
Fortran compiler for the host machine: gfortran (gcc 14.1.0 "GNU Fortran (Homebrew GCC 14.1.0_2) 14.1.0")
Fortran linker for the host machine: gfortran ld64 1053.12
C compiler for the host machine: cc (clang 15.0.0 "Apple clang version 15.0.0 (clang-1500.3.9.4)")
C linker for the host machine: cc ld64 1053.12
Host machine cpu family: aarch64
Host machine cpu: aarch64

import sys
print(numpy.version)
2.0.1
print(sys.version)
3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)]

Runtime Environment:

Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import numpy
numpy.show_runtime()
[{'numpy_version': '2.0.1',
'python': '3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 '
'(clang-1500.3.9.4)]',
'uname': uname_result(system='Darwin', node='MBP2023.fritz.box', release='23.5.0', version='Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}},
{'architecture': 'neoversen1',
'filepath': '/opt/homebrew/Cellar/openblas/0.3.27/lib/libopenblasp-r0.3.27.dylib',
'internal_api': 'openblas',
'num_threads': 10,
'prefix': 'libopenblas',
'threading_layer': 'openmp',
'user_api': 'blas',
'version': '0.3.27'},
{'filepath': '/opt/homebrew/Cellar/gcc/14.1.0_2/lib/gcc/current/libgomp.1.dylib',
'internal_api': 'openmp',
'num_threads': 10,
'prefix': 'libgomp',
'user_api': 'openmp',
'version': None}]

Context for the issue:

Code which ran fine in numpy 1.26 fails to run in numpy 2.0.1.
A simple if ugly workaround is to add some executable code within the module. Only then will it become visible in Python, including all the assigned variables..

@jorenham jorenham changed the title f2py in numpy 2.0.1 does not expose variables to Python in modules containing only assignments. BUG: f2py in numpy 2.0.1 does not expose variables to Python in modules containing only assignments. Aug 16, 2024
@dionhaefner
Copy link

Running into the same issue here. Our CI (which tests against a Fortran reference code) has been broken since NumPy 2.0.

@charris
Copy link
Member
charris commented Aug 19, 2024

@HaoZeke Ping.

@charris charris added this to the 2.1.1 release milestone Aug 19, 2024
@P-Kaempf
Copy link
Contributor Author

Until a fix comes around, you can always add some bogus executable statements to the assignment module. They do not even need to be reachable. Worked fine for me.

@charris charris modified the milestones: 2.1.1 release, 2.1.2 release Sep 2, 2024
@ngoldbaum
Copy link
Member
ngoldbaum commented Sep 20, 2024 • 8000

I can't reproduce this on NumPy main, I think this might be fixed:

goldbaum at Nathans-MBP in ~/Documents/f2py-test on main!
± cat test.py
import f_test as f
maxN = f.f_globals.n_max
maxI = f.f_globals.i_max
maxJ = f.f_globals.j_max

goldbaum at Nathans-MBP in ~/Documents/f2py-test on main!
± f2py -c -m f_test F_GLOBALS.f90
Cannot use distutils backend with Python>=3.12, using meson backend instead.
Using meson backend
Will pass --lower to f2py
See https://numpy.org/doc/stable/f2py/buildtools/meson.html
Reading fortran codes...
	Reading file 'F_GLOBALS.f90' (format:free)
Post-processing...
	Block: f_test
			Block: f_globals
Applying post-processing hooks...
  character_backward_compatibility_hook
Post-processing (stage 2)...
	Block: f_test
		Block: unknown_interface
			Block: f_globals
Building modules...
    Building module "f_test"...
		Constructing F90 module support for "f_globals"...
		  Variables: n_max i_max j_max
    Wrote C/API module "f_test" to file "./f_testmodule.c"
    Fortran 90 wrappers are saved to "./f_test-f2pywrappers2.f90"
The Meson build system
Version: 1.4.0
Source dir: /private/var/folders/nk/yds4mlh97kg9qdq745g715rw0000gn/T/tmpkizrdx_4
Build dir: /private/var/folders/nk/yds4mlh97kg9qdq745g715rw0000gn/T/tmpkizrdx_4/bbdir
Build type: native build
Project name: f_test
Project version: 0.1
Fortran compiler for the host machine: gfortran (gcc 14.2.0 "GNU Fortran (Homebrew GCC 14.2.0) 14.2.0")
Fortran linker for the host machine: gfortran ld64 1053.12
C compiler for the host machine: ccache cc (clang 15.0.0 "Apple clang version 15.0.0 (clang-1500.3.9.4)")
C linker for the host machine: cc ld64 1053.12
Host machine cpu family: aarch64
Host machine cpu: aarch64
Program /Users/goldbaum/.pyenv/versions/3.12.3/bin/python found: YES (/Users/goldbaum/.pyenv/versions/3.12.3/bin/python)
Found pkg-config: YES (/opt/homebrew/bin/pkg-config) 0.29.2
Run-time dependency python found: YES 3.12
Library quadmath found: YES
Build targets in project: 1

Found ninja-1.11.1.git.kitware.jobserver-1 at /Users/goldbaum/.pyenv/versions/3.12.3/bin/ninja
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /Users/goldbaum/.pyenv/versions/3.12.3/bin/ninja -C /private/var/folders/nk/yds4mlh97kg9qdq745g715rw0000gn/T/tmpkizrdx_4/bbdir
ninja: Entering directory `/private/var/folders/nk/yds4mlh97kg9qdq745g715rw0000gn/T/tmpkizrdx_4/bbdir'
[6/6] Linking target f_test.cpython-312-darwin.so

goldbaum at Nathans-MBP in ~/Documents/f2py-test on main!
± python test.py

@P-Kaempf is there any chance you can try again using Numpy 2.1.1? It looks like it's fixed there as well.

@P-Kaempf
Copy link
Contributor Author
P-Kaempf commented Sep 23, 2024 via email

@ngoldbaum
Copy link
Member

Does the example in the issue trigger the bug for you? The output you shared looks like it's about different code. If it doesn't can you share an example I can trigger the issue with?

If you are still able to reproduce the issue as described, then either I made a mistake trying to reproduce it, or there's some difference in our environments.

For the non-ASCII character thing, please file a separate issue about that so it doesn't get lost, assuming there isn't an open issue already.

@P-Kaempf
Copy link
Contributor Author

The example is an excerpt from a much longer code file. Indeed, if I run this short example, it works now as advertised.

I need to isolate the issue better. I cannot post an example with more than 6300 lines!

@P-Kaempf
Copy link
Contributor Author

The sample needs some executable added. Here is the minimum viable code for reproducing the issue:

Fortran:

      MODULE MOD_TYPES
        INTEGER, PARAMETER :: SP = SELECTED_REAL_KIND(6, 37)
        INTEGER, PARAMETER :: DP = SELECTED_REAL_KIND(15, 307)
      END MODULE
!
      MODULE F_GLOBALS
         USE MOD_TYPES
         IMPLICIT NONE
         INTEGER, PARAMETER :: N_MAX = 16
         INTEGER, PARAMETER :: I_MAX = 18
         INTEGER, PARAMETER :: J_MAX = 72
         REAL(SP) :: XREF
      END MODULE F_GLOBALS
!
       SUBROUTINE DUMMY ()
!
       USE F_GLOBALS
       USE MOD_TYPES
       IMPLICIT NONE
!
       REAL(SP) :: MINIMAL
       MINIMAL = 0.01*XREF
       RETURN
!
       END SUBROUTINE DUMMY

Python:

import f_globals as f

def test():
        maxN = f.f_globals.n_max
        maxI = f.f_globals.i_max
        maxJ = f.f_globals.j_max
        print("maxN = ", maxN, maxI, maxJ)

test()

peter@MBP2023 geo % python test.py
Traceback (most recent call last):
File "/Users/peter/Applications/Python/PyVFE/geo/test.py", line 9, in
test()
File "/Users/peter/Applications/Python/PyVFE/geo/test.py", line 4, in test
maxN = f.f_globals.n_max
^^^^^^^^^^^
AttributeError: module 'f_globals' has no attribute 'f_globals'

@charris charris modified the milestones: 2.1.2 release, 2.1.3 release Oct 5, 2024
@charris charris modified the milestones: 2.1.3 release, 2.2.0 release Nov 2, 2024
@HaoZeke
Copy link
Member
HaoZeke commented Nov 10, 2024

This is fixed by #27695

From the fortran code in #27167 (comment)

saved to a file fbug.f90 and compiled with:

python -m numpy.f2py -c -m fbug fbug.f90

The function works as expected.

In [1]: import fbug as f

In [2]: def test():
   ...:         maxN = f.f_globals.n_max
   ...:         maxI = f.f_globals.i_max
   ...:         maxJ = f.f_globals.j_max
   ...:         print("maxN = ", maxN, maxI, maxJ)
   ...: 
   ...: test()
maxN =  16 18 72

I'll leave this open since I want to add a test and a release note for #27695 anyway.

EDIT: @P-Kaempf did you open an issue for the ASCII thing suggested by @ngoldbaum?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
0