After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 625211 - import gst acts on --help or -h in sys.argv, preempting __main__
import gst acts on --help or -h in sys.argv, preempting __main__
Status: RESOLVED OBSOLETE
Product: GStreamer
Classification: Platform
Component: gst-python
unspecified
Other Linux
: Normal normal
: git master
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on:
Blocks:
 
 
Reported: 2010-07-25 04:16 UTC by Andy Harrington
Modified: 2012-12-17 11:39 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gst-python: do not hijack --help, -h, --version (1.52 KB, patch)
2011-01-13 12:42 UTC, Vincent Penquerc'h
none Details | Review
gst-python: do not hijack --help, -h, --version (1.52 KB, patch)
2011-01-14 10:37 UTC, Vincent Penquerc'h
none Details | Review
gst-python: do not hijack --help, -h, --version (1.49 KB, patch)
2011-01-14 14:00 UTC, Vincent Penquerc'h
rejected Details | Review

Description Andy Harrington 2010-07-25 04:16:49 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
Comment 1 Vincent Penquerc'h 2011-01-13 12:42:58 UTC
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.
Comment 2 Vincent Penquerc'h 2011-01-13 12:46:29 UTC
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
Comment 3 Philippe Normand 2011-01-13 23:07:17 UTC
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 ;)
Comment 4 Vincent Penquerc'h 2011-01-14 10:36:43 UTC
The try block catches this:

Traceback (most recent call last):
  • File "/home/v/lib/python2.6/site-packages/gst-0.10/gst/__init__.py", line 198 in <module>
    local_argv = sys.argv
AttributeError: 'module' object has no attribute '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 ?
Comment 5 Vincent Penquerc'h 2011-01-14 10:37:01 UTC
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.
Comment 6 Vincent Penquerc'h 2011-01-14 14:00:38 UTC
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
Comment 7 Sebastian Dröge (slomo) 2012-12-17 11:39:47 UTC
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.