[Python-modules-commits] [python-setproctitle] 06/13: Import python-setproctitle_1.0.1.orig.tar.gz

Orestis Ioannou oorestisime-guest at moszumanska.debian.org
Sun Sep 18 12:02:49 UTC 2016


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

oorestisime-guest pushed a commit to branch master
in repository python-setproctitle.

commit e334c91ece2e56e0c4c9f422f9c080fbf1d1297f
Author: Orestis Ioannou <orestis at oioannou.com>
Date:   Wed Sep 14 00:43:12 2016 +0200

    Import python-setproctitle_1.0.1.orig.tar.gz
---
 COPYRIGHT                  |  2 +-
 HISTORY                    |  7 +++++
 PKG-INFO                   | 16 ++++++++--
 README                     |  7 +++--
 setup.py                   |  4 +--
 src/c.h                    |  2 +-
 src/setproctitle.c         | 31 ++++++++++++++++---
 tests/setproctitle_test.py | 77 ++++++++++++++++++++++++++++++++++++++--------
 8 files changed, 120 insertions(+), 26 deletions(-)

diff --git a/COPYRIGHT b/COPYRIGHT
index 9cb3f80..b970a5a 100644
--- a/COPYRIGHT
+++ b/COPYRIGHT
@@ -1,4 +1,4 @@
-Copyright (c) 2009, Daniele Varrazzo <daniele.varrazzo at gmail.com>
+Copyright (c) 2009-2010, Daniele Varrazzo <daniele.varrazzo at gmail.com>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
diff --git a/HISTORY b/HISTORY
index e0e8cdb..db33587 100644
--- a/HISTORY
+++ b/HISTORY
@@ -1,6 +1,13 @@
 Releases history
 ----------------
 
+Version 1.0.1
+~~~~~~~~~~~~~
+
+- ``setproctitle()`` works even when Python messes up with argv, e.g. when run
+  with the -m option.
+
+
 Version 1.0
 ~~~~~~~~~~~
 
diff --git a/PKG-INFO b/PKG-INFO
index 76b9be0..75bc771 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.0
 Name: setproctitle
-Version: 1.0
+Version: 1.0.1
 Summary: Allow customization of the process title.
 Home-page: http://code.google.com/p/py-setproctitle/
 Author: Daniele Varrazzo
@@ -71,12 +71,15 @@ Description: A ``setproctitle`` implementation for Python
         - Windows
         
         Notice that on Windows there is no way to change the process string:
-        what the module does is to create a Named Object whose value can be read
-        in tools such as Process Explorer.
+        what the module does is to create a *Named Object* whose value can be read
+        using a tool such as `Process Explorer`_ (contribution of a more useful tool
+        to be used together with ``setproctitle`` would be well accepted).
         
         The module can probably work on HP-UX, but I haven't found any to test with.
         It is unlikely that it can work on Solaris instead.
         
+        .. _Process Explorer: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
+        
         
         Other known implementations and discussions
         -------------------------------------------
@@ -97,6 +100,13 @@ Description: A ``setproctitle`` implementation for Python
         Releases history
         ----------------
         
+        Version 1.0.1
+        ~~~~~~~~~~~~~
+        
+        - ``setproctitle()`` works even when Python messes up with argv, e.g. when run
+        with the -m option.
+        
+        
         Version 1.0
         ~~~~~~~~~~~
         
diff --git a/README b/README
index 9fe63ca..43ef1da 100644
--- a/README
+++ b/README
@@ -62,12 +62,15 @@ platforms:
 - Windows
 
 Notice that on Windows there is no way to change the process string:
-what the module does is to create a Named Object whose value can be read
-in tools such as Process Explorer.
+what the module does is to create a *Named Object* whose value can be read
+using a tool such as `Process Explorer`_ (contribution of a more useful tool
+to be used together with ``setproctitle`` would be well accepted).
 
 The module can probably work on HP-UX, but I haven't found any to test with.
 It is unlikely that it can work on Solaris instead.
 
