8000 add __f2py_numpy_version__ attribute · numpy/numpy@908d865 · GitHub
[go: up one dir, main page]

Skip to content

Commit 908d865

Browse files
committed
add __f2py_numpy_version__ attribute
1 parent 1fb7047 commit 908d865

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

numpy/f2py/rules.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
from . import __version__
5656
f2py_version = __version__.version
5757

58+
from .. import version as _numpy_version
59+
numpy_version = _numpy_version.version
60+
5861
import os
5962
import time
6063
import copy
@@ -206,6 +209,9 @@
206209
\t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\");
207210
\tPyDict_SetItemString(d, \"__doc__\", s);
208211
\tPy_DECREF(s);
212+
\ts = PyUnicode_FromString(\"""" + numpy_version + """\");
213+
\tPyDict_SetItemString(d, \"__f2py_numpy_version__\", s);
214+
\tPy_DECREF(s);
209215
\t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL);
210216
\t/*
211217
\t * Store the error object inside the dict, so that it could get deallocated.

numpy/f2py/tests/test_regression.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import numpy as np
5-
from numpy.testing import assert_raises, assert_equal
5+
from numpy.testing import assert_, assert_raises, assert_equal, assert_string_equal
66

77
from . import util
88

@@ -25,3 +25,23 @@ def test_inout(self):
2525
x = np.arange(3, dtype=np.float32)
2626
self.module.foo(x)
2727
assert_equal(x, [3, 1, 2])
28+
29+
30+
class TestNumpyVersionAttribute(util.F2PyTest):
31+
# Check that th attribute __f2py_numpy_version__ is present
32+
# in the compiled module and that has the value np.__version__.
33+
sources = [_path('src', 'regression', 'inout.f90')]
34+
35+
@pytest.mark.slow
36+
def test_numpy_version_attribute(self):
37+
38+
# Check that self.module has an attribute named "__f2py_numpy_version__"
39+
assert_(hasattr(self.module, "__f2py_numpy_version__"),
40+
msg="Fortran module does not have __f2py_numpy_version__")
41+
42+
# Check that the attribute __f2py_numpy_version__ is a string
43+
assert_(isinstance(self.module.__f2py_numpy_version__, str),
44+
msg="__f2py_numpy_version__ is not a string")
45+
46+
# Check that __f2py_numpy_version__ has the value numpy.__version__
47+
assert_string_equal(np.__version__, self.module.__f2py_numpy_version__)

0 commit comments

Comments
 (0)
0