diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-03-10 18:19:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-10 18:19:14 -0700 |
commit | 9d69ad78cdcb93b378a734e938372941cb3e9a23 (patch) | |
tree | 165b08f25c103c14e38d1d0832ba70bdfb8a5161 | |
parent | 33756c98d7da3ff1042c903ea7db971ff9a64072 (diff) | |
parent | 025733bae3dae4200577cb35ef3229cc0f47a5b2 (diff) | |
download | python-numpy-9d69ad78cdcb93b378a734e938372941cb3e9a23.tar.gz python-numpy-9d69ad78cdcb93b378a734e938372941cb3e9a23.tar.bz2 python-numpy-9d69ad78cdcb93b378a734e938372941cb3e9a23.zip |
Merge pull request #10720 from WarrenWeckesser/fix-gh-10712
BUG: distutils: Remove named templates from the processed output (fix gh-10712)
-rw-r--r-- | numpy/distutils/from_template.py | 12 | ||||
-rw-r--r-- | numpy/distutils/tests/test_from_template.py | 48 |
2 files changed, 57 insertions, 3 deletions
diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 8f587eab9..65c60c498 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -98,6 +98,11 @@ def find_repl_patterns(astr): names[name] = thelist return names +def find_and_remove_repl_patterns(astr): + names = find_repl_patterns(astr) + astr = re.subn(named_re, '', astr)[0] + return astr, names + item_re = re.compile(r"\A\\(?P<index>\d+)\Z") def conv(astr): b = astr.split(',') @@ -186,7 +191,7 @@ def expand_sub(substr, names): def process_str(allstr): newstr = allstr - writestr = '' #_head # using _head will break free-format files + writestr = '' struct = parse_structure(newstr) @@ -194,8 +199,9 @@ def process_str(allstr): names = {} names.update(_special_names) for sub in struct: - writestr += newstr[oldend:sub[0]] - names.update(find_repl_patterns(newstr[oldend:sub[0]])) + cleanedstr, defs = find_and_remove_repl_patterns(newstr[oldend:sub[0]]) + writestr += cleanedstr + names.update(defs) writestr += expand_sub(newstr[sub[0]:sub[1]], names) oldend = sub[1] writestr += newstr[oldend:] diff --git a/numpy/distutils/tests/test_from_template.py b/numpy/distutils/tests/test_from_template.py new file mode 100644 index 000000000..d3c513437 --- /dev/null +++ b/numpy/distutils/tests/test_from_template.py @@ -0,0 +1,48 @@ + +from numpy.distutils.from_template import process_str +from numpy.testing import assert_equal, run_module_suite + + +pyf_src = """ +python module foo + <_rd=real,double precision> + interface + subroutine <s,d>foosub(tol) + <_rd>, intent(in,out) :: tol + end subroutine <s,d>foosub + end interface +end python module foo +""" + +expected_pyf = """ +python module foo + interface + subroutine sfoosub(tol) + real, intent(in,out) :: tol + end subroutine sfoosub + subroutine dfoosub(tol) + double precision, intent(in,out) :: tol + end subroutine dfoosub + end interface +end python module foo +""" + + +def normalize_whitespace(s): + """ + Remove leading and trailing whitespace, and convert internal + stretches of whitespace to a single space. + """ + return ' '.join(s.split()) + + +def test_from_template(): + """Regression test for gh-10712.""" + pyf = process_str(pyf_src) + normalized_pyf = normalize_whitespace(pyf) + normalized_expected_pyf = normalize_whitespace(expected_pyf) + assert_equal(normalized_pyf, normalized_expected_pyf) + + +if __name__ == "__main__": + run_module_suite() |