[Pkg-privacy-commits] [pyptlib] 73/136: update changelog and make unix-specific stuff cross-platform - there is no cross-platform way to get child-pid so report it explicitly - see http://stackoverflow.com/questions/16323385/get-ppid-in-windows-with-python

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository pyptlib.

commit 589526499618680334ef1f08ce54575232a16b5d
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Tue Aug 6 18:29:57 2013 +0100

    update changelog and make unix-specific stuff cross-platform
    - there is no cross-platform way to get child-pid so report it explicitly
    - see http://stackoverflow.com/questions/16323385/get-ppid-in-windows-with-python
---
 ChangeLog                         |  6 ++++++
 pyptlib/test/test_util_subproc.py | 28 ++++++++++++----------------
 pyptlib/test/util_subproc_main.py | 16 ++++++++++------
 pyptlib/util/subproc.py           |  4 ++--
 4 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f606ea5..c49319f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Changes in version 0.0.4 - UNRELEASED
+ - Add a subprocess management module, which should make it easier to
+   write composed plugins such as obfs-flash, and the 2-INT termination
+   behaviour specified in the PT spec.
+
+
 Changes in version 0.0.3 - 2013-02-18
  - Restrict the MANIFEST.in file some more, so that no .pyc files
    sneak into the release tarballs.
diff --git a/pyptlib/test/test_util_subproc.py b/pyptlib/test/test_util_subproc.py
index deb4d82..f468c1f 100644
--- a/pyptlib/test/test_util_subproc.py
+++ b/pyptlib/test/test_util_subproc.py
@@ -10,10 +10,6 @@ from subprocess import PIPE
 # We ought to run auto_killall(), instead of manually calling proc.terminate()
 # but it's not very good form to use something inside the test for itself. :p
 
-def get_child_pids(pid):
-    # TODO(infinity0): add windows version
-    return subprocess.check_output(("ps h --ppid %s -o pid" % pid).split()).split()
-
 def proc_wait(proc, wait_s):
     time.sleep(wait_s)
     proc.poll() # otherwise it doesn't exit properly
@@ -31,16 +27,16 @@ class SubprocTest(unittest.TestCase):
     def getMainArgs(self):
         return ["python", "./util_subproc_main.py", self.name()]
 
-    def spawnMain(self, cmd=None, *args, **kwargs):
+    def spawnMain(self, cmd=None, stdout=PIPE, **kwargs):
         # spawn the main test process and wait a bit for it to initialise
-        proc = Popen(cmd or self.getMainArgs(), *args, **kwargs)
+        proc = Popen(cmd or self.getMainArgs(), stdout = stdout, **kwargs)
         time.sleep(0.2)
         return proc
 
-    def getOnlyChild(self, proc):
-        children = get_child_pids(proc.pid)
-        self.assertTrue(len(children) == 1)
-        return children[0]
+    def readChildPid(self, proc):
+        line = proc.stdout.readline()
+        self.assertTrue(line.startswith("child "))
+        return int(line.replace("child ", ""))
 
     def test_Popen_IOpassthru(self):
         output = subprocess.check_output(self.getMainArgs())
@@ -51,7 +47,7 @@ class SubprocTest(unittest.TestCase):
         self.assertTrue(len(output) == 0)
 
     def test_trap_sigint_multiple(self):
-        proc = self.spawnMain(stdout=PIPE)
+        proc = self.spawnMain()
         proc.send_signal(signal.SIGINT)
         self.assertEquals("run h1\n", proc.stdout.readline())
         proc.send_signal(signal.SIGINT)
@@ -60,7 +56,7 @@ class SubprocTest(unittest.TestCase):
         proc.terminate()
 
     def test_trap_sigint_reset(self):
-        proc = self.spawnMain(stdout=PIPE)
+        proc = self.spawnMain()
         proc.send_signal(signal.SIGINT)
         self.assertEquals("run h2\n", proc.stdout.readline())
         proc.terminate()
@@ -68,7 +64,7 @@ class SubprocTest(unittest.TestCase):
     def test_killall_kill(self):
         proc = self.spawnMain()
         pid = proc.pid
