Skip to content

etsdemo 'Run' button does not execute script in __main__ block #1428

@kitchoi

Description

@kitchoi

If one has a script that contains a block like this:

if __name__ == "__main__":
    ...

Trying to run this script from the etsdemo application by pressing the 'Run' button will not run that block, which is unexpected.

To reproduce, create a file like this:

if __name__ == "__main__":
    print("Hello")

Then run this:

from etsdemo.main import main
main(
    [
        {
            "version": 1,
            "name": "Some name",
            "root": ".",  # or a path to a directory that contains the above test file.
        },
    ]
)

That block is not run when one presses the 'Run' button.
Dec-01-2020 18-03-35

But if one changes the "__main__" to "___main___" (three underscores), then it would run:
Dec-01-2020 18-01-11

From the snapshot of an old version of the application, this ___main___ is not a typo: It looks deliberate.
https://github.com/enthought/traitsui/blob/96f45ff9c51ff55b5724cbed2361b933f468c183/enthought/traits/ui/extras/demo.py

Looks like the run_code method is serving two purposes:
(1) When the file is clicked, it loads the file to find this specially named variable 'demo'.
(2) When the Run button is clicked, it runs the file too.

For the first purpose, we don't want the block in if __name__ == "__main__" to run. For the second purpose, we do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions