Merge lp:~mbp/bzr/initialize into lp:bzr

Proposed by Martin Pool
Status: Merged
Approved by: John A Meinel
Approved revision: no longer in the source branch.
Merged at revision: 5749
Proposed branch: lp:~mbp/bzr/initialize
Merge into: lp:bzr
Diff against target: 113 lines (+35/-8)
4 files modified
bzrlib/__init__.py (+11/-3)
bzrlib/library_state.py (+10/-2)
doc/developers/integration.txt (+8/-3)
doc/en/release-notes/bzr-2.4.txt (+6/-0)
To merge this branch: bzr merge lp:~mbp/bzr/initialize
Reviewer Review Type Date Requested Status
John A Meinel Approve
Martin Pool Pending
Review via email: mp+55674@code.launchpad.net

Commit message

bzrlib.initialize() now actually initializes, as well as returning a context manager

Description of the change

It's just too perverse that

  import bzrlib
  bzrlib.initialize()

doesn't actually initialize things. This keeps it returning a context manager,
but it's effectively started by default.

To post a comment you must log in.
Revision history for this message
John A Meinel (jameinel) wrote :

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 3/31/2011 6:31 AM, Martin Pool wrote:
> Martin Pool has proposed merging lp:~mbp/bzr/initialize into lp:bzr.
>
> Requested reviews:
> Martin Pool (mbp)
> Related bugs:
> Bug #507710 in Bazaar: "want bzrlib.initialize() to do all typical setup"
> https://bugs.launchpad.net/bzr/+bug/507710
>
> For more details, see:
> https://code.launchpad.net/~mbp/bzr/initialize/+merge/55674
>
> It's just too perverse that
>
> import bzrlib
> bzrlib.initialize()
>
> doesn't actually initialize things. This keeps it returning a context manager,
> but it's effectively started by default.

I like the idea, but I don't really see that we have to not use things
the way they are documented. In my test scripts, I certainly do:

 bzrlib.initialize().__enter__()

So this does make things easier there.

I'm not sure that it is perverse to ask people to use the api the way it
was designed. I suppose it is called 'initialize()' and not
'get_initializer_context()'.

Anyway, if you feel it is worthwhile, that's good enough to overcome my
ambivalence.

 merge: approve

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2UNxEACgkQJdeBCYSNAAMb7QCfSTWWCzijmHp3x6r1y6CMC5xj
jyAAnircUUtEkXg69yfkvbyNkFZ5fHtJ
=Gjdl
-----END PGP SIGNATURE-----

review: Approve
Revision history for this message
Martin Pool (mbp) wrote :

> I like the idea, but I don't really see that we have to not use things
> the way they are documented. In my test scripts, I certainly do:
>
>  bzrlib.initialize().__enter__()
>
> So this does make things easier there.
>
> I'm not sure that it is perverse to ask people to use the api the way it
> was designed. I suppose it is called 'initialize()' and not
> 'get_initializer_context()'.

It's really just the name I think doesn't go with the semantics. As
you say if the name implied that it didn't actually do the
initialization that would be ok.

I wrote this out of annoyance at wondering why it wasn't initializing.
 (I did realize we had the state concept but I thought initialize
automatically entered it.)

Third opinion welcome...

Revision history for this message
John Szakmeister (jszakmeister) wrote :

I agree about the name. It gives the impression that you'll be done. I would not have expected a context manager as a result of that call. I like the change.

Revision history for this message
Andrew Bennetts (spiv) wrote :

Martin Pool wrote:
[…]
> It's really just the name I think doesn't go with the semantics. As
> you say if the name implied that it didn't actually do the
> initialization that would be ok.
>
> I wrote this out of annoyance at wondering why it wasn't initializing.
> (I did realize we had the state concept but I thought initialize
> automatically entered it.)
>
> Third opinion welcome...

I've made this mistake too, even though I had discussed this API with lifeless
when he was designing/implementing it. I'm in favour of this change.

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

On Thu, 2011-03-31 at 08:15 +0000, Martin Pool wrote:
> > I like the idea, but I don't really see that we have to not use things
> > the way they are documented. In my test scripts, I certainly do:
> >
> > bzrlib.initialize().__enter__()
> >
> > So this does make things easier there.
> >
> > I'm not sure that it is perverse to ask people to use the api the way it
> > was designed. I suppose it is called 'initialize()' and not
> > 'get_initializer_context()'.
>
> It's really just the name I think doesn't go with the semantics. As
> you say if the name implied that it didn't actually do the
> initialization that would be ok.
>
> I wrote this out of annoyance at wondering why it wasn't initializing.
> (I did realize we had the state concept but I thought initialize
> automatically entered it.)
I've hit this too. Changing it or the name seems like a good idea. If
neither of us can predict what it does, then it'll be even stranger for
the other external users that should use this API.

Cheers,

Jelmer