-        cid = self.getOnlyChild(proc)
+        cid = self.readChildPid(proc)
         self.assertTrue(proc_is_alive(cid), "child did not hang")
         time.sleep(2)
         self.assertTrue(proc_is_alive(cid), "child did not ignore TERM")
@@ -79,7 +75,7 @@ class SubprocTest(unittest.TestCase):
     def test_auto_killall_2_int(self):
         proc = self.spawnMain()
         pid = proc.pid
-        cid = self.getOnlyChild(proc)
+        cid = self.readChildPid(proc)
         # test first signal is ignored
         proc.send_signal(signal.SIGINT)
         proc_wait(proc, 3)
@@ -94,7 +90,7 @@ class SubprocTest(unittest.TestCase):
     def test_auto_killall_term(self):
         proc = self.spawnMain()
         pid = proc.pid
-        cid = self.getOnlyChild(proc)
+        cid = self.readChildPid(proc)
         # test TERM is handled
         proc.send_signal(signal.SIGTERM)
         proc_wait(proc, 3)
@@ -104,7 +100,7 @@ class SubprocTest(unittest.TestCase):
     def test_auto_killall_exit(self):
         proc = self.spawnMain()
         pid = proc.pid
-        cid = self.getOnlyChild(proc)
+        cid = self.readChildPid(proc)
         # test exit is handled. main exits by itself after 1 seconds
         # exit handler takes ~2s to run, usually
         proc_wait(proc, 3)
diff --git a/pyptlib/test/util_subproc_main.py b/pyptlib/test/util_subproc_main.py
index d79500a..16ad795 100644
--- a/pyptlib/test/util_subproc_main.py
+++ b/pyptlib/test/util_subproc_main.py
@@ -9,12 +9,16 @@ from pyptlib.util.subproc import auto_killall, killall, trap_sigint, Popen, SINK
 from subprocess import PIPE
 
 
-def startChild(subcmd, stdout=SINK, **kwargs):
-    return Popen(
+def startChild(subcmd, report=False, stdout=SINK, **kwargs):
+    proc = Popen(
         ["python", "util_subproc_child.py", subcmd],
         stdout = stdout,
         **kwargs
     )
+    if report:
+        print "child %s" % proc.pid
+        sys.stdout.flush()
+    return proc
 
 def sleepIgnoreInts(ignoreNumInts=3):
     for i in xrange(ignoreNumInts):
@@ -48,24 +52,24 @@ def main_trap_sigint_reset(testname, *argv):
     sleepIgnoreInts(1)
 
 def main_killall_kill(testname, *argv):
-    child = startChild(testname)
+    child = startChild(testname, True)
     time.sleep(1)
     killall(4)
     time.sleep(100)
 
 def main_auto_killall_2_int(testname, *argv):
     auto_killall(1)
-    child = startChild("default")
+    child = startChild("default", True)
     child.wait()
 
 def main_auto_killall_term(testname, *argv):
     auto_killall()
-    child = startChild("default")
+    child = startChild("default", True)
     child.wait()
 
 def main_auto_killall_exit(testname, *argv):
     auto_killall()
-    child = startChild("default")
+    child = startChild("default", True)
     time.sleep(1)
 
 if __name__ == "__main__":
diff --git a/pyptlib/util/subproc.py b/pyptlib/util/subproc.py
index add34f3..36d1a32 100644
--- a/pyptlib/util/subproc.py
+++ b/pyptlib/util/subproc.py
@@ -6,6 +6,7 @@ the Popen() here rather than subprocess.Popen() directly.
 
 import atexit
 import inspect
+import os
 import signal
 import subprocess
 import time
@@ -44,8 +45,7 @@ class Popen(subprocess.Popen):
     # use while/readline(); see man page for "python -u" for more details.
 
 def create_sink():
-    # TODO(infinity0): do a windows version of this
-    return open("/dev/null", "w", 0)
+    return open(os.devnull, "w", 0)
 
 _SIGINT_RUN = {}
 def trap_sigint(handler, ignoreNum=0):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git



More information about the Pkg-privacy-commits mailing list