+.. _Process Explorer: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
+
 
 Other known implementations and discussions
 -------------------------------------------
diff --git a/setup.py b/setup.py
index 0e418d6..da53d01 100644
--- a/setup.py
+++ b/setup.py
@@ -2,10 +2,10 @@
 """
 setproctitle setup script.
 
-Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+Copyright (c) 2009-2010 Daniele Varrazzo <daniele.varrazzo at gmail.com>
 """
 
-VERSION = '1.0'
+VERSION = '1.0.1'
 
 import os
 import re
diff --git a/src/c.h b/src/c.h
index f40cdab..18f0639 100644
--- a/src/c.h
+++ b/src/c.h
@@ -3,7 +3,7 @@
  * c.h
  *    A few fundamental C definitions.
  *
- * Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+ * Copyright (c) 2009-2010 Daniele Varrazzo <daniele.varrazzo at gmail.com>
  *-------------------------------------------------------------------------
  */
 
diff --git a/src/setproctitle.c b/src/setproctitle.c
index 6d580a0..a03f7b1 100644
--- a/src/setproctitle.c
+++ b/src/setproctitle.c
@@ -3,7 +3,7 @@
  * setproctitle.c
  *    Python extension module to update and read the process title.
  *
- * Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+ * Copyright (c) 2009-2010 Daniele Varrazzo <daniele.varrazzo at gmail.com>
  *
  * The module allows Python code to access the functions get_ps_display()
  * and set_ps_display().  The process title initialization (functions
@@ -108,6 +108,29 @@ join_argv(int argc, char **argv)
 }
 
 
+/* Return a copy of argv referring to the original arg area.
+ *
+ * python -m messes up with arg (issue #8): ensure to have a vector to the
+ * original args or save_ps_display_args() will stop processing too soon.
+ *
+ * Return a buffer allocated with malloc: should be cleaned up with free()
+ * (it is never released though).
+ */
+static char **
+fix_argv(int argc, char **argv)
+{
+    char **buf = (char **)malloc(argc * sizeof(char *));
+    int i;
+    char *ptr = argv[0];
+    for (i = 0; i < argc; ++i) {
+        buf[i] = ptr;
+        ptr += strlen(ptr) + 1;
+    }
+
+    return buf;
+}
+
+
 /* Initialization function for the module (*must* be called initsetproctitle) */
 
 static char setproctitle_module_documentation[] =
@@ -120,9 +143,8 @@ initsetproctitle(void)
     PyObject *m, *d;
 
     /* Create the module and add the functions */
-    m = Py_InitModule4("setproctitle", spt_methods,
-        setproctitle_module_documentation,
-        (PyObject*)NULL,PYTHON_API_VERSION);
+    m = Py_InitModule3("setproctitle", spt_methods,
+        setproctitle_module_documentation);
 
     /* Add version string to the module*/
     d = PyModule_GetDict(m);
@@ -134,6 +156,7 @@ initsetproctitle(void)
     int argc;
     char **argv;
     Py_GetArgcArgv(&argc, &argv);
+    argv = fix_argv(argc, argv);
     save_ps_display_args(argc, argv);
 
     /* Set up the first title to fully initialize the code */
diff --git a/tests/setproctitle_test.py b/tests/setproctitle_test.py
index 3a5edbb..8faa90d 100644
--- a/tests/setproctitle_test.py
+++ b/tests/setproctitle_test.py
@@ -2,18 +2,20 @@
 
 Use nosetests to run this test suite.
 
-Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+Copyright (c) 2009-2010 Daniele Varrazzo <daniele.varrazzo at gmail.com>
 """
 
 import os
 import re
 import sys
+import shutil
+import tempfile
 import unittest
 from subprocess import Popen, PIPE, STDOUT
 
 from nose.plugins.skip import SkipTest
 
-class GetProcTitleTestCase(unittest.TestCase):
+class SetproctitleTestCase(unittest.TestCase):
     def test_runner(self):
         """Test the script execution method."""
         rv = self.run_script("""
