subprocess considered harmfull?
Hi all, I've been trying to use (Python 2.4 on WinXP) the subprocess module to execute a shell command (nmake in this case), and pass its output to a higher level. Using the following snippet: p = subprocess.Popen(nmake,stderr=subprocess.PIPE,stdout=subprocess.PIPE, \ universal_newlines=True, bufsize=1) os.sys.stdout.writelines(p.stdout) os.sys.stdout.writelines(p.stderr) Works fine on the command line, but fails when called from within Visual Studio, with the following error: File "C:\Python24\lib\subprocess.py", line 549, in __init__ (p2cread, p2cwrite, File "C:\Python24\lib\subprocess.py", line 609, in _get_handles p2cread = self._make_inheritable(p2cread) File "C:\Python24\lib\subprocess.py", line 650, in _make_inheritable DUPLICATE_SAME_ACCESS) TypeError: an integer is required If I replace the functionality with: p = os.popen4(nmake) # p[1] = stdout_and_stderr result pipe p[1].flush() os.sys.stdout.writelines(p[1].readlines()) All is well. I have a feeling this has been encountered before (by googling here), but didn't see any concise answer as to subprocess' robustness. So what is the matter here? And should I consider the subprocess module still unstable? Cheers, Uri -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess considered harmfull?
Roger Upole wrote: > "Fredrik Lundh" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > Steven Bethard wrote: > > > >> > Using the following snippet: > >> > p = > >> > subprocess.Popen(nmake,stderr=subprocess.PIPE,stdout=subprocess.PIPE, \ > >> >universal_newlines=True, bufsize=1) > >> > os.sys.stdout.writelines(p.stdout) > >> > os.sys.stdout.writelines(p.stderr) > >> > Works fine on the command line, but fails when called from within > >> > Visual Studio, with the following error: > >> > File "C:\Python24\lib\subprocess.py", line 549, in __init__ > >> > (p2cread, p2cwrite, > >> > File "C:\Python24\lib\subprocess.py", line 609, in _get_handles > >> > p2cread = self._make_inheritable(p2cread) > >> > File "C:\Python24\lib\subprocess.py", line 650, in _make_inheritable > >> > DUPLICATE_SAME_ACCESS) > >> > TypeError: an integer is required > >> > >> This looks like these known bugs: > >> http://python.org/sf/1124861 > >> http://python.org/sf/1126208 > >> > >> Try setting stderr to subprocess.PIPE. I think that was what worked for > >> me. (You might also try setting shell=True. That's what I currently > >> have in my code that didn't work before.) > > > > if someone wants to investigate, is seeing this problem, and have the win32 > > extensions on their machine, try changing this line in subprocess.py: > > > >if 0: # <-- change this to use pywin32 instead of the _subprocess driver > > > > to: > > > >if 1: # <-- change this to use _subprocess instead of the pywin32 driver > > > > and see if it either fixes the problem (not very likely) or gives you a > > better > > error message (very likely). > > > > > > > > The error msg is only slightly better: > > error: (6, 'DuplicateHandle', 'The handle is invalid.') > > Basically, gui apps like VS don't have a console, so > GetStdHandle returns 0. _subprocess.GetStdHandle > returns None if the handle is 0, which gives the original > error. Pywin32 just returns the 0, so the process gets > one step further but still hits the above error. > > Subprocess.py should probably check the > result of GetStdHandle for None (or 0) > and throw a readable error that says something like > "No standard handle available, you must specify one" > > Roger > > > > > == Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet > News== > http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ > Newsgroups > = East and West-Coast Server Farms - Total Privacy via Encryption = I gathered as much about why this happens in VS. A further question is why does n't os.popen fall in the same trap? Cheers, Uri -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess problem
Hi, I had a similar problem recently, and found that using pipes with os.popen* helped in my case. You can check the thread at: http://mail.python.org/pipermail/python-list/2005-September/300744.html. Cheers, Uri Do Re Mi chel La Si Do wrote: > Hi! > > This script (under Win-XP + P-2.4.1) : > import subprocess > p1=subprocess.Popen(r'cmd /cdir *.* /S /W /B', stdout=subprocess.PIPE) > chaineretour=p1.stdout.read() > > run OK if called from DOS-console. But, from another Python's script (by COM > + exec) give me an error. > > Here, the traceback : > > Traceback (most recent call last): > File "D:\dev\Python\ponx.py", line 4446, in PRun > exec(ccod,globals(),globals()) > File "C:\Python24\lib\subprocess.py", line 546, in __init__ >(p2cread, p2cwrite, > File "C:\Python24\lib\subprocess.py", line 606, in __get_handles__ >p2cread = self._make_inheritable(p2cread) > File "C:\Python24\lib\subprocess.py", line 647, in _make_inheritable >DUPLICATE_SAME_ACCESS) > error: (6, 'DuplicateHandle','Descripteur non valide') > > > I know the bug-signaled : http://python.org/sf/1124861 > > > But... a idea for a solution ? > > Thanks, and sorry for my bad english. > > > Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess.Popen on Windows
Hi, Been there - try looking at: http://groups.google.com/group/comp.lang.python/browse_frm/thread/f2cb83e948326ff5/d69feabbfc940b01?q=uri.nix&rnum=2#d69feabbfc940b01 Cheers, Uri -- http://mail.python.org/mailman/listinfo/python-list
ctypes and DLL exceptions
Hi all, Is there a method to deal with exceptions originating from a DLL creatively? After experimentation, I've observed that calling a ctypes wrapped DLL exception raises WindowsError, and an accompanying code. Does the code mean something? Can I throw an object from C++ and somehow catch it in Python? [Using Python 2.4 on Windows XP] Cheers, Uri -- http://mail.python.org/mailman/listinfo/python-list