[Patches] [ python-Patches-695275 ] environment parameter for popen2

SourceForge.net noreply at sourceforge.net
Sat Apr 2 19:10:44 CEST 2005


Patches item #695275, was opened at 2003-02-28 21:21
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=695275&group_id=5470

Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Bernhard Herzog (bernhard)
Assigned to: Nobody/Anonymous (nobody)
Summary: environment parameter for popen2

Initial Comment:
The patch provides a way to specify the environment for
the subprocess run by the popen2, popen3 and popen4
functions in the popen2 module on platforms that use
the Popen3 class. It does that by adding this parameter
to the __init__ method of Popen3 and the popenN
functions calling execvpe in _run_child if the env
parameter was not None.

This patch obviously doesn't add this parameter on e.g.
Windows. AFAICT from posixmodule.c where popen is
implemented via CreateProcess on Windows it would be at
least theorectically possible to implement it on
Windows as well. I don't have the need nor the time for
that unfortunately.

Being able to specify the environment for the
subprocess in popen is very useful for e.g. running a
CGI from Zope. ZCGI for instance modifies os.environ so
that the subprocess inherits it but that cause problems
because other threads may occasionally see a modified
environment. This is particularly bad for the oracle
client libraries on Linux which require certain
environment settings.



----------------------------------------------------------------------

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-04-02 19:10

Message:
Logged In: YES 
user_id=1188172

I have made a documentation patch (attached here; where to
submit else?). However, since similar popen functions are
available in the os module, it is questionable if this
should be applied because there may be confusion about the
different signature.


Index: Doc/lib/libpopen2.tex
===================================================================
RCS file:
/cvsroot/python/python/dist/src/Doc/lib/libpopen2.tex,v
retrieving revision 1.17.2.2
diff -c -r1.17.2.2 libpopen2.tex
*** Doc/lib/libpopen2.tex       7 Jan 2005 06:57:26 -0000  
    1.17.2.2
--- Doc/lib/libpopen2.tex       2 Apr 2005 17:10:29 -0000
***************
*** 14,20 ****
  Note that starting with Python 2.0, this functionality is
available
  using functions from the \refmodule{os} module which have
the same
  names as the factory functions here, but the order of the
return
! values is more intuitive in the \refmodule{os} module
variants.

  The primary interface offered by this module is a trio of
factory
  functions.  For each of these, if \var{bufsize} is specified,
--- 14,21 ----
  Note that starting with Python 2.0, this functionality is
available
  using functions from the \refmodule{os} module which have
the same
  names as the factory functions here, but the order of the
return
! values is different and more intuitive in the
\refmodule{os} module
! variants.

  The primary interface offered by this module is a trio of
factory
  functions.  For each of these, if \var{bufsize} is specified,
***************
*** 29,55 ****
  \function{os.spawnv()}). If \var{cmd} is a string it will
be passed to the
  shell (as with \function{os.system()}).

  The only way to retrieve the return codes for the child
processes is
  by using the \method{poll()} or \method{wait()} methods on the
  \class{Popen3} and \class{Popen4} classes; these are only
available on
  \UNIX.  This information is not available when using the
  \function{popen2()}, \function{popen3()}, and
\function{popen4()}
  functions, or the equivalent functions in the
\refmodule{os} module.
! (Note that the tuples returned by the \refmodule{os}
module's functions
! are in a different order from the ones returned by the
\module{popen2}
! module.)

! \begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{,
mode}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout}, \var{child_stdin})}.
  \end{funcdesc}

! \begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{,
mode}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout}, \var{child_stdin},
\var{child_stderr})}.
  \end{funcdesc}

! \begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{,
mode}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout_and_stderr}, \var{child_stdin})}.
  \versionadded{2.0}
--- 30,59 ----
  \function{os.spawnv()}). If \var{cmd} is a string it will
be passed to the
  shell (as with \function{os.system()}).

+ \versionadded[If you specify the \var{env} parameter,
which is only available on \UNIX,
+ it will be the environment of the subprocess as described
for the
+ \function{spawn*e()} functions in the \refmodule{os}
module.]{2.5}
+
  The only way to retrieve the return codes for the child
processes is
  by using the \method{poll()} or \method{wait()} methods on the
  \class{Popen3} and \class{Popen4} classes; these are only
available on
  \UNIX.  This information is not available when using the
  \function{popen2()}, \function{popen3()}, and
\function{popen4()}
  functions, or the equivalent functions in the
\refmodule{os} module.
! Also, you could use the \refmodule{subprocess} module
which provides
! a more general approach to spawning of child processes.

! \begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{,
mode\optional{, env}}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout}, \var{child_stdin})}.
  \end{funcdesc}

! \begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{,
mode\optional{, env}}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout}, \var{child_stdin},
\var{child_stderr})}.
  \end{funcdesc}

! \begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{,
mode\optional{, env}}}}
  Executes \var{cmd} as a sub-process.  Returns the file objects
  \code{(\var{child_stdout_and_stderr}, \var{child_stdin})}.
  \versionadded{2.0}
***************
*** 60,66 ****
  functions is also available.  These are not used for the
Windows
  implementation, and are not available on that platform.

! \begin{classdesc}{Popen3}{cmd\optional{,
capturestderr\optional{, bufsize}}}
  This class represents a child process.  Normally,
\class{Popen3}
  instances are created using the \function{popen2()} and
  \function{popen3()} factory functions described above.
--- 64,70 ----
  functions is also available.  These are not used for the
Windows
  implementation, and are not available on that platform.

! \begin{classdesc}{Popen3}{cmd\optional{,
capturestderr\optional{, bufsize\optional{, env}}}}
  This class represents a child process.  Normally,
\class{Popen3}
  instances are created using the \function{popen2()} and
  \function{popen3()} factory functions described above.
***************
*** 69,79 ****
  objects, the parameter \var{cmd} is the shell command to
execute in a
  sub-process.  The \var{capturestderr} flag, if true,
specifies that
  the object should capture standard error output of the
child process.
! The default is false.  If the \var{bufsize} parameter is
specified, it
  specifies the size of the I/O buffers to/from the child
process.
  \end{classdesc}

! \begin{classdesc}{Popen4}{cmd\optional{, bufsize}}
  Similar to \class{Popen3}, but always captures standard
error into the
  same file object as standard output.  These are typically
created
  using \function{popen4()}.
--- 73,86 ----
  objects, the parameter \var{cmd} is the shell command to
execute in a
  sub-process.  The \var{capturestderr} flag, if true,
specifies that
  the object should capture standard error output of the
child process.
! The default is false.  If the \var{bufsize} parameter is
given, it
  specifies the size of the I/O buffers to/from the child
process.
+ If the \var{env} parameter is given (it must be a
mapping), it will
+ be the environment of the new process (instead of a copy
of the
+ current process's).
  \end{classdesc}

! \begin{classdesc}{Popen4}{cmd\optional{,
bufsize\optional{, env}}}
  Similar to \class{Popen3}, but always captures standard
error into the
  same file object as standard output.  These are typically
created
  using \function{popen4()}.


----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2004-08-19 13:36

Message:
Logged In: YES 
user_id=21627

Can you please add changes to Doc/lib/libpopen2.tex as well?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=695275&group_id=5470


More information about the Patches mailing list