Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def visit_Argument(self, node):
if not hasattr(node, 'orig_name'):
node.orig_name = node.name
node.name = self.name_map.get(node.name, node.name)
if isinstance(node, ft.Argument):
# replace names in dimension attribute expressions
for (old_name, new_name) in self.name_map.items():
new_attribs = []
for attrib in node.attributes:
if attrib.startswith('dimension('):
new_attribs.append(attrib.replace(old_name, new_name))
else:
new_attribs.append(attrib)
node.attributes = new_attribs
return self.generic_visit(node)
ds = ArrayDimensionConverter.split_dimensions(dims[0])
new_dummy_args = []
new_ds = []
for i, d in enumerate(ds):
if ArrayDimensionConverter.valid_dim_re.match(d):
if d.startswith('len'):
arg.f2py_line = ('!f2py %s %s, dimension(%s) :: %s' % \
(arg.type,
','.join(
[attr for attr in arg.attributes if not attr.startswith('dimension')]),
d.replace('len', 'slen'), arg.name))
new_ds.append(d)
continue
dummy_arg = ft.Argument(name='n%d' % n_dummy, type='integer', attributes=['intent(hide)'])
if 'intent(out)' not in arg.attributes:
dummy_arg.f2py_line = ('!f2py intent(hide), depend(%s) :: %s = shape(%s,%d)' %
(arg.name, dummy_arg.name, arg.name, i))
new_dummy_args.append(dummy_arg)
new_ds.append(dummy_arg.name)
n_dummy += 1
if new_dummy_args != []:
logging.debug('adding dummy arguments %r to %s' % (new_dummy_args, node.name))
arg.attributes = ([attr for attr in arg.attributes if not attr.startswith('dimension')] +
['dimension(%s)' % ','.join(new_ds)])
node.arguments.extend(new_dummy_args)
for node in ft.walk(tree):
if not isinstance(node, ft.Type):
continue
for child in ft.iter_child_nodes(node):
if isinstance(child, ft.Procedure):
if 'constructor' in child.attributes:
logging.info('found constructor %s' % child.name)
break
else:
logging.info('adding missing constructor for %s' % node.name)
new_node = ft.Subroutine('%s_initialise' % node.name,
node.filename,
['Automatically generated constructor for %s' % node.name],
node.lineno,
[ft.Argument(name='this', # self.prefix + 'this' would probably be safer
filename=node.filename,
doc=['Object to be constructed'],
lineno=node.lineno,
attributes=['intent(out)'],
type='type(%s)' % node.name)],
node.uses,
['constructor', 'skip_call'],
mod_name=node.mod_name,
type_name=node.name)
new_node.method_name = '__init__'
node.procedures.append(new_node)
return tree
def write_constructor(self, node):
handle_arg = ft.Argument(name='handle',
filename=node.filename,
doc=['Opaque reference to existing derived type instance'],
lineno=node.lineno,
attributes=['intent(in)', 'optional'],
type='integer')
handle_arg.py_name = 'handle'
# special case for constructors: return value is 'self' argument,
# plus we add an extra optional argument
args = node.arguments + [handle_arg]
dct = dict(func_name=node.name,
prefix=self.prefix,
mod_name=self.f90_mod_name,
py_arg_names=', '.join(['%s%s' % (arg.py_name,
'optional' in arg.attributes and '=None' or '')
for node in ft.walk(tree):
if not isinstance(node, ft.Type):
continue
for child in ft.iter_child_nodes(node):
if isinstance(child, ft.Procedure):
if 'destructor' in child.attributes:
logging.info('found destructor %s' % child.name)
break
else:
logging.info('adding missing destructor for %s' % node.name)
new_node = ft.Subroutine('%s_finalise' % node.name,
node.filename,
['Automatically generated destructor for %s' % node.name],
node.lineno,
[ft.Argument(name='this', # self.prefix + 'this' would probably be safer
filename=node.filename,
doc=['Object to be destructed'],
lineno=node.lineno,
attributes=['intent(inout)'],
type='type(%s)' % node.name)],
node.uses,
['destructor', 'skip_call'],
mod_name=node.mod_name,
type_name=node.name)
new_node.method_name = '__del__'
node.procedures.append(new_node)
return tree
def __init__(self, name='', filename='', doc=None, lineno=0,
arguments=None, uses=None, attributes=None,
ret_val=None, ret_val_doc=None, mod_name=None, type_name=None):
Procedure.__init__(self, name, filename, doc,
lineno, arguments, uses, attributes,
mod_name, type_name)
if ret_val is None:
ret_val = Argument()
self.ret_val = ret_val
self.ret_val_doc = ret_val_doc