Matt,

Le 04/03/2022 à 23:19, Matt Wilkie a écrit :
Hello folks who know about the python script construction :)

The pattern used in all of the python scripts under swig/python/gdal-utils is foreign to me. Can someone explain what's going on and why it's used? (or point to where this has been explored already).

if__name__ == '__main__':
    sys.exit(main(sys.argv))

Gdal utils .py all end with the block above. Everything else I've used to this point are more like:

if__name__ == '__main__':
    main(sys.argv)
I believe the idea was that the main() function could be used as a library function, in larger processing scripts, and thus using sys.exit() in the main itself would be quite unfriendly. If you just do main(sys.argv), the return value of the main wouldn't be propagated to the caller, right ? Or perhaps "everything else" you are refering to uses exceptions ?, and thus errors are naturally propagated to the caller.

The motivation for asking (besides just being curious about something new!) is that I'm trying to figure out how we can use pip to create standard launch wrappers in PYTHONHOME\Scripts instead of manual post-install setup of the batch files we use now. When I change setup.py to do this via console_scripts the resulting wrappers fail on this main construct. So I need to understand what the original intent is to try and come up with a remedy.

```
» gdal_edit fooo
Traceback (most recent call last):
  File "C:\Users\Matt\.conda\envs\gdal\Scripts\gdal_edit-script.py", line 33, in <module>     sys.exit(load_entry_point('gdal-utils', 'console_scripts', 'gdal_edit')())
TypeError: main() missing 1 required positional argument: 'argv'
```

The issue seems to be different. I believe this was mentioned in a recent ticket or pull request that the main() signature should probably be changed to

def main(argv = None):
    if argv is None:
        argv = sys.argv


Take all the above with a grain of salt. I'm not an expert in the best way to create idiomatic Python scripts. A large part of what we have too is largely due to just things being like they are, not necessarily with deep reasons.

Even


--
http://www.spatialys.com
My software is free, but my time generally not.

_______________________________________________
gdal-dev mailing list
gdal-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to