@@ -35,22 +37,18 @@ class GetProcTitleTestCase(unittest.TestCase):
         rv = self.run_script(r"""
             import setproctitle
             setproctitle.setproctitle('Hello, world!')
-            
+
             import os
             print os.getpid()
-            print os.popen("ps -o pid,command").read()
+            # ps can fail on kfreebsd arch
+            # (http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=460331)
+            print os.popen("ps -o pid,command 2> /dev/null").read()
             """)
         lines = filter(None, rv.splitlines())
         pid = lines.pop(0)
         pids = dict([r.strip().split(None, 1) for r in lines])
 
-        title = pids[pid]
-        if 'bsd' in sys.platform:
-            # BSD's setproctitle decorates the title with the process name
-            procname = os.path.basename(sys.executable)
-            title = ' '.join([t for t in title.split(' ')
-                if procname not in t])  
-
+        title = self._clean_up_title(pids[pid])
         self.assertEqual(title, "Hello, world!")
 
     def test_prctl(self):
@@ -112,7 +110,40 @@ class GetProcTitleTestCase(unittest.TestCase):
         self.assertEqual(test, 'setenv-value')
         self.assert_(path.endswith('fakepath'), path)
 
-    def run_script(self, script, args=None):
+    def test_issue_8(self):
+        """Test that the module works with 'python -m'."""
+        module = 'spt_issue_8'
+        pypath = os.environ.get('PYTHONPATH', None)
+        dir = tempfile.mkdtemp()
+        os.environ['PYTHONPATH'] = dir + os.pathsep + (pypath or '')
+        try:
+            open(dir + '/' + module + '.py', 'w').write(
+                self._clean_whitespaces(r"""
+                    import setproctitle
+                    setproctitle.setproctitle("Hello, module!")
+
+                    import os
+                    print os.getpid()
+                    print os.popen("ps -o pid,command 2> /dev/null").read()
+                """))
+
+            rv = self.run_script(args="-m " + module)
+            lines = filter(None, rv.splitlines())
+            pid = lines.pop(0)
+            pids = dict([r.strip().split(None, 1) for r in lines])
+
+            title = self._clean_up_title(pids[pid])
+            self.assertEqual(title, "Hello, module!")
+
+        finally:
+            shutil.rmtree(dir, ignore_errors=True)
+            if pypath is not None:
+                os.environ['PYTHONPATH'] = pypath
+            else:
+                del os.environ['PYTHONPATH']
+
+
+    def run_script(self, script=None, args=None):
         """run a script in a separate process.
 
         if the script completes successfully, return the concatenation of
@@ -122,11 +153,13 @@ class GetProcTitleTestCase(unittest.TestCase):
         if args:
             cmdline = cmdline + " " + args
 
-        script = self._clean_whitespaces(script)
         proc = Popen(cmdline,
                 stdin=PIPE, stdout=PIPE, stderr=STDOUT,
                 shell=True, close_fds=True)
 
+        if script is not None:
+            script = self._clean_whitespaces(script)
+
         out = proc.communicate(script)[0]
         if 0 != proc.returncode:
             print out
@@ -157,6 +190,24 @@ class GetProcTitleTestCase(unittest.TestCase):
                         % (i + 1, line.strip()))
             script[i] = line[len(spaces):]
 
+        # drop final blank lines: they produce import errors
+        while script and script[-1].isspace():
+            del script[-1]
+
         assert not script[0][0].isspace(), script[0]
         return ''.join(script)
 
+    def _clean_up_title(self, title):
+        """Clean up a string from the prefix added by the platform.
+        """
+        # BSD's setproctitle decorates the title with the process name.
+        if 'bsd' in sys.platform:
+            procname = os.path.basename(sys.executable)
+            title = ' '.join([t for t in title.split(' ')
+                if procname not in t])  
+
+        return title
+
+
+if __name__ == '__main__':
+    unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-setproctitle.git



More information about the Python-modules-commits mailing list