f90wrap icon indicating copy to clipboard operation
f90wrap copied to clipboard

Fix python keyword renaming

Open zhucaoxiang opened this issue 3 years ago • 6 comments

(This pull request sits for a long time in my fork)

When renaming the variable name to avoid using python keywords, the current code has a bug and it uses the same name for renaming. Thus it will lead to failure. This is fixed in this PR.

self.write_uses_lines(t, {t.name: ['%s_%s => %s' % (t.name, el.name, el.name)]})

zhucaoxiang avatar Dec 01 '21 16:12 zhucaoxiang

Looks good! Would you be willing to add/update a test in examples/ that would have failed before this fix?

jameskermode avatar Dec 01 '21 16:12 jameskermode

@jameskermode I think I might miss some other parts (or I lost something when merging). The following test doesn't pass. I will work on this later. Just post an example here in case you know how to fix it.

module parameters

    implicit none
    
    INTEGER, PARAMETER :: abc=0
    INTEGER, PARAMETER :: lambda=1
    
    contains
    
end module parameters

subroutine in(a)
    implicit none

    INTEGER, INTENT(INOUT) :: a

    a = 123

    return
end subroutine in

zhucaoxiang avatar Dec 01 '21 17:12 zhucaoxiang

I am close to finishing. I think I need some help with the following issue.

If a derived type is conflicting with python keywords, it should also be renamed. In f90warpgen.py, there are lines writing down the extra use lines.

        if isinstance(t, ft.Module):
            extra_uses[t.orig_name] = ['%s_%s => %s' % (t.name, el.orig_name, el.orig_name)]
        elif isinstance(t, ft.Type):
            extra_uses[self.types[t.name].mod_name] = [t.name]

If the derived type is conflicting with python keywords, the current implementation will cause the following error.

  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 503, in visit
    result = visitor(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 411, in visit_Type
    self._write_scalar_wrappers(node, el, self.sizeof_fortran_t)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 540, in _write_scalar_wrappers
    self._write_scalar_wrapper(t, element, sizeof_fortran_t, "get")
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 784, in _write_scalar_wrapper
    extra_uses[self.types[t.name].mod_name] = [t.name]
KeyError: 'class_'
make: *** [_keywordr_rename.so] Error 1

I think the last line should be revised to extra_uses[self.types[t.orig_name].mod_name] = [t.orig_name] and it works.

But I got the following error in the example of cylinder, example2, extends, etc.

f90wrap: KeyError('SolverOptionsDef')
         for help use --help
Traceback (most recent call last):
  File "/opt/anaconda3/bin/f90wrap", line 7, in <module>
    exec(compile(f.read(), __file__, 'exec'))
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/scripts/f90wrap", line 408, in <module>
    sys.exit(main())
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/scripts/f90wrap", line 387, in main
    fwrap.F90WrapperGenerator(prefix, fsize, string_lengths,
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 503, in visit
    result = visitor(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 106, in visit_Root
    self.generic_visit(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 511, in generic_visit
    self.visit(item)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 503, in visit
    result = visitor(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 122, in visit_Module
    self.generic_visit(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 511, in generic_visit
    self.visit(item)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/fortran.py", line 503, in visit
    result = visitor(node)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 411, in visit_Type
    self._write_scalar_wrappers(node, el, self.sizeof_fortran_t)
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 540, in _write_scalar_wrappers
    self._write_scalar_wrapper(t, element, sizeof_fortran_t, "get")
  File "/Users/caoxiang/Documents/Codes/f90wrap_zhu/f90wrap/f90wrapgen.py", line 784, in _write_scalar_wrapper
    extra_uses[self.types[t.orig_name].mod_name] = [t.orig_name]
KeyError: 'SolverOptionsDef'
make[1]: *** [_mockdt.so] Error 1
make: *** [test] Error 2

Apparently, when the derived type name is not in the python keywords, its orig_name is not saved in self.types. @jameskermode Do you know how to fix it?

zhucaoxiang avatar Dec 09 '21 17:12 zhucaoxiang

@jameskermode I am one of the users of the SPEC modeling code for which these fixes are necessary. Our team would be very grateful if this issue could be resolved soon. Is there anything we can do to support you on our way to merging these changes in to f90wrap (other than trying to fix the remaining bugs ourselves)?

jonathanschilling avatar Jan 12 '22 13:01 jonathanschilling

A quick fix is to ignore the case when the derived type is conflicting with python keywords. Then everything else should be working and the only limitation is that people cannot use a derived type name conflicting with python keywords.

zhucaoxiang avatar Jan 12 '22 13:01 zhucaoxiang

I will be happy to merge if this PR is updated to incorporate @zhucaoxiang's suggestion, provided that it doesn't cause any regressions: currently there are failing tests so this can't be included as is.

jameskermode avatar Jan 13 '22 16:01 jameskermode

Closing in favour of #194

jameskermode avatar Jun 19 '24 14:06 jameskermode