python 2.5 scripting in vim on windows: subprocess problem

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

python 2.5 scripting in vim on windows: subprocess problem

Dmitry Teslenko

Hello!
I'm using subprocess.Popen in python script in vim.
It called this way:
def some_func():
                p = subprocess.Popen( command , stdout =
subprocess.PIPE, stderr =
subprocess.STDOUT)
                while True:
                        s = p.stdout.readline()
                        if not s:
                                break
                        self.__output( '... %s' % s )
                return p.wait()

It filters command's output and re-ouputs it in stdout.
Being called from console, it works fine.
Being called from vim with :python some_func() it says:
file "...subprocess.py", line 586 in __init__
...
file "...subprocess.py", line 699, in _get_handles
...
file "...subprocess.py", line 744 in _make_inheritable
DUPLICATE_SAME_ACCESS
WindowsError: [Error 6]

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Andy Kittner

Dmitry Teslenko wrote:
> Hello!
> I'm using subprocess.Popen in python script in vim.

Hi, I just tried the following minimal sample similar to yours:
python << _EOF
def call_something(what):
    import sys, subprocess
    p = subprocess.Popen(what, stdout = subprocess.PIPE,
                               stderr = subprocess.STDOUT)
    while True:
        s = p.stdout.readline()
        if not s:
            break
        sys.stdout.write(s)
    return p.wait()
_EOF

And then in the vim commandline::
:py call_something(["cmd", "/c", "echo hello world"])

and it worked as I would expect. This is on Win XP with python-2.5.1.

> It called this way:
> def some_func():
>                 p = subprocess.Popen( command , stdout =
> subprocess.PIPE, stderr =
> subprocess.STDOUT)
>                 while True:
>                         s = p.stdout.readline()
>                         if not s:
>                                 break
>                         self.__output( '... %s' % s )
What is self.__output doing, maybe this is causing your problems.

>                 return p.wait()
>
> It filters command's output and re-ouputs it in stdout.
> Being called from console, it works fine.
> Being called from vim with :python some_func() it says:
> file "...subprocess.py", line 586 in __init__
> ...
> file "...subprocess.py", line 699, in _get_handles
> ...
> file "...subprocess.py", line 744 in _make_inheritable
> DUPLICATE_SAME_ACCESS
> WindowsError: [Error 6]
>

It would be good to see the full exception, as well as a corresponding
minimal, but complete example that triggers the bug.


Regards Andy

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Hari Krishna Dara-3
In reply to this post by Dmitry Teslenko

On 10/22/07, Dmitry Teslenko <[hidden email]> wrote:

>
> Hello!
> I'm using subprocess.Popen in python script in vim.
> It called this way:
> def some_func():
>                p = subprocess.Popen( command , stdout =
> subprocess.PIPE, stderr =
> subprocess.STDOUT)
>                while True:
>                        s = p.stdout.readline()
>                        if not s:
>                                break
>                        self.__output( '... %s' % s )
>                return p.wait()
>
> It filters command's output and re-ouputs it in stdout.
> Being called from console, it works fine.
> Being called from vim with :python some_func() it says:
> file "...subprocess.py", line 586 in __init__
> ...
> file "...subprocess.py", line 699, in _get_handles
> ...
> file "...subprocess.py", line 744 in _make_inheritable
> DUPLICATE_SAME_ACCESS
> WindowsError: [Error 6]

Are you running this on vim or gvim? If you are running on gvim, my
guess is that the handles that you are passing are not valid. In
either case, try creating explicit handles that are valid (such as for
/dev/null) and create the process with these handles.

--
HTH,
Hari

>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Dmitry Teslenko
In reply to this post by Andy Kittner

On 22/10/2007, Andy Kittner <[hidden email]> wrote:

> Hi, I just tried the following minimal sample similar to yours:
> python << _EOF
> def call_something(what):
>     import sys, subprocess
>     p = subprocess.Popen(what, stdout = subprocess.PIPE,
>                                stderr = subprocess.STDOUT)
>     while True:
>         s = p.stdout.readline()
>         if not s:
>             break
>         sys.stdout.write(s)
>     return p.wait()
> _EOF
>
> And then in the vim commandline::
> :py call_something(["cmd", "/c", "echo hello world"])
>
> and it worked as I would expect. This is on Win XP with python-2.5.1.

> What is self.__output doing, maybe this is causing your problems.

self.__output() writes some strings in file and prints out others.
With  standard "print" construct.
I see you print strings with sys.stdout.write(). Maybe that makes
difference. Why do you?

> VIM can, also, to be OLE-COM-server.
>
> Try with:
>   from win32com.client.dynamic import Dispatch
>   vim = Dispatch('Vim.Application')

Thanks, I'll try to avoid COM whenever possible.

> Are you running this on vim or gvim? If you are running on gvim, my
> guess is that the handles that you are passing are not valid. In
> either case, try creating explicit handles that are valid (such as for
> /dev/null) and create the process with these handles.

I'm passing hadles that I get from subprocess.Popen. Just passing
command to Popen constructor and using his handles to read data. No
other handle-manipulations.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Andy Kittner
On Mon, Oct 22, 2007 at 10:48:56PM +0400, Dmitry Teslenko wrote:
>
> On 22/10/2007, Andy Kittner <[hidden email]> wrote:
>
>> What is self.__output doing, maybe this is causing your problems.
>
> self.__output() writes some strings in file and prints out others.
> With  standard "print" construct.
> I see you print strings with sys.stdout.write(). Maybe that makes
> difference. Why do you?
For no particular reason, and it shouldn't really matter as the print
keyword does roughly the same.

