Funny incompatibility of Cubit 2021.5 with previous versions

If your old professionally written Python scripts like:

def func():
    ...

if __name__ == '__console__':
    func()

do not work in the 2021.5, the reason is: special Python variable __name__ historically (from cubit 11 to 2021.4) equal to ‘__console__’, now has standard value ‘__main__’. So if you want to keep your scripts compatible with the previous versions of Cubit/Trelis you have to use:

if __name__ in ['__console__', '__main__']:
    func()

I am not sure, do we need to ask developers to return old value back or not…

@AndyU I’m not seeing anywhere in the code where we explicitly set __name__ and I don’t see any documentation that guarantees the value __console__. I think this is unintentional, undocumented behavior and should not be relied on. If you’re using the value __console__ as a way to detect if the script is being run from Cubit or not, you can instead write a function that attempts to import the Cubit module, like this:

def isCubit():
    try:
        import cubit
        return True
    except ImportError:
        return False

Hi!

C:\Program Files\Trelis 17.1\bin\python2\Lib\code.py
C:\Program Files\Trelis 17.1\bin\python3\Lib\code.py

If change value of the locals[‘doc’] in the line 34 of the code.py from None to something else, restart 17.1 and print(locals()) this modified value will be seen in the output.

The same files exist in 2021.5 directory structure, but if modify code.py in 2021.5 - modification will not be printed by print(locals()). Probably code.py file is not used now…

No, I use this guard (instead of standard ‘main’) to allow usage of my scripts as main executables and/or as imported modules:

# file a:
__all__ = ['make_geometry']
import cubit

def make_geometry():
    ...

if __name__ == '__console__'
  make_geometry()

# file main:
import a
import b   # similar to a.py
...

if __name__ == '__console__'
    a.make_geometry()
    b.make_geometry()
    ...

P.S. It is interesting, that code.py exists in Python distribution and mentioned in manuals. Why ‘console’ is used here?

Correct, code.py comes with the Python distribution. The python2 and python3 folders on Windows are direct copies of the Python redistributable folders, so any behavior of code.py is used unmodified by Cubit. I’m not sure why __name__ changed, but we can’t guarantee the behavior of Python internals since we don’t directly control them. All Cubit does (apart from some initialization code that sets things like PYTHONHOME) is link against the Python shared library python38.dll.

Hi,

If remove code.py from Trelis 17.1 distribution (say from bin/python3/lib and start Trelis with

>trelis.exe -pythonversion 3

it will crashes at start, because code is imported in structure/bhelper.py module, which is probably called from clarogui.dll and clarofw.dll.

Simultaneously, it is safe to delete code.py from Coreform Cubit 2021.5 distribution just because file structure/bhelper.py is absent, that is code module is not used now for interactive console emulation and __name__ is not redefined.

P.S. Workaround: old value of the __name__ variable in 2021.5 can be restored by modified wrapper script from Cubit User Documentation -> Controlling the Application -> gui -> Command Line Workspace:

with open('myscript.py', encoding='utf8') as f:
    code = compile(f.read(), 'myscript.py', 'exec')
glob = globals().copy()
glob['__name__'] = '__console__'
exec(code, glob)

Additionally: I just installed the latest version 2022.11 (release) and here value of the predefined variable __name__ is equal to ‘CubitPythonInterpreter_2’ :slight_smile: