[Tutor] Selecting a browser

Ricardo Aráoz ricaraoz at gmail.com
Tue Dec 4 18:05:02 CET 2007


Tim Golden wrote:
> Ricardo Aráoz wrote:
>> These guys have given their free time so that we can enjoy their module, 
>  > hey could have no documentation at all...
>  > That said, my comments about documentation lacking still stand.
> 
> I think the important thing here is that "these guys" is you
> and me. Documentation is exactly one of those areas where less
> experienced people -- or people with less time on their hands --
> can add to the quality of the overall product.
> 
>> I'll check your links as soon as I have some time (right now my use of
>> python is just for the love of the trade) and if I can come up with
>> something to improve the docs I'll send it to these blokes.
> 
> If you don't think you'll be able to get something together within
> a day or two, get back to me and I'll write something up while it's
> still fresh in my mind. It's only a line or two...
> 
> TJG

Ok. Tim, Marty, I think I got it.
My code :

>>> import webbrowser
>>> ff = webbrowser.get(r"S:\FirefoxPortable\FirefoxPortable.exe %s &")
>>> ff.open('http://www.google.com')
False
>>> ff = webbrowser.get("S:/FirefoxPortable/FirefoxPortable.exe %s &")
>>> ff.open('http://www.google.com')
True

So I checked webbrowser module, and there I found that the fault resides
in shlex module or in the way webbrowser uses shlex module.
In function get() defined in line 27 of webbrowser.py, in line 37 you have :
"""
        if '%s' in browser:
            # User gave us a command line, split it into name and args
            browser = shlex.split(browser)
            if browser[-1] == '&':
                return BackgroundBrowser(browser[:-1])
            else:
                return GenericBrowser(browser)
        else:
"""
Notice the & at the end of the command line is handled ok.
And the problem lies in the use of shlex.split(browser) instruction.
This is the problem :

>>> browser = r"S:\FirefoxPortable\FirefoxPortable.exe %s &"
>>> browser
'S:\\FirefoxPortable\\FirefoxPortable.exe %s &'
>>> import shlex
>>> shlex.split(browser)	&& this one does not work
['S:FirefoxPortableFirefoxPortable.exe', '%s', '&']
>>> browser = "S:/FirefoxPortable/FirefoxPortable.exe %s &"
>>> shlex.split(browser)	&& this one works ok.
['S:/FirefoxPortable/FirefoxPortable.exe', '%s', '&']

The point is shlex uses the '\\' as an escape character, that's why it
gets ignored. One fix would be to disable or change the escape character
so instead of shlex.split(browser) it would be :

        if '%s' in browser:
            # User gave us a command line, split it into name and args
#            browser = shlex.split(browser)
            oShlex = shlex.shlex(browser, posix=True)
            oShlex.whitespace_split = True
            oShlex.escape = ''	# or whatever character we wish to be escape
            browser = list(oShlex)
            if browser[-1] == '&':
                return BackgroundBrowser(browser[:-1])
            else:
                return GenericBrowser(browser)
        else:

Did this in my webbrowser.py and then :

>>> import webbrowser
>>> ff = webbrowser.get("S:\FirefoxPortable\FirefoxPortable.exe %s &")
>>> ff.open('http://www.google.com', 2)
True

And the browser opened as expected (note that I didn't have to use raw
strings).

The other way to handle it would be to include in the documentation that
windows paths should have '/' or '\\\\' instead of '\\'.
The choice would depend on whether the authors consider there is a use
for the escape character, and what value that escape character might have.
I've registered in the issue tracker, but got scared and didn't post
anything. Can I be certain the blokes maintaining this module will see
it, or should and try contact them directly? You see, I'm new in this
contribution thingy. Or even better, could you Tim or Marty post the
issue and then let me know where it is so I can take a look and get the
flavor of what is expected in the tracker?

Cheers

Ricardo






More information about the Tutor mailing list