Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Use environment variables at load time in f2py code #24974

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 GitHub”, 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
HaoZeke opened this issue Oct 21, 2023 · 3 comments
Closed

ENH: Use environment variables at load time in f2py code #24974

HaoZeke opened this issue Oct 21, 2023 · 3 comments

Comments

@HaoZeke
Copy link
Member

HaoZeke commented Oct 21, 2023

...
@HaoZeke wanted to use environment variables in a different way, access them in a library compiled/linked with f2py and these set before loading the module - or used on the module in general. I had not found a way to change that in this case.

...

Originally posted by @2sn in #24874 (comment)

@rgommers
Copy link
Member

I skimmed the linked thread, but I don't really understand what the idea is here. It seems to have to do with FFLAGS specifically, but that's about all I can tell. Is this really a separate issue from gh-24974, and if so could this gain a self-contained problem statement and proposed solution?

@HaoZeke
Copy link
Member Author

HaoZeke commented Oct 21, 2023

I skimmed the linked thread, but I don't really understand what the idea is here. It seems to have to do with FFLAGS specifically, but that's about all I can tell. Is this really a separate issue from gh-24974, and if so could this gain a self-contained problem statement and proposed solution?

Sorry yeah this was not well phrased. The discussion on #24974 was about build systems. The last comment by @2sn as I understood it was related to a more fundamental problem, using environment variables in f2py code. This is part of the Fortran 2003 standard, GET_ENVIRONMENT_VARIABLE.

Though I'll close this since it is already supported 1.26.0:

! env_module.f90
MODULE env_module
  IMPLICIT NONE
CONTAINS
  SUBROUTINE get_env_value(varname, value)
    CHARACTER(LEN=*), INTENT(IN) :: varname
    CHARACTER(LEN=100), INTENT(OUT) :: value
    CHARACTER(LEN=100) :: buffer
    INTEGER :: status

    CALL GET_ENVIRONMENT_VARIABLE(varname, buffer, STATUS=status)

    IF (status == 0) THEN
      value = buffer
    ELSE
      value = 'NOT FOUND'
    END IF

  END SUBROUTINE get_env_value
END MODULE env_module

With f2py -c envlib.f90 -m envlib and:

In [1]: import envlib
   ...: import os
   ...: 
   ...: # Set an environment variable for testing
   ...: os.environ["TEST_VAR"] = "Hello, Fortran!"
   ...: 
   ...: # Retrieve the value using the Fortran subroutine
   ...: value = envlib.env_module.get_env_value("TEST_VAR")
   ...: print(f"Value of TEST_VAR: {value}")
   ...: 
   ...: value_not_found = envlib.env_module.get_env_value("NON_EXISTENT_VAR")
   ...: print(f"Value of NON_EXISTENT_VAR: {value_not_found}")
Value of TEST_VAR: b'Hello, Fortran!'
Value of NON_EXISTENT_VAR: b'NOT FOUND'

@2sn let me know if there was something else w.r.t. f2py and environment variables.

@HaoZeke HaoZeke closed this as completed Oct 21, 2023
@2sn
Copy link
Contributor

2sn commented Oct 21, 2023

@HaoZeke
Your example seems to test exactly what I was after.
Thanks!

I may have been using some different compatibility library with F77 at the time I tested it, Ila few yea s Back, but that should not matter.

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

No branches or pull requests

3 participants