summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristján Valur Jónsson <sweskman@gmail.com>2013-03-19 22:07:35 (GMT)
committerKristján Valur Jónsson <sweskman@gmail.com>2013-03-19 22:07:35 (GMT)
commit8927e8f4211dc50b87a6365ed7281452a1931ebf (patch)
treec519bb98128d6fa159ea2ea9b2e1a8c9f2b69067
parentac0866f2ab9aa43c635618681682dd9f6da6219e (diff)
downloadcpython-8927e8f4211dc50b87a6365ed7281452a1931ebf.zip
cpython-8927e8f4211dc50b87a6365ed7281452a1931ebf.tar.gz
cpython-8927e8f4211dc50b87a6365ed7281452a1931ebf.tar.bz2
Issue #12098: multiprocessing on Windows now starts child processes
using the same sys.flags as the current process. Backport from default branch.
-rw-r--r--Lib/multiprocessing/forking.py3
-rw-r--r--Lib/multiprocessing/util.py1
-rw-r--r--Lib/subprocess.py31
-rw-r--r--Lib/test/test_multiprocessing.py34
-rw-r--r--Lib/test/test_support.py17
-rw-r--r--Misc/NEWS4
6 files changed, 72 insertions, 18 deletions
diff --git a/Lib/multiprocessing/forking.py b/Lib/multiprocessing/forking.py
index 1597ae8..dc465b4 100644
--- a/Lib/multiprocessing/forking.py
+++ b/Lib/multiprocessing/forking.py
@@ -361,7 +361,8 @@ else:
return [sys.executable, '--multiprocessing-fork']
else:
prog = 'from multiprocessing.forking import main; main()'
- return [_python_exe, '-c', prog, '--multiprocessing-fork']
+ opts = util._args_from_interpreter_flags()
+ return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork']
def main():
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index fe76bcc..3ce480f 100644
--- a/Lib/multiprocessing/util.py
+++ b/Lib/multiprocessing/util.py
@@ -37,6 +37,7 @@ import weakref
import atexit
import threading # we want threading to install it's
# cleanup function before multiprocessing does
+from subprocess import _args_from_interpreter_flags
from multiprocessing.process import current_process, active_children
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 19a5188..309f9a3 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -482,6 +482,37 @@ def _eintr_retry_call(func, *args):
raise
+# XXX This function is only used by multiprocessing and the test suite,
+# but it's here so that it can be imported when Python is compiled without
+# threads.
+
+def _args_from_interpreter_flags():
+ """Return a list of command-line arguments reproducing the current
+ settings in sys.flags and sys.warnoptions."""
+ flag_opt_map = {
+ 'debug': 'd',
+ # 'inspect': 'i',
+ # 'interactive': 'i',
+ 'optimize': 'O',
+ 'dont_write_bytecode': 'B',
+ 'no_user_site': 's',
+ 'no_site': 'S',
+ 'ignore_environment': 'E',
+ 'verbose': 'v',
+ 'bytes_warning': 'b',
+ 'hash_randomization': 'R',
+ 'py3k_warning': '3',
+ }
+ args = []
+ for flag, opt in flag_opt_map.items():
+ v = getattr(sys.flags, flag)
+ if v > 0:
+ args.append('-' + opt * v)
+ for opt in sys.warnoptions:
+ args.append('-W' + opt)
+ return args
+
+
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
index 9b3f0a9..8091a9f 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -2400,11 +2400,43 @@ class TestNoForkBomb(unittest.TestCase):
self.assertEqual('', err.decode('ascii'))
#
+# Issue 12098: check sys.flags of child matches that for parent
+#
+
+class TestFlags(unittest.TestCase):
+ @classmethod
+ def run_in_grandchild(cls, conn):
+ conn.send(tuple(sys.flags))
+
+ @classmethod
+ def run_in_child(cls):
+ import json
+ r, w = multiprocessing.Pipe(duplex=False)
+ p = multiprocessing.Process(target=cls.run_in_grandchild, args=(w,))
+ p.start()
+ grandchild_flags = r.recv()
+ p.join()
+ r.close()
+ w.close()
+ flags = (tuple(sys.flags), grandchild_flags)
+ print(json.dumps(flags))
+
+ def test_flags(self):
+ import json, subprocess
+ # start child process using unusual flags
+ prog = ('from test.test_multiprocessing import TestFlags; ' +
+ 'TestFlags.run_in_child()')
+ data = subprocess.check_output(
+ [sys.executable, '-E', '-S', '-O', '-c', prog])
+ child_flags, grandchild_flags = json.loads(data.decode('ascii'))
+ self.assertEqual(child_flags, grandchild_flags)
+#
#
#
testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
- TestStdinBadfiledescriptor, TestTimeouts, TestNoForkBomb]
+ TestStdinBadfiledescriptor, TestTimeouts, TestNoForkBomb,
+ TestFlags]
#
#
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 034a374..b3076c1 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -1344,22 +1344,7 @@ def py3k_bytes(b):
def args_from_interpreter_flags():
"""Return a list of command-line arguments reproducing the current
settings in sys.flags."""
- flag_opt_map = {
- 'bytes_warning': 'b',
- 'dont_write_bytecode': 'B',
- 'ignore_environment': 'E',
- 'no_user_site': 's',
- 'no_site': 'S',
- 'optimize': 'O',
- 'py3k_warning': '3',
- 'verbose': 'v',
- }
- args = []
- for flag, opt in flag_opt_map.items():
- v = getattr(sys.flags, flag)
- if v > 0:
- args.append('-' + opt * v)
- return args
+ return subprocess._args_from_interpreter_flags()
def strip_python_stderr(stderr):
"""Strip the stderr of a Python process from potential debug output
diff --git a/Misc/NEWS b/Misc/NEWS
index 67c1e9e..f309e6f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -214,6 +214,10 @@ Core and Builtins
Library
-------
+- Issue #12098: multiprocessing on Windows now starts child processes
+ using the same sys.flags as the current process. Initial patch by
+ Sergey Mezentsev.
+
- Issue #8862: Fixed curses cleanup when getkey is interrputed by a signal.
- Issue #9090: When a socket with a timeout fails with EWOULDBLOCK or EAGAIN,