Skip to content

Commit

Permalink
Add support for calling things with mixed cased names
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
rjfarmer committed Feb 27, 2024
1 parent b2fe00b commit 59b8b71
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gfort2py/gfort2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ def __dir__(self):
return self.keys()

def __getattr__(self, key):
k = key
key = key.lower()

if key in self.__dict__:
return self.__dict__[key]

if "_initialized" in self.__dict__:
if self._initialized:
if key not in self.keys():
raise AttributeError(f"{self._mod_file} has no attribute {key}")
raise AttributeError(f"{self._mod_file} has no attribute {k}")

if self._module[key].is_variable():
if key not in self._saved:
Expand All @@ -98,6 +101,9 @@ def __getattr__(self, key):
)

def __setattr__(self, key, value):
k = key
key = key.lower()

if key in self.__dict__:
self.__dict__[key] = value
return
Expand Down
10 changes: 10 additions & 0 deletions tests/basic.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module basic
real :: a_real_set=7.0
real(dp) :: a_real_dp_set=8.0_dp

! Test Mixed Case variables
integer :: a_int_MIXED
integer :: const_int_MIXED = 1


contains

Expand Down Expand Up @@ -235,4 +239,10 @@ logical function func_test_bool(x)

end function func_test_bool


integer function func_TEST_CASE()
func_TEST_CASE = 1
end function func_TEST_CASE


end module basic
18 changes: 18 additions & 0 deletions tests/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,21 @@ def test_func_check_mod(self):
x.a_real_dp = 5.0

assert x.func_check_mod().result

def test_mixed_case(self):

assert x.const_int_MIXED == 1
assert x.const_int_mixed == 1
assert x.CONST_INT_MIXED == 1

x.a_int_MIXED = 5
assert x.a_int_MIXED == 5

assert x.a_int_mixed == 5

x.a_int_mixed = 6
assert x.A_INT_MIXED == 6

assert x.func_TEST_CASE().result
assert x.func_test_case().result
assert x.FUNC_test_case().result

0 comments on commit 59b8b71

Please sign in to comment.