8000 ENH: Add ``__f2py_numpy_version__`` attribute to Fortran modules. by sturlamolden · Pull Request #16594 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add __f2py_numpy_version__ attribute to Fortran modules. #16594

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

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions numpy/f2py/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
from . import __version__
f2py_version = __version__.version

from .. import version as _numpy_version
numpy_version = _numpy_version.version

import os
import time
import copy
Expand Down Expand Up @@ -206,6 +209,9 @@
\t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\");
\tPyDict_SetItemString(d, \"__doc__\", s);
\tPy_DECREF(s);
\ts = PyUnicode_FromString(\"""" + numpy_version + """\");
\tPyDict_SetItemString(d, \"__f2py_numpy_version__\", s);
\tPy_DECREF(s);
\t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL);
\t/*
\t * Store the error object inside the dict, so that it could get deallocated.
Expand Down
22 changes: 21 additions & 1 deletion numpy/f2py/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

import numpy as np
from numpy.testing import assert_raises, assert_equal
from numpy.testing import assert_, assert_raises, assert_equal, assert_string_equal

from . import util

Expand All @@ -25,3 +25,23 @@ def test_inout(self):
x = np.arange(3, dtype=np.float32)
self.module.foo(x)
assert_equal(x, [3, 1, 2])


class TestNumpyVersionAttribute(util.F2PyTest):
# Check that th attribute __f2py_numpy_version__ is present
# in the compiled module and that has the value np.__version__.
sources = [_path('src', 'regression', 'inout.f90')]

@pytest.mark.slow
def test_numpy_version_attribute(self):

# Check that self.module has an attribute named "__f2py_numpy_version__"
assert_(hasattr(self.module, "__f2py_numpy_version__"),
msg="Fortran module does not have __f2py_numpy_version__")

# Check that the attribute __f2py_numpy_version__ is a string
assert_(isinstance(self.module.__f2py_numpy_version__, str),
msg="__f2py_numpy_version__ is not a string")

# Check that __f2py_numpy_version__ has the value numpy.__version__
assert_string_equal(np.__version__, self.module.__f2py_numpy_version__)
0