Rev 5749: (mbp) bzrlib.initialize() now actually initializes, in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Fri Apr 1 02:46:38 UTC 2011
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 5749 [merge]
revision-id: pqm at pqm.ubuntu.com-20110401024635-dkc6q8xysk46kej9
parent: pqm at pqm.ubuntu.com-20110331175912-fhowhforwlzn11d3
parent: mbp at canonical.com-20110401014642-do8ybycbx8uwhbsi
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-04-01 02:46:35 +0000
message:
(mbp) bzrlib.initialize() now actually initializes,
as well as returning a context manager (Martin Pool)
modified:
bzrlib/__init__.py __init__.py-20050309040759-33e65acf91bbcd5d
bzrlib/library_state.py library_state.py-20100625053036-962zdkiik8k6m5jx-1
doc/developers/integration.txt integration.txt-20080404022341-2lorxocp1in07zij-1
doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/__init__.py'
--- a/bzrlib/__init__.py 2011-03-17 17:18:11 +0000
+++ b/bzrlib/__init__.py 2011-03-29 09:50:04 +0000
@@ -157,13 +157,18 @@
More options may be added in future so callers should use named arguments.
+ The object returned by this function can be used as a contex manager
+ through the 'with' statement to automatically shut down when the process
+ is finished with bzrlib. However (from bzr 2.4) it's not necessary to
+ separately enter the context as well as starting bzr: bzrlib is ready to
+ go when this function returns.
+
:param setup_ui: If true (default) use a terminal UI; otherwise
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
the caller.
:param stdin, stdout, stderr: If provided, use these for terminal IO;
otherwise use the files in `sys`.
- :return: A context manager for the use of bzrlib. The __enter__ method of
- this context needs to be called before it takes effect, and the __exit__
+ :return: A context manager for the use of bzrlib. The __exit__
should be called by the caller before exiting their process or
otherwise stopping use of bzrlib. Advanced callers can use
BzrLibraryState directly.
@@ -178,7 +183,10 @@
else:
ui_factory = None
tracer = trace.DefaultConfig()
- return library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
+ state = library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
+ # Start automatically in case people don't realize this returns a context.
+ state._start()
+ return state
def test_suite():
=== modified file 'bzrlib/library_state.py'
--- a/bzrlib/library_state.py 2011-02-08 16:06:21 +0000
+++ b/bzrlib/library_state.py 2011-03-29 09:50:04 +0000
@@ -31,7 +31,7 @@
This is the core state needed to make use of bzr. The current instance is
currently always exposed as bzrlib.global_state, but we desired to move
to a point where no global state is needed at all.
-
+
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
called.
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
@@ -61,8 +61,16 @@
"""
self._ui = ui
self._trace = trace
+ self.started = False
def __enter__(self):
+ if not self.started:
+ self._start()
+ return self # This is bound to the 'as' clause in a with statement.
+
+ def _start(self):
+ """Do all initialization.
+ """
# NB: This function tweaks so much global state it's hard to test it in
# isolation within the same interpreter. It's not reached on normal
# in-process run_bzr calls. If it's broken, we expect that
@@ -86,7 +94,7 @@
self.saved_state = bzrlib.global_state
bzrlib.global_state = self
- return self # This is bound to the 'as' clause in a with statement.
+ self.started = True
def __exit__(self, exc_type, exc_val, exc_tb):
self.cleanups.cleanup_now()
=== modified file 'doc/developers/integration.txt'
--- a/doc/developers/integration.txt 2011-02-04 05:07:34 +0000
+++ b/doc/developers/integration.txt 2011-03-29 09:50:04 +0000
@@ -27,9 +27,14 @@
bzrlib needs ways to handle user input, passwords, a place to emit
progress bars, logging setup appropriately for your program. The easiest
way to set all this up in the same fashion ``bzr`` does is to call
-``bzrlib.initialize``. This returns a context manager within which bzrlib
-functions will work correctly. See the pydoc for ``bzrlib.initialize`` for
-more information. In Python 2.4 the ``with`` keyword is not supported and
+``bzrlib.initialize``.
+
+This returns a context manager within which bzrlib functions will work
+correctly. See the pydoc for ``bzrlib.initialize`` for more information.
+(You can get away without entering the context manager, because the setup
+work happens directly from ``initialize``.)
+
+In Python 2.4 the ``with`` keyword is not supported and
so you need to use the context manager manually::
# This sets up your ~/.bzr.log, ui factory and so on and so forth. It is
=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt 2011-03-30 11:50:40 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt 2011-04-01 01:46:42 +0000
@@ -59,6 +59,12 @@
* ``Hooks.create_hook`` is now deprecated in favour of ``Hooks.add_hook``.
(Jelmer Vernooij)
+* If you call `bzrlib.initialize` but forget to enter the resulting object
+ as a context manager, bzrlib will now be initialized anyhow.
+ (Previously simple programs calling bzrlib might find the library was
+ mysteriously silent.)
+ (Martin Pool)
+
Internals
*********
More information about the bazaar-commits
mailing list