Issue #15064: Make BaseManager.enter() start server if necessary. · python/cpython@ac38571 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1281,9 +1281,14 @@ their parent process exits. The manager classes are defined in the
1281 1281
1282 1282 The address used by the manager.
1283 1283
1284 - Manager objects support the context manager protocol -- see
1285 -:ref:`typecontextmanager`. :meth:`__enter__` returns the
1286 - manager object, and :meth:`__exit__` calls :meth:`shutdown`.
1284 + .. versionchanged:: 3.3
1285 + Manager objects support the context manager protocol -- see
1286 +:ref:`typecontextmanager`. :meth:`__enter__` starts the server
1287 + process (if it has not already started) and then returns the
1288 + manager object. :meth:`__exit__` calls :meth:`shutdown`.
1289 +
1290 + In previous versions :meth:`__enter__` did not start the
1291 + manager's server process if it was not already started.
1287 1292
1288 1293 .. class:: SyncManager
1289 1294
Original file line number Diff line number Diff line change
@@ -561,6 +561,9 @@ def _number_of_objects(self):
561 561 conn.close()
562 562
563 563 def __enter__(self):
564 +if self._state.value == State.INITIAL:
565 +self.start()
566 +assert self._state.value == State.STARTED
564 567 return self
565 568
566 569 def __exit__(self, exc_type, exc_val, exc_tb):
Original file line number Diff line number Diff line change
@@ -1888,7 +1888,27 @@ class _TestMyManager(BaseTestCase):
1888 1888 def test_mymanager(self):
1889 1889 manager = MyManager()
1890 1890 manager.start()
1891 +self.common(manager)
1892 +manager.shutdown()
1893 +
1894 +# If the manager process exited cleanly then the exitcode
1895 +# will be zero. Otherwise (after a short timeout)
1896 +# terminate() is used, resulting in an exitcode of -SIGTERM.
1897 +self.assertEqual(manager._process.exitcode, 0)
1898 +
1899 +def test_mymanager_context(self):
1900 +with MyManager() as manager:
1901 +self.common(manager)
1902 +self.assertEqual(manager._process.exitcode, 0)
1903 +
1904 +def test_mymanager_context_prestarted(self):
1905 +manager = MyManager()
1906 +manager.start()
1907 +with manager:
1908 +self.common(manager)
1909 +self.assertEqual(manager._process.exitcode, 0)
1891 1910
1911 +def common(self, manager):
1892 1912 foo = manager.Foo()
1893 1913 bar = manager.Bar()
1894 1914 baz = manager.baz()
@@ -1911,12 +1931,6 @@ def test_mymanager(self):
1911 1931
1912 1932 self.assertEqual(list(baz), [i*i for i in range(10)])
1913 1933
1914 -manager.shutdown()
1915 -
1916 -# If the manager process exited cleanly then the exitcode
1917 -# will be zero. Otherwise (after a short timeout)
1918 -# terminate() is used, resulting in an exitcode of -SIGTERM.
1919 -self.assertEqual(manager._process.exitcode, 0)
1920 1934
1921 1935 #
1922 1936 # Test of connecting to a remote server and using xmlrpclib for serialization