Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess "handle is invalid" error

Reply
Thread Tools

subprocess "handle is invalid" error

 
 
Grant Edwards
Guest
Posts: n/a
 
      04-18-2007
I'm trying to use the py-gnuplot module on windows, and have
been unable to get it to work reliably under Win2K and WinXP.

By default, it uses popen(gnuplotcmd,'w'), but in some
situations that consistently gets an "invalid operand" IOError
when write() is called on the pipe.

So I switched to subprocess. It works fine when executed
"normally" (e.g. "python progname.py"), but when bundled by
py2exe, it always does this:

Traceback (most recent call last):
File "surfedit.py", line 28, in ?
File "Gnuplot\_Gnuplot.pyc", line 178, in __init__
File "Gnuplot\gp_win32.pyc", line 117, in __init__
File "subprocess.pyc", line 533, in __init__
File "subprocess.pyc", line 607, in _get_handles
File "subprocess.pyc", line 634, in _make_inheritable
WindowsError: [Errno 6] The handle is invalid

How does one troubleshoot errors that happen three layers deep
in the subprocess module?

--
Grant Edwards grante Yow! I'm a fuschia bowling
at ball somewhere in Brittany
visi.com
 
Reply With Quote
 
 
 
 
Erik Johnson
Guest
Posts: n/a
 
      04-18-2007

"Grant Edwards" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

<snip stuff about gnu-plot and py2exe problems>
> How does one troubleshoot errors that happen three layers deep
> in the subprocess module?