[snip]
>
>> Are you running this on vim or gvim? If you are running on gvim, my
>> guess is that the handles that you are passing are not valid. In
>> either case, try creating explicit handles that are valid (such as for
>> /dev/null) and create the process with these handles.
Just as a side note: my vim was a gvim-7.1.140 with dynamic python
support, so it doesn't look like a general problem.

> I'm passing hadles that I get from subprocess.Popen. Just passing
> command to Popen constructor and using his handles to read data. No
> other handle-manipulations.
When exactly does it throw the exception? Directly on creation of the
Popen object, or when you try to read stdout?

Regards Andy

--
Rube Walker: "Hey, Yogi, what time is it?"
Yogi Berra:  "You mean now?"

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Dmitry Teslenko

On 22/10/2007, Andy Kittner <[hidden email]> wrote:
> >> Are you running this on vim or gvim? If you are running on gvim, my
> >> guess is that the handles that you are passing are not valid. In
> >> either case, try creating explicit handles that are valid (such as for
> >> /dev/null) and create the process with these handles.
> Just as a side note: my vim was a gvim-7.1.140 with dynamic python
> support, so it doesn't look like a general problem.
I've also tried *-1-140 from cream's sourceforge website and it works
just like my custom-built one.

> > I'm passing hadles that I get from subprocess.Popen. Just passing
> > command to Popen constructor and using his handles to read data. No
> > other handle-manipulations.
> When exactly does it throw the exception? Directly on creation of the
> Popen object, or when you try to read stdout?
It throws exception on subprocess.Popen object instantiation.
os.system() works fine but I want Popen functionality.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Andy Kittner

Dmitry Teslenko wrote:
> On 22/10/2007, Andy Kittner <[hidden email]> wrote:
>> When exactly does it throw the exception? Directly on creation of the
>> Popen object, or when you try to read stdout?
> It throws exception on subprocess.Popen object instantiation.
> os.system() works fine but I want Popen functionality.

Okay, I had another look at your error message, and at the
subproccess.py file. It seems like it gets an 'ERROR_INVALID_HANDLE'
when it tries to duplicate the stdin handle. Do you supply something as
stdin? If yes does the error also occur if you leave it as normal, and
if no can you try something like:
p = subprocess.Popen(command,
                      stdin = subprocess.PIPE, stdout = subprocess.PIPE,
                      stderr = subprocess.PIPE)
p.stdin.close()
[... your  other code...]

Regards, Andy

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Dmitry Teslenko

On 24/10/2007, Andy Kittner <[hidden email]> wrote:

> Okay, I had another look at your error message, and at the
> subproccess.py file. It seems like it gets an 'ERROR_INVALID_HANDLE'
> when it tries to duplicate the stdin handle. Do you supply something as
> stdin? If yes does the error also occur if you leave it as normal, and
> if no can you try something like:
> p = subprocess.Popen(command,
>                       stdin = subprocess.PIPE, stdout = subprocess.PIPE,
>                       stderr = subprocess.PIPE)
> p.stdin.close()
> [... your  other code...]
I've provided stdin=subprocess.PIPE for Popen constructor and it
solved my problem. Previously I haven't supllied anything for it and
constructed used default value for it.

Big thanks, Andy!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Roland.Puntaier

>
> On 24/10/2007, Andy Kittner <[hidden email]> wrote:
> > Okay, I had another look at your error message, and at the
> > subproccess.py file. It seems like it gets an 'ERROR_INVALID_HANDLE'
> > when it tries to duplicate the stdin handle. Do you supply something
as
> > stdin? If yes does the error also occur if you leave it as normal, and
> > if no can you try something like:
> > p = subprocess.Popen(command,
> >                       stdin = subprocess.PIPE, stdout =
subprocess.PIPE,
> >                       stderr = subprocess.PIPE)
> > p.stdin.close()
> > [... your  other code...]
> I've provided stdin=subprocess.PIPE for Popen constructor and it
> solved my problem. Previously I haven't supllied anything for it and
> constructed used default value for it.
>

Some time ago I had a problem which I think is the same as yours is/was.
I patched SubProcess.py:

        def _make_inheritable(self, handle):
            """Return a duplicate of handle, which is inheritable"""
            if handle==None: handle=-1
            return DuplicateHandle(GetCurrentProcess(), handle,
                                   GetCurrentProcess(), 0, 1,
                                   DUPLICATE_SAME_ACCESS)



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: python 2.5 scripting in vim on windows: subprocess problem

Andy Kittner
In reply to this post by Dmitry Teslenko
On Thu, Oct 25, 2007 at 04:22:32PM +0400, Dmitry Teslenko wrote:
> I've provided stdin=subprocess.PIPE for Popen constructor and it
> solved my problem. Previously I haven't supllied anything for it and
> constructed used default value for it.
Hmm, there is a comment in subprocess.py about a bug regarding the
redirection of only some of the three handles that was fixed in
python-2.5.1, so if you run 2.5.0 you may have hit it.

Well in any case, all that counts is that your code does what you
wanted it to do now ;-)

> Big thanks, Andy!
Glad I could help you.

Andy

--
Murphy's Law is recursive.  Washing your car to make it rain doesn't work.

attachment0 (196 bytes) Download Attachment