From 498f7a9c1e88bbfcb97dae41729a4c1b6d43c82c Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 26 Jun 2019 15:07:27 -0700 Subject: [PATCH 1/6] Add a test for error handling in os.sched_setaffinity. --- Lib/test/test_os.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 18cd78b55f6019..09c32531221a9d 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2560,6 +2560,12 @@ def test_waitpid(self): status = os.waitpid(pid, 0) self.assertEqual(status, (pid, 0)) +class AffinityTests(unittest.TestCase): + + @unittest.skipUnless(hasattr(os, 'sched_setaffinity'), "test needs os.sched_setaffinity") + def test_os_sched_setaffinity_error(self): + bad_iter = map(int, "0X") + self.assertRaises(ValueError, os.sched_setaffinity, 0, bad_iter) class SpawnTests(unittest.TestCase): def create_args(self, *, with_env=False, use_bytes=False): From 2a1ed85aaaca2124d5c74ef4ca212242cc09f4d7 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 26 Jun 2019 15:07:57 -0700 Subject: [PATCH 2/6] Fix error handling in os.sched_setaffinity. --- Modules/posixmodule.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5f17fce1a717ca..bac24f0da997af 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6415,6 +6415,10 @@ os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) } Py_CLEAR(iterator); + if (PyErr_Occurred()) { + goto error; + } + if (sched_setaffinity(pid, setsize, cpu_set)) { posix_error(); goto error; From 32652a3d708ddbb0e097e82dd95ea3ff14516686 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 26 Jun 2019 22:25:08 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2019-06-26-22-25-05.bpo-37420.CxFJ09.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-26-22-25-05.bpo-37420.CxFJ09.rst diff --git a/Misc/NEWS.d/next/Library/2019-06-26-22-25-05.bpo-37420.CxFJ09.rst b/Misc/NEWS.d/next/Library/2019-06-26-22-25-05.bpo-37420.CxFJ09.rst new file mode 100644 index 00000000000000..dea1a292501449 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-26-22-25-05.bpo-37420.CxFJ09.rst @@ -0,0 +1,2 @@ +:func:`os.sched_setaffinity` now correctly handles errors that arise during iteration over its ``mask`` argument. +Patch by Brandt Bucher. \ No newline at end of file From 7d2129d703091511f92e3d65d07cee2226482077 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 26 Jun 2019 23:39:01 -0700 Subject: [PATCH 4/6] Move test from test_os.py to test_posix.py. --- Lib/test/test_os.py | 7 ------- Lib/test/test_posix.py | 1 + 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 09c32531221a9d..4e9b86707ad8c0 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2560,13 +2560,6 @@ def test_waitpid(self): status = os.waitpid(pid, 0) self.assertEqual(status, (pid, 0)) -class AffinityTests(unittest.TestCase): - - @unittest.skipUnless(hasattr(os, 'sched_setaffinity'), "test needs os.sched_setaffinity") - def test_os_sched_setaffinity_error(self): - bad_iter = map(int, "0X") - self.assertRaises(ValueError, os.sched_setaffinity, 0, bad_iter) - class SpawnTests(unittest.TestCase): def create_args(self, *, with_env=False, use_bytes=False): self.exitcode = 17 diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index afa1398d4edc0d..9bdd2848afc81b 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -1368,6 +1368,7 @@ def test_sched_setaffinity(self): self.assertEqual(posix.sched_getaffinity(0), mask) self.assertRaises(OSError, posix.sched_setaffinity, 0, []) self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10]) + self.assertRaises(ValueError, posix.sched_setaffinity, 0, map(int, "0X")) self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128]) self.assertRaises(OSError, posix.sched_setaffinity, -1, mask) From 385e96490c486e792e22680999814ae6c0c50268 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 26 Jun 2019 23:40:23 -0700 Subject: [PATCH 5/6] Check for errors immediately after iterating. --- Modules/posixmodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index bac24f0da997af..197607c9cb101c 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6413,11 +6413,10 @@ os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) } CPU_SET_S(cpu, setsize, cpu_set); } - Py_CLEAR(iterator); - if (PyErr_Occurred()) { goto error; } + Py_CLEAR(iterator); if (sched_setaffinity(pid, setsize, cpu_set)) { posix_error(); From 655bb0e2158812e2fb275d122b47a7fb711cede6 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 26 Jun 2019 23:42:59 -0700 Subject: [PATCH 6/6] Add back stripped newline. --- Lib/test/test_os.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 4e9b86707ad8c0..18cd78b55f6019 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2560,6 +2560,7 @@ def test_waitpid(self): status = os.waitpid(pid, 0) self.assertEqual(status, (pid, 0)) + class SpawnTests(unittest.TestCase): def create_args(self, *, with_env=False, use_bytes=False): self.exitcode = 17