Skip to content

Using SmartBugs as a Python module

gsalzer edited this page Dec 11, 2022 · 3 revisions

SmartBugs can be used as a Python module and called from other Python scripts. For example, here's a Python script that uses SmartBugs to run the tool conkas on the contracts samples/simple_dao.*:

import sb.smartbugs, sb.settings, sb.exceptions
if __name__ == "__main__":
    settings = sb.settings.Settings()
    settings.update({
        "tools": ["conkas"],
        "files": ["samples/simple_dao.*"],
        #"quiet": True # suppress output on stdout
    })
    try:
        sb.smartbugs.main(settings)
    except sb.exceptions.SmartBugsError as e:
        print(f"Something didn't work: {e}")

The line if __name__ == "__main__": is essential. SmartBugs sets up a multi-processing context, which means that any global code will be executed multiple times, once for each process. The line ensures that the code beneath it is executed only once, when the script is called as the main script. In the same vein, complex initialisations of global variables or modules should be avoided, as this code will be executed multiple times.

Clone this wiki locally