[PATCH 2 of 4 hglib] util: introduce popen

Idan Kamara idankk86 at gmail.com
Mon Sep 26 14:40:59 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1317065864 -10800
# Node ID 15485fa4b35e4fcad4ca4b63ddaa0649e3006003
# Parent  20ffb6486412791e56577e29dc3a95e8fc8a781f
util: introduce popen

diff --git a/hglib/__init__.py b/hglib/__init__.py
--- a/hglib/__init__.py
+++ b/hglib/__init__.py
@@ -14,9 +14,7 @@
                            insecure=insecure)
 
     args.insert(0, HGPATH)
-    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                            close_fds=util.closefds)
-
+    proc = util.popen(args)
     out, err = proc.communicate()
     if proc.returncode:
         raise error.CommandError(args, proc.returncode, out, err)
diff --git a/hglib/client.py b/hglib/client.py
--- a/hglib/client.py
+++ b/hglib/client.py
@@ -19,14 +19,11 @@
             args += ['-R', path]
         if configs:
             args += ['--config'] + configs
-        env = dict(os.environ)
+        env = {}
         if encoding:
             env['HGENCODING'] = encoding
 
-        self.server = subprocess.Popen(args, stdin=subprocess.PIPE,
-                                       stdout=subprocess.PIPE, env=env,
-                                       close_fds=util.closefds)
-
+        self.server = util.popen(args, env)
         self._readhello()
         self._version = None
 
diff --git a/hglib/util.py b/hglib/util.py
--- a/hglib/util.py
+++ b/hglib/util.py
@@ -1,6 +1,4 @@
-import itertools, cStringIO, error, os
-
-closefds = os.name == 'posix'
+import itertools, cStringIO, error, os, subprocess
 
 def grouper(n, iterable):
     ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
@@ -132,3 +130,14 @@
     def __nonzero__(self):
         """ Returns True if the return code was 0, False otherwise """
         return self.ret == 0
+
+close_fds = os.name == 'posix'
+
+def popen(args, env={}):
+    environ = None
+    if env:
+        environ = dict(os.environ)
+        environ.update(env)
+
+    return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+                            close_fds=close_fds, env=environ)


More information about the Mercurial-devel mailing list