SMAT-Lab / Scalpel

Scalpel: The Python Static Analysis Framework
Apache License 2.0
278 stars 42 forks source link

Creating CFG fails #43

Closed simisimon closed 2 years ago

simisimon commented 2 years ago

With the following source code from the repository Conditional_Density_Estimation, the creation of the control flow graph fails.

def initialize_models(model_dict, verbose=False, model_name_prefix=''):
    ''' make kartesian product of listed parameters per model '''
    model_configs = {}
    for model_key, conf_dict in model_dict.items():
        model_configs[model_key] = [dict(zip(conf_dict.keys(), value_tuple)) for value_tuple in
                                    list(itertools.product(*list(conf_dict.values())))]

    """ initialize models """
    configs_initialized = {}
    for model_key, model_conf_list in model_configs.items():
        configs_initialized[model_key] = []
        for i, conf in enumerate(model_conf_list):
            conf['name'] = model_name_prefix + model_key.replace(' ', '_') + '_%i' % i
            if verbose: print("instantiating ", conf['name'])
            """ remove estimator entry from dict to instantiate it"""
            estimator = conf.pop('estimator')
            configs_initialized[model_key].append(globals()[estimator](**conf))
    return configs_initialized

Here is the corresponding stack trace. I think the line configs_initialized[model_key].append(globals()[estimator](**conf)) causes the error.

  File "D:\GitHub\test\test_scalpel.py", line 91, in <module>
    main()
  File "D:\GitHub\test\test_scalpel.py", line 66, in main
    cfg = CFGBuilder().build_from_src(name="", src=code_str)
  File "C:\Users\ssimon\AppData\Local\Programs\Python\Python39\lib\site-packages\scalpel\cfg\builder.py", line 128, in build_from_src
    return self.build(name, tree)
  ...
  File "C:\Users\ssimon\AppData\Local\Programs\Python\Python39\lib\ast.py", line 407, in visit
    return visitor(node)
  File "C:\Users\ssimon\AppData\Local\Programs\Python\Python39\lib\site-packages\scalpel\core\func_call_visitor.py", line 91, in visit_Call
    call_info["params"] += [self.param2str(arg)]
  File "C:\Users\ssimon\AppData\Local\Programs\Python\Python39\lib\site-packages\scalpel\core\func_call_visitor.py", line 47, in param2str
    return get_func(param)
  File "C:\Users\ssimon\AppData\Local\Programs\Python\Python39\lib\site-packages\scalpel\core\func_call_visitor.py", line 39, in get_func
    raise Exception(str(type(node.func)))
Exception: <class 'ast.Subscript'>
Jarvx commented 2 years ago

Thanks for the test case, I will push a fix tonight.

Jarvx commented 2 years ago

The bug is fixed now.