diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/distutils/command/__init__.py | 1 | ||||
-rw-r--r-- | Lib/distutils/command/check.py | 5 | ||||
-rw-r--r-- | Lib/distutils/tests/test_spawn.py | 20 | ||||
-rw-r--r-- | Lib/multiprocessing/managers.py | 13 | ||||
-rw-r--r-- | Lib/multiprocessing/pool.py | 3 | ||||
-rw-r--r-- | Lib/test/test_multiprocessing.py | 32 | ||||
-rw-r--r-- | Lib/test/test_unittest.py | 23 | ||||
-rw-r--r-- | Lib/unittest.py | 20 |
8 files changed, 107 insertions, 10 deletions
diff --git a/Lib/distutils/command/__init__.py b/Lib/distutils/command/__init__.py index f7dcde4..c379edb 100644 --- a/Lib/distutils/command/__init__.py +++ b/Lib/distutils/command/__init__.py @@ -23,6 +23,7 @@ __all__ = ['build', 'bdist_rpm', 'bdist_wininst', 'check', + 'upload', # These two are reserved for future use: #'bdist_sdux', #'bdist_pkgtool', diff --git a/Lib/distutils/command/check.py b/Lib/distutils/command/check.py index d164f3f..5dd73b0 100644 --- a/Lib/distutils/command/check.py +++ b/Lib/distutils/command/check.py @@ -27,8 +27,9 @@ try: self.messages.append((level, message, children, kwargs)) HAS_DOCUTILS = True -except ImportError: - # docutils is not installed +except Exception: + # Catch all exceptions because exceptions besides ImportError probably + # indicate that docutils is not ported to Py3k. HAS_DOCUTILS = False class check(Command): diff --git a/Lib/distutils/tests/test_spawn.py b/Lib/distutils/tests/test_spawn.py new file mode 100644 index 0000000..33e8bcf --- /dev/null +++ b/Lib/distutils/tests/test_spawn.py @@ -0,0 +1,20 @@ +"""Tests for distutils.spawn.""" +import unittest +from distutils.spawn import _nt_quote_args + +class SpawnTestCase(unittest.TestCase): + + def test_nt_quote_args(self): + + for (args, wanted) in ((['with space', 'nospace'], + ['"with space"', 'nospace']), + (['nochange', 'nospace'], + ['nochange', 'nospace'])): + res = _nt_quote_args(args) + self.assertEquals(res, wanted) + +def test_suite(): + return unittest.makeSuite(SpawnTestCase) + +if __name__ == "__main__": + unittest.main(defaultTest="test_suite") diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index cb102e5..40af8f0 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -478,12 +478,15 @@ class BaseManager(object): dispatch(conn, None, 'dummy') self._state.value = State.STARTED - def start(self): + def start(self, initializer=None, initargs=()): ''' Spawn a server process for this manager object ''' assert self._state.value == State.INITIAL + if initializer is not None and not hasattr(initializer, '__call__'): + raise TypeError('initializer must be a callable') + # pipe over which we will retrieve address of server reader, writer = connection.Pipe(duplex=False) @@ -491,7 +494,7 @@ class BaseManager(object): self._process = Process( target=type(self)._run_server, args=(self._registry, self._address, self._authkey, - self._serializer, writer), + self._serializer, writer, initializer, initargs), ) ident = ':'.join(str(i) for i in self._process._identity) self._process.name = type(self).__name__ + '-' + ident @@ -512,10 +515,14 @@ class BaseManager(object): ) @classmethod - def _run_server(cls, registry, address, authkey, serializer, writer): + def _run_server(cls, registry, address, authkey, serializer, writer, + initializer=None, initargs=()): ''' Create a server, report its address and run it ''' + if initializer is not None: + initializer(*initargs) + # create server server = cls._Server(registry, address, authkey, serializer) diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index bf2608d..5c1805f 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -92,6 +92,9 @@ class Pool(object): except NotImplementedError: processes = 1 + if initializer is not None and not hasattr(initializer, '__call__'): + raise TypeError('initializer must be a callable') + self._pool = [] for i in range(processes): w = self.Process( diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 69309ba..100738e 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -1832,7 +1832,37 @@ class OtherTest(unittest.TestCase): multiprocessing.connection.answer_challenge, _FakeConnection(), b'abc') -testcases_other = [OtherTest, TestInvalidHandle] +# +# Test Manager.start()/Pool.__init__() initializer feature - see issue 5585 +# + +def initializer(ns): + ns.test += 1 + +class TestInitializers(unittest.TestCase): + def setUp(self): + self.mgr = multiprocessing.Manager() + self.ns = self.mgr.Namespace() + self.ns.test = 0 + + def tearDown(self): + self.mgr.shutdown() + + def test_manager_initializer(self): + m = multiprocessing.managers.SyncManager() + self.assertRaises(TypeError, m.start, 1) + m.start(initializer, (self.ns,)) + self.assertEqual(self.ns.test, 1) + m.shutdown() + + def test_pool_initializer(self): + self.assertRaises(TypeError, multiprocessing.Pool, initializer=1) + p = multiprocessing.Pool(1, initializer, (self.ns,)) + p.close() + p.join() + self.assertEqual(self.ns.test, 1) + +testcases_other = [OtherTest, TestInvalidHandle, TestInitializers] # # diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index aaa3dc5..d0d2049 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2311,6 +2311,16 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): # from this TestCase instance but since its a local nothing else # will ever notice that. + def testAssertIs(self): + thing = object() + self.assertIs(thing, thing) + self.assertRaises(self.failureException, self.assertIs, thing, object()) + + def testAssertIsNot(self): + thing = object() + self.assertIsNot(thing, object()) + self.assertRaises(self.failureException, self.assertIsNot, thing, thing) + def testAssertIn(self): animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} @@ -2454,6 +2464,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): # Test that sequences of unhashable objects can be tested for sameness: self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]]) + self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) self.assertRaises(self.failureException, self.assertSameElements, [[1]], [[2]]) @@ -2988,6 +2999,18 @@ class TestLongMessage(TestCase): "^unexpectedly None$", "^unexpectedly None : oops$"]) + def testAssertIs(self): + self.assertMessages('assertIs', (None, 'foo'), + ["^None is not 'foo'$", "^oops$", + "^None is not 'foo'$", + "^None is not 'foo' : oops$"]) + + def testAssertIsNot(self): + self.assertMessages('assertIsNot', (None, None), + ["^unexpectedly identical: None$", "^oops$", + "^unexpectedly identical: None$", + "^unexpectedly identical: None : oops$"]) + ###################################################################### ## Main diff --git a/Lib/unittest.py b/Lib/unittest.py index 244a45b..0b7cea4 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -807,6 +807,18 @@ class TestCase(object): standardMsg = '%r unexpectedly found in %r' % (member, container) self.fail(self._formatMessage(msg, standardMsg)) + def assertIs(self, expr1, expr2, msg=None): + """Just like self.assertTrue(a is b), but with a nicer default message.""" + if expr1 is not expr2: + standardMsg = '%r is not %r' % (expr1, expr2) + self.fail(self._formatMessage(msg, standardMsg)) + + def assertIsNot(self, expr1, expr2, msg=None): + """Just like self.assertTrue(a is not b), but with a nicer default message.""" + if expr1 is expr2: + standardMsg = 'unexpectedly identical: %r' % (expr1,) + self.fail(self._formatMessage(msg, standardMsg)) + def assertDictEqual(self, d1, d2, msg=None): self.assert_(isinstance(d1, dict), 'First argument is not a dictionary') self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary') @@ -1020,7 +1032,7 @@ class TestSuite(object): self.addTests(tests) def __repr__(self): - return "<%s tests=%s>" % (_strclass(self.__class__), self._tests) + return "<%s tests=%s>" % (_strclass(self.__class__), list(self)) def __eq__(self, other): if not isinstance(other, self.__class__): @@ -1035,7 +1047,7 @@ class TestSuite(object): def countTestCases(self): cases = 0 - for test in self._tests: + for test in self: cases += test.countTestCases() return cases @@ -1055,7 +1067,7 @@ class TestSuite(object): self.addTest(test) def run(self, result): - for test in self._tests: + for test in self: if result.shouldStop: break test(result) @@ -1066,7 +1078,7 @@ class TestSuite(object): def debug(self): """Run the tests without collecting errors in a TestResult""" - for test in self._tests: + for test in self: test.debug() |