Skip to content
Browse files

Merge pull request #125 from adamreeve/python

Bindings generation updates
  • Loading branch information...
2 parents aa6eab4 + 05937eb commit 35b6675727dc576bb8931b8846d9616e7906d648 @chrispbradley chrispbradley committed Oct 4, 2012
View
51 bindings/generate_bindings/parse.py
@@ -84,7 +84,7 @@ class PublicFinder(LineFinder):
def add(self, match, line_number):
for symbol in match.group(1).split(','):
- self.source_file.public.append(symbol.strip())
+ self.source_file.public.add(symbol.strip())
class ConstantFinder(LineFinder):
line_re = re.compile(
@@ -124,19 +124,19 @@ def __init__(self, source_file, params_only=False):
"""
self.file_path = source_file
- self.public = []
+ self.public = IdentifierSet()
self.doxygen_groupings = []
- self.interfaces = {}
- self.subroutines = {}
- self.constants = {}
- self.types = {}
+ self.interfaces = IdentifierDict()
+ self.subroutines = IdentifierDict()
+ self.constants = IdentifierDict()
+ self.types = IdentifierDict()
self.parse_file(params_only)
def parse_file(self, params_only=False):
"""Run through file once, getting everything we'll need"""
source_lines = _join_lines(
- open(self.file_path, 'r').read()).splitlines(True)
+ open(self.file_path, 'r').read()).splitlines()
if not params_only:
# only keep the source_lines if we need them
self.source_lines = source_lines
@@ -250,8 +250,8 @@ def __init__(self, cm_path):
def resolve_constants(self):
"""Go through all public constants and work out their actual values"""
- for pub in self.lib_source.public:
- if pub in self.lib_source.constants:
+ for pub in self.lib_source.constants:
+ if pub in self.lib_source.public:
self.get_constant_value(pub)
def get_constant_value(self, constant):
@@ -484,6 +484,9 @@ def filter_match(string):
r'([A-Z0-9_]+)\(([A-Z0-9_,\*\s]*)\)',
self.lines[0],
re.IGNORECASE)
+ if not match:
+ raise ValueError(
+ "Could not read subroutine line:\n %s" % self.lines[0])
parameters = [p.strip() for p in match.group(3).split(',')]
try:
parameters.remove('Err')
@@ -716,4 +719,32 @@ class UnsupportedParameterError(Exception):
def _join_lines(source):
"""Remove Fortran line continuations"""
- return re.sub(r'[\t ]*&[\t ]*\n[\t ]*&[\t ]*', ' ', source)
+ return re.sub(r'[\t ]*&[\t ]*[\r\n]+[\t ]*&[\t ]*', ' ', source)
+
+
+class IdentifierDict(dict):
+ """Dictionary used to store Fortran identifiers, to allow
+ getting items with case insensitivity"""
+
+ def __getitem__(self, key):
+ try:
+ val = dict.__getitem__(self, key)
+ except KeyError:
+ for ikey in self:
+ if ikey.lower() == key.lower():
+ val = dict.__getitem__(self, ikey)
+ break
+ else:
+ raise
+ return val
+
+
+class IdentifierSet(set):
+ """Set used to store Fortran identifiers, to allow
+ checking for items with case insensitivity"""
+
+ def add(self, val):
+ set.add(self, val.lower())
+
+ def __contains__(self, val):
+ return set.__contains__(self, val.lower())
View
10 bindings/generate_bindings/tests/test_c.py
@@ -114,16 +114,6 @@ def test_array_parameters(self):
self.assertEqual(c_result, c_expected)
self.assertEqual(cf90_result, cf90_expected)
- # Input array pointer
- c_result = parameter_to_c(m.input_array_pointer)
- c_expected = ["const int testSize", "const int *test"]
- cf90_result = parameter_c_f90_declaration(m.input_array_pointer)
- cf90_expected = [
- "INTEGER(C_INT), VALUE, INTENT(IN) :: testSize",
- "TYPE(C_PTR), VALUE, INTENT(IN) :: testPtr"]
- self.assertEqual(c_result, c_expected)
- self.assertEqual(cf90_result, cf90_expected)
-
# Output array pointer
c_result = parameter_to_c(m.output_array_pointer)
c_expected = ["int *testSize", "int **test"]
View
27 bindings/generate_bindings/tests/test_swig.py
@@ -26,7 +26,7 @@ def test_scalar_parameters(self):
# Output integer
result = parameter_swig_lines(m.output_integer)
expected = typemap_apply(
- "int *DummyOutputInt",
+ "int *DummyOutputScalar",
"int *test")
self.assertEqual(result[0], expected)
@@ -38,7 +38,7 @@ def test_scalar_parameters(self):
# Output real
result = parameter_swig_lines(m.output_real)
expected = typemap_apply(
- "double *DummyOutputDouble",
+ "double *DummyOutputScalar",
"double *test")
self.assertEqual(result[0], expected)
@@ -48,44 +48,37 @@ def test_array_parameters(self):
# Input 1D array
result = parameter_swig_lines(m.input_array)
expected = typemap_apply(
- "const int ArraySize, const int *DummyInputArray",
+ "const int DIM1, const int *IN_ARRAY1",
"const int testSize, const int *test")
self.assertEqual(result[0], expected)
# Input 2D array
result = parameter_swig_lines(m.input_array_2d)
expected = typemap_apply(
- "const int ArraySize1, const int ArraySize2, "
- "const int *DummyInputArray",
+ "const int DIM1, const int DIM2, "
+ "const int *IN_FARRAY2",
"const int testSize1, const int testSize2, const int *test")
self.assertEqual(result[0], expected)
# Input array of known size
result = parameter_swig_lines(m.output_array_known_size)
expected = typemap_apply(
- "double *DummyOutputArraySize2",
- "double *test")
+ "double ARGOUT_ARRAY1[ANY]",
+ "double test[2]")
self.assertEqual(result[0], expected)
# Output array
result = parameter_swig_lines(m.output_array)
expected = typemap_apply(
- "const int ArraySize, int *DummyOutputArray",
+ "const int DIM1, int *ARGOUT_ARRAY1",
"const int testSize, int *test")
self.assertEqual(result[0], expected)
- # Input array pointer
- result = parameter_swig_lines(m.input_array_pointer)
- expected = typemap_apply(
- "const int ArraySize, const int *DummyInputArray",
- "const int testSize, const int *test")
- self.assertEqual(result[0], expected)
-
# Output array pointer
result = parameter_swig_lines(m.output_array_pointer)
expected = typemap_apply(
- "const int ArraySize, int *DummyOutputArray",
- "const int testSize, int *test")
+ "int *DIM1, int **ARGOUTVIEW_ARRAY1",
+ "int *testSize, int **test")
self.assertEqual(result[0], expected)
def test_string_parameters(self):

0 comments on commit 35b6675

Please sign in to comment.
Something went wrong with that request. Please try again.