GNOME Bugzilla – Bug 625211
import gst acts on --help or -h in sys.argv, preempting __main__
Last modified: 2012-12-17 11:39:48 UTC
A one line program import gst reacts to a command line with parameter --help or -h, and gives garbled gst help and quits. True also if the import is buried inside other imports. In a longer script with a command line parser, this preempts the main script. Get rid of this behavior when gst is not __main__. Using 64 bit Ubuntu 10.4
Created attachment 178218 [details] [review] gst-python: do not hijack --help, -h, --version We want gstreamer to not hijack some options, as it will exit if it finds htem, namely --help, -h, and --version. We take them out of sys.argv, and will be restoring them afterwards so the calling program sees them as supplied.
This was surprisingly annoying to get right, this code is called (at least) twice recursively, the second time having a non existent argv in sys, hence the monkeying around with exceptions. Test program: import sys import pygst pygst.require("0.10") import gst print "Hello, world" for o in sys.argv: print "We see: " + o
Review of attachment 178218 [details] [review]: You might want to del local_argv when done with it ::: gst/__init__.py.in @@ -191,2 +191,3 @@ pass +# We want gstreamer to not hijack some options, as it will exit if it finds htem, typo htem :) @@ -193,0 +193,8 @@ +# We want gstreamer to not hijack some options, as it will exit if it finds htem, +# namely --help, -h, and --version. We take them out of sys.argv, and will be +# restoring them afterwards so the calling program sees them as supplied. ... 5 more ... if option not in ('--help', '-h', '--version') is easier to read and has the same effect. @@ -193,0 +193,10 @@ +# We want gstreamer to not hijack some options, as it will exit if it finds htem, +# namely --help, -h, and --version. We take them out of sys.argv, and will be +# restoring them afterwards so the calling program sees them as supplied. ... 7 more ... except: pass is not a very good practice. What exception could be raised exactly? That code looks fairly safe to me. @@ -196,0 +208,6 @@ +# Restore the original command line if appropriate +try: + if local_argv: ... 3 more ... same question ;)
The try block catches this: Traceback (most recent call last):
+ Trace 225521
local_argv = sys.argv
This exception happens on the inner run of this block (which, to be honest, I have no idea why it is being recursively called). I'm attaching a patch with the other suggestions, thanks for them, as I only dabble in Python so I have a very C like style. My understanding though was that setting local_argv to None would garbage collect it. Isn't that correct ?
Created attachment 178306 [details] [review] gst-python: do not hijack --help, -h, --version We want gstreamer to not hijack some options, as it will exit if it finds them, namely --help, -h, and --version. We take them out of sys.argv, and will be restoring them afterwards so the calling program sees them as supplied.
Created attachment 178319 [details] [review] gst-python: do not hijack --help, -h, --version We want gstreamer to not hijack some options, as it will exit if it finds them, namely --help, -h, and --version. We take them out of sys.argv, and will be restoring them afterwards so the calling program sees them as supplied. Remove the last assignment to None, which is now unneeded due to the del statement above, pointed out by philn-tp
Closing this bug now, gst-python is only an extension module to pygi now and this bug doesn't make much sense anymore in this context.