[Tutor] mamelauncher (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Sep 13 23:31:22 CEST 2005



On Tue, 13 Sep 2005, Max Russell wrote:

> There actually seems to be a bit of a problem in the interaction with
> the system when it launches mame.
>
> If I run this:
>
> import os
>
> def run_mame_selection(selection):
>     os.system("C:\\mame096b\\mame.exe"+ selection)
>
> #main
> selection = " tnzs"
> run_mame_selection(selection)
> raw_input()
>
> mame reports files missing, but I can launch the game manually- I
> suspect that this is some sort of error that occurs normally but is
> bypassed on launch and that my one-shot attempt to launch the thing
> cannot handle.

Hi Max,

Hmmmm... that'll be something you'll want to check then.  At least we can
see that there might be some problems there.  Do you know if the current
working directory matters to MAME, or if it's looking for any particular
environmental variables?

Perhaps you might want to post those errors on a MAME support group, to
see if other people know how to fix things properly.


By the way, I notice that you're accounting above for the possible bug of
having the selection text smushed right into the front of the selection
text by manually prepending a space:

######
> def run_mame_selection(selection):
>     os.system("C:\\mame096b\\mame.exe"+ selection)
> selection = " tnzs"
######

That was the bug that I thought was really causing problems, since's it
wasn't clear at all that the code was handling the possible issue of the
wordsrunningtogether.


But you may want to modify run_mame_selection so that it does the
prepending for you:

######
def run_mame_selection(selection):
    os.system("C:\\mame096b\\mame.exe %s" % selection)
######

That way, there's no possiblity of making that kind of mistake.
Unforunately, this isn't robust because if 'selection' itself contains
weird characters like quotes or spaces, that'll probably cause problems.


Instead, we can pass a real list of command line arguments and avoid
worrying about shell escape and quotation issues.  For example, instead
of:

######
>>> f = os.popen("ls -l /etc/passwd")
>>> f.read()
'-rw-r--r--  1 root root 1957 Jul 12 17:25 /etc/passwd\n'
######

it is preferable to do:

######
>>> child_stdin, child_stdout = os.popen2(['ls', '-l', '/etc/passwd'])
>>> child_stdout.read()
'-rw-r--r--  1 root root 1957 Jul 12 17:25 /etc/passwd\n'
######

If the filename here had spaces in it, then the first example with
os.popen() will probably break.  But the second with os.popen2 and the
list of explicit arguments should fare better.



> I think I need to have a wee look at subprocess!

Unfortunately, you'll probably run into similar problems with
'subprocess'.  I think the problem here is your MAME setup: it might be
expecting something in your environment that isn't there, like the correct
current working directory, or something else.  I don't know too much about
MAME, so I can't say in definite terms what's going on with the MAME error
messages.


Best of wishes to you!



More information about the Tutor mailing list