first correct explanation wins a beer...
...or the caffeinated beverage of your choice, collectable at IPC9. I'm running on a straightforward Linux box: $ uname -a Linux akbar.nevex.com 2.2.16 #3 Mon Aug 14 14:43:46 EDT 2000 i686 unknown with Python 2.1, built fresh from today's repo: $ python Python 2.1a2 (#2, Feb 26 2001, 15:27:11) [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2 I have one tiny script called "tryout.py": $ cat tryout.py print "We made it!" and a small HTML file called "printing.html": $ cat printing.html <html><body> <pre prog="tryout.py"> We made it! </pre> </body></html> The idea is that my little SAX handler will look for "pre" elements with "prog" attributes, re-run the appropriate script, and compare the output with what's in the HTML page (it's an example for the class). The problem is that "popen2" doesn't work as expected when called from within a SAX content handler, even though it works just fine when called from a method of another class, or on its own. The whole script is: $ cat repy #!/usr/bin/env python import sys from os import popen2 from xml.sax import parse, ContentHandler class JustAClass: def method(self, progName): shellCmd = "python " + progName print "using just a class, shell command is '" + shellCmd + "'" inp, outp = popen2(shellCmd) inp.close() print "using just a class, result is", outp.readlines() class UsingSax(ContentHandler): def startElement(self, name, attrs): if name == "pre": shellCmd = "python " + attrs["prog"] print "using SAX, shell command is '" + shellCmd + "'" inp, outp = popen2(shellCmd) inp.close() print "using SAX, result is", outp.readlines() if __name__ == "__main__": # Run it directly inp, outp = popen2("python tryout.py") inp.close() print "Running popen2 directly, result is", outp.readlines() # Use a plain old class JustAClass().method("tryout.py") # Using SAX input = open("printing.html", 'r') parse(input, UsingSax()) input.close() The output is: $ python repy Running popen2 directly, result is ['We made it!\n'] using just a class, shell command is 'python tryout.py' using just a class, result is ['We made it!\n'] using SAX, shell command is 'python tryout.py' using SAX, result is [] My system has a stock 1.5.2 in /usr/bin/python, but my path is: $ echo $PATH /home/gvwilson/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/sbin:/ho me/gnats/bin so that I get the 2.1 version: $ which python /home/gvwilson/bin/python My PYTHONPATH is set up properly as well (I think): $ echo $PYTHONPATH /home/gvwilson/lib/python2.1:/home/gvwilson/lib/python2.1/lib-dynload I'm using PyXML-0.6.4, built fresh from the .tar.gz source today. So, like I said --- a beer or coffee to the first person who can explain what's up. I'm attaching the Python scripts, the HTML file, and a verbose strace output from my machine. Thanks, Greg <<printing.html>> <<repy>> <<strace.txt>> <<tryout.py>>
My guess: Unicode. Try casting to an 8-bit string and see what happens. -- Vote for Your Favorite Python & Perl Programming Accomplishments in the first Active Awards! http://www.ActiveState.com/Awards
participants (2)
-
Greg Wilson
-
Paul Prescod