ImageEngine/gaffer — pull request #1132 by ivanimanishi (2014-12-16 20:40:10+00:00)
Title: Altering python app so that it behaves more like a regular python call

In order to be able to easily make a python script capable of importing gaffer it is important variables like sys.argv have the same value regardless of whether they were called from "python" or from "gaffer python".
We also want things like return values from the script to be preserved, and that the output from one or the other be similar.

My proposal here is to get rid of the traceback print out, as that was interfering with sys.exit calls, and replacing the return value with "1". That is the behaviour we get if we use python directly.

Also, I'm suggesting we "hack" the sys.argv variable value so that it matches what we'd get from a call to python, as explained in the code:
```python
# trying to set the sys.argv to the same values as we would expect from a python call
#
# the expected gaffer python command line syntax is expected to be:
# gaffer python -file ]
#
# given the limitations of the gaffer python app, we can assume that the equivalent python command syntax would be:
# python ]
#
# python removes the "python" command from the start of the sys.argv list, making the first argument the script path
# sys.argv should therefore be:
# []
#
# sys.argv is assumed to be the base source of information for python argument parsing methods (argparse, getopt, ...)
```

This is changing the existing behaviour of the gaffer python app, but I'm not aware of any uses, so maybe it would be ok to not preserve full backward compatibility. But if that is an issue, we could create a different app with the changes I'm suggesting (possibly internal to IE).
Note that I'm still passing the argv variable (with an IECore.StringVectorData) as before, as that is not part of regular python.

There is another change that we may consider, which is to alter the "__name__" python variable.
When running python with a script, you get that set to "__main__". Using the gaffer python app we get "__builtin__". Maybe we should set it to "__main__"? But changing existing code to support both is not difficult, and there could be reasons to keep it as "__builtin__" that I'm not aware of.