summaryrefslogtreecommitdiff
path: root/numpy/f2py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/f2py')
-rwxr-xr-xnumpy/f2py/crackfortran.py34
1 files changed, 19 insertions, 15 deletions
diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py
index b831697d8..84579fdcc 100755
--- a/numpy/f2py/crackfortran.py
+++ b/numpy/f2py/crackfortran.py
@@ -147,7 +147,6 @@ import os
import copy
import platform
import codecs
-from pathlib import Path
try:
import charset_normalizer
except ImportError:
@@ -290,15 +289,22 @@ def undo_rmbadname(names):
return [undo_rmbadname1(_m) for _m in names]
+def getextension(name):
+ i = name.rfind('.')
+ if i == -1:
+ return ''
+ if '\\' in name[i:]:
+ return ''
+ if '/' in name[i:]:
+ return ''
+ return name[i + 1:]
+
+is_f_file = re.compile(r'.*\.(for|ftn|f77|f)\Z', re.I).match
_has_f_header = re.compile(r'-\*-\s*fortran\s*-\*-', re.I).search
_has_f90_header = re.compile(r'-\*-\s*f90\s*-\*-', re.I).search
_has_fix_header = re.compile(r'-\*-\s*fix\s*-\*-', re.I).search
_free_f90_start = re.compile(r'[^c*]\s*[^\s\d\t]', re.I).match
-# Extensions
-COMMON_FREE_EXTENSIONS = ['.f90', '.f95', '.f03', '.f08']
-COMMON_FIXED_EXTENSIONS = ['.for', '.ftn', '.f77', '.f']
-
def openhook(filename, mode):
"""Ensures that filename is opened with correct encoding parameter.
@@ -331,28 +337,26 @@ def openhook(filename, mode):
return open(filename, mode, encoding=encoding)
-def is_free_format(fname):
+def is_free_format(file):
"""Check if file is in free format Fortran."""
# f90 allows both fixed and free format, assuming fixed unless
# signs of free format are detected.
- result = False
- if Path(fname).suffix.lower() in COMMON_FREE_EXTENSIONS:
- result = True
- with openhook(fname, 'r') as fhandle:
- line = fhandle.readline()
+ result = 0
+ with openhook(file, 'r') as f:
+ line = f.readline()
n = 15 # the number of non-comment lines to scan for hints
if _has_f_header(line):
n = 0
elif _has_f90_header(line):
n = 0
- result = True
+ result = 1
while n > 0 and line:
if line[0] != '!' and line.strip():
n -= 1
if (line[0] != '\t' and _free_f90_start(line[:5])) or line[-2:-1] == '&':
- result = True
+ result = 1
break
- line = fhandle.readline()
+ line = f.readline()
return result
@@ -408,7 +412,7 @@ def readfortrancode(ffile, dowithline=show, istop=1):
strictf77 = 0
sourcecodeform = 'fix'
ext = os.path.splitext(currentfilename)[1]
- if Path(currentfilename).suffix.lower() in COMMON_FIXED_EXTENSIONS and \
+ if is_f_file(currentfilename) and \
not (_has_f90_header(l) or _has_fix_header(l)):
strictf77 = 1
elif is_free_format(currentfilename) and not _has_fix_header(l):