Revision history for this message
Martin Pool (mbp) wrote :

sent to pqm by email

Revision history for this message
Martin Pool (mbp) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/__init__.py'
2--- bzrlib/__init__.py 2011-03-17 17:18:11 +0000
3+++ bzrlib/__init__.py 2011-04-01 01:48:32 +0000
4@@ -157,13 +157,18 @@
5
6 More options may be added in future so callers should use named arguments.
7
8+ The object returned by this function can be used as a contex manager
9+ through the 'with' statement to automatically shut down when the process
10+ is finished with bzrlib. However (from bzr 2.4) it's not necessary to
11+ separately enter the context as well as starting bzr: bzrlib is ready to
12+ go when this function returns.
13+
14 :param setup_ui: If true (default) use a terminal UI; otherwise
15 some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
16 the caller.
17 :param stdin, stdout, stderr: If provided, use these for terminal IO;
18 otherwise use the files in `sys`.
19- :return: A context manager for the use of bzrlib. The __enter__ method of
20- this context needs to be called before it takes effect, and the __exit__
21+ :return: A context manager for the use of bzrlib. The __exit__
22 should be called by the caller before exiting their process or
23 otherwise stopping use of bzrlib. Advanced callers can use
24 BzrLibraryState directly.
25@@ -178,7 +183,10 @@
26 else:
27 ui_factory = None
28 tracer = trace.DefaultConfig()
29- return library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
30+ state = library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
31+ # Start automatically in case people don't realize this returns a context.
32+ state._start()
33+ return state
34
35
36 def test_suite():
37
38=== modified file 'bzrlib/library_state.py'
39--- bzrlib/library_state.py 2011-02-08 16:06:21 +0000
40+++ bzrlib/library_state.py 2011-04-01 01:48:32 +0000
41@@ -31,7 +31,7 @@
42 This is the core state needed to make use of bzr. The current instance is
43 currently always exposed as bzrlib.global_state, but we desired to move
44 to a point where no global state is needed at all.
45-
46+
47 :ivar saved_state: The bzrlib.global_state at the time __enter__ was
48 called.
49 :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
50@@ -61,8 +61,16 @@
51 """
52 self._ui = ui
53 self._trace = trace
54+ self.started = False
55
56 def __enter__(self):
57+ if not self.started:
58+ self._start()
59+ return self # This is bound to the 'as' clause in a with statement.
60+
61+ def _start(self):
62+ """Do all initialization.
63+ """
64 # NB: This function tweaks so much global state it's hard to test it in
65 # isolation within the same interpreter. It's not reached on normal
66 # in-process run_bzr calls. If it's broken, we expect that
67@@ -86,7 +94,7 @@
68
69 self.saved_state = bzrlib.global_state
70 bzrlib.global_state = self
71- return self # This is bound to the 'as' clause in a with statement.
72+ self.started = True
73
74 def __exit__(self, exc_type, exc_val, exc_tb):
75 self.cleanups.cleanup_now()
76
77=== modified file 'doc/developers/integration.txt'
78--- doc/developers/integration.txt 2011-02-04 05:07:34 +0000
79+++ doc/developers/integration.txt 2011-04-01 01:48:32 +0000
80@@ -27,9 +27,14 @@
81 bzrlib needs ways to handle user input, passwords, a place to emit
82 progress bars, logging setup appropriately for your program. The easiest
83 way to set all this up in the same fashion ``bzr`` does is to call
84-``bzrlib.initialize``. This returns a context manager within which bzrlib
85-functions will work correctly. See the pydoc for ``bzrlib.initialize`` for
86-more information. In Python 2.4 the ``with`` keyword is not supported and
87+``bzrlib.initialize``.
88+
89+This returns a context manager within which bzrlib functions will work
90+correctly. See the pydoc for ``bzrlib.initialize`` for more information.
91+(You can get away without entering the context manager, because the setup
92+work happens directly from ``initialize``.)
93+
94+In Python 2.4 the ``with`` keyword is not supported and
95 so you need to use the context manager manually::
96
97 # This sets up your ~/.bzr.log, ui factory and so on and so forth. It is
98
99=== modified file 'doc/en/release-notes/bzr-2.4.txt'
100--- doc/en/release-notes/bzr-2.4.txt 2011-03-30 11:50:40 +0000
101+++ doc/en/release-notes/bzr-2.4.txt 2011-04-01 01:48:32 +0000
102@@ -59,6 +59,12 @@
103 * ``Hooks.create_hook`` is now deprecated in favour of ``Hooks.add_hook``.
104 (Jelmer Vernooij)
105
106+* If you call `bzrlib.initialize` but forget to enter the resulting object
107+ as a context manager, bzrlib will now be initialized anyhow.
108+ (Previously simple programs calling bzrlib might find the library was
109+ mysteriously silent.)
110+ (Martin Pool)
111+
112 Internals
113 *********
114