I think the problem is more likely in how your py2exe is getting
bundled, rather than any real problem with the subprocess module. I have
been on the py2exe email list but not really paying much attention... If I
recall correctly, there are some special considerations when using gnuplot
with py2exe? I would suggest digging through the py2exe email archives to
see if you can locate emails on that subject (one copy of which I believe
can be found at:
http://sourceforge.net/mailarchive/f...=py2exe-users), and
if that doesn't work, to direct your question to the py2exe users list at:
http://www.velocityreviews.com/forums/(E-Mail Removed)

(I believe the current home page of the py2exe project is at:
http://sourceforge.net/projects/py2exe/)

Hope that helps,
-ej


 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      04-18-2007
On 2007-04-18, Erik Johnson <(E-Mail Removed)> wrote:
>
> "Grant Edwards" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
><snip stuff about gnu-plot and py2exe problems>
>> How does one troubleshoot errors that happen three layers deep
>> in the subprocess module?

>
> I think the problem is more likely in how your py2exe is
> getting bundled, rather than any real problem with the
> subprocess module.


I've switched back to os.popen() since I seem to get the same
problems with either. The odd thing is that it only seems to
fail when the current directory is mounted via the network
_and_ when it's being started by clicking on a file whose
filetype is associated with the application.

Running the program from the command line (cmd.exe or bash)
always works for any current directory. Running the program by
cliking on a shortcut icon on the desktop always works for any
'start in' directory.

The one case that doesn't work is when its run by clicking on
an associated file on a network drive.

--
Grant Edwards grante Yow! Now I'm concentrating
at on a specific tank battle
visi.com toward the end of World
War II!
 
Reply With Quote
 
Thomas Heller
Guest
Posts: n/a
 
      04-18-2007
Grant Edwards schrieb:
> I'm trying to use the py-gnuplot module on windows, and have
> been unable to get it to work reliably under Win2K and WinXP.
>
> By default, it uses popen(gnuplotcmd,'w'), but in some
> situations that consistently gets an "invalid operand" IOError
> when write() is called on the pipe.
>
> So I switched to subprocess. It works fine when executed
> "normally" (e.g. "python progname.py"), but when bundled by
> py2exe, it always does this:
>
> Traceback (most recent call last):
> File "surfedit.py", line 28, in ?
> File "Gnuplot\_Gnuplot.pyc", line 178, in __init__
> File "Gnuplot\gp_win32.pyc", line 117, in __init__
> File "subprocess.pyc", line 533, in __init__
> File "subprocess.pyc", line 607, in _get_handles
> File "subprocess.pyc", line 634, in _make_inheritable
> WindowsError: [Errno 6] The handle is invalid
>
> How does one troubleshoot errors that happen three layers deep
> in the subprocess module?
>


I think this is a subprocess bug. It is often attributed to py2exe because
usually developers do never run the script in pythonW.exe instead of python.exe,
and later build a *windows* program with py2exe (the *windows* program has no
console, and that triggers the bug).

Consider ths little script:

"""
import os, sys, subprocess

sys.stderr = open("errors.txt", "w")

if os.path.exists("output.txt"):
os.remove("output.txt")

proc = subprocess.Popen("dir", shell=True,
stdout=subprocess.PIPE,
## stderr=subprocess.PIPE,
## stdin=subprocess.PIPE,
)
##proc.stderr.close()
##proc.stdin.close()
data = proc.stdout.read()

open("output.txt", "w").write(data)
"""

It calls 'dir' in the current directory, and writes the output
to the file 'output.txt'. Any errors are written to 'errors.txt'.

When you run this script with python.exe, everything works.
If the script is run with pythonW.exe, nothing works and 'errors.txt'
contains this:

c:\svn\theller>type errors.txt
Traceback (most recent call last):
File "test_subproc.py", line 9, in <module>
stdout=subprocess.PIPE,
File "c:\python25\lib\subprocess.py", line 586, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "c:\python25\lib\subprocess.py", line 699, in _get_handles
p2cread = self._make_inheritable(p2cread)
File "c:\python25\lib\subprocess.py", line 744, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] Das Handle ist ungültig
c:\svn\theller>

The error message, translated to english, is 'the handle is invalid'.

The script can be made to work correctly even with pythonW.exe (and also
as py2exe'd windows program, I just checked it out) when the 4 commented out
lines are uncommented. subprocess cannot inherit the standard handles when
the process has no console, you have to create pipes for all 3 channels, and
close those that are not needed.

I thought that this bug was fixed in Python2.5.1 (the release candidate),
but it seems it wasn't. The bug is at
http://sourceforge.net/tracker/index...70&atid=105470

If all this is correct, I hope that someone adds a section to the py2exe wiki;
and reopens the above bug report.

Thomas

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-18-2007
On 2007-04-18, Thomas Heller <(E-Mail Removed)> wrote:

> I think this is a subprocess bug. It is often attributed to
> py2exe because usually developers do never run the script in
> pythonW.exe instead of python.exe, and later build a *windows*
> program with py2exe (the *windows* program has no console, and
> that triggers the bug).


[...]

Ah, excellent analysis. It does indeed to appear to be a
subprocess bug. Switching back to os.popen() makes things
"work" again. There's a different problem when the program is
started by double-clicking an associated file in a networked
directory (works fine in a local directory). That was the
initial problem I was trying to troubleshoot by switching
trying subprocess.Popen().

> The script can be made to work correctly even with pythonW.exe
> (and also as py2exe'd windows program, I just checked it out)
> when the 4 commented out lines are uncommented. subprocess
> cannot inherit the standard handles when the process has no
> console, you have to create pipes for all 3 channels, and
> close those that are not needed.


Perhaps I'll try switching back to subprocess.Popen() and
creating all three pipes.

> I thought that this bug was fixed in Python2.5.1 (the release candidate),
> but it seems it wasn't. The bug is at
> http://sourceforge.net/tracker/index...70&atid=105470
>
>> If all this is correct, I hope that someone adds a section to the py2exe wiki;


It had been documented at the py2exe wiki -- though it mentions a
different exception. I added the bug-tracker link and also
that it throws the "invalid handle" exception.

http://www.py2exe.org/index.cgi/Py2E...ssInteractions

> and reopens the above bug report.


Since I'm still using 2.4.3, I don't think it would be
appropriate for me to do so.

--
Grant Edwards grante Yow! It don't mean a
at THING if you ain't got
visi.com that SWING!!
 
Reply With Quote
 
Daniel Serodio
Guest
Posts: n/a
 
      01-10-2008


Thomas Heller-2 wrote:
>
> Grant Edwards schrieb:
>
> [snip]
>
>>
>> Traceback (most recent call last):
>> File "surfedit.py", line 28, in ?
>> File "Gnuplot\_Gnuplot.pyc", line 178, in __init__
>> File "Gnuplot\gp_win32.pyc", line 117, in __init__
>> File "subprocess.pyc", line 533, in __init__
>> File "subprocess.pyc", line 607, in _get_handles
>> File "subprocess.pyc", line 634, in _make_inheritable
>> WindowsError: [Errno 6] The handle is invalid
>>
>> How does one troubleshoot errors that happen three layers deep
>> in the subprocess module?
>>

>
> I think this is a subprocess bug. It is often attributed to py2exe
> because
> usually developers do never run the script in pythonW.exe instead of
> python.exe,
> and later build a *windows* program with py2exe (the *windows* program has
> no
> console, and that triggers the bug).
>
> [snip]
>
> I thought that this bug was fixed in Python2.5.1 (the release candidate),
> but it seems it wasn't. The bug is at
> http://sourceforge.net/tracker/index...70&atid=105470
>
> If all this is correct, I hope that someone adds a section to the py2exe
> wiki;
> and reopens the above bug report.
>
> Thomas
>

I've been bitten by this same bug in 2.5.1, and the bug you linked to (which
is now @ http://bugs.python.org/issue1124861) is still marked as fixed.

I've tried to reopen it but it seems only the reporter (or some "admin"
user) is able to do it.

--
View this message in context: http://www.nabble.com/subprocess-%22...p14740517.html
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
execve error with the subprocess module yogamatt1970@gmail.com Python 1 10-22-2008 04:26 AM
Trouble with subprocess.call(...) and zsh script (OSError: [Errno 8] Exec format error) Michael George Lerner Python 2 11-13-2007 11:41 PM
subprocess -- broken pipe error 7stud Python 11 07-02-2007 10:12 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM



Advertisments