Rev 5322: Move BzrLibraryState to its own module and prepare to start testing it. in http://bazaar.launchpad.net/~lifeless/bzr/globalzapping
Robert Collins
robertc at robertcollins.net
Fri Jun 25 07:12:57 BST 2010
At http://bazaar.launchpad.net/~lifeless/bzr/globalzapping
------------------------------------------------------------
revno: 5322
revision-id: robertc at robertcollins.net-20100625061256-x2mbkcmcz5xb81wx
parent: robertc at robertcollins.net-20100625061121-0cx949gxhae5aeib
committer: Robert Collins <robertc at robertcollins.net>
branch nick: globalzapping
timestamp: Fri 2010-06-25 16:12:56 +1000
message:
Move BzrLibraryState to its own module and prepare to start testing it.
=== modified file 'bzrlib/__init__.py'
--- a/bzrlib/__init__.py 2010-06-21 20:03:23 +0000
+++ b/bzrlib/__init__.py 2010-06-25 06:12:56 +0000
@@ -131,12 +131,12 @@
# bzr has various bits of global state that are slowly being eliminated.
# This variable is intended to permit any new state-like things to be attached
-# to a BzrLibraryState object rather than getting new global variables that
-# need to be hunted down. Accessing the current BzrLibraryState through this
-# variable is not encouraged: it is better to pass it around as part of the
-# context of an operation than to look it up directly, but when that is too
-# hard, it is better to use this variable than to make a branch new global
-# variable.
+# to a library_state.BzrLibraryState object rather than getting new global
+# variables that need to be hunted down. Accessing the current BzrLibraryState
+# through this variable is not encouraged: it is better to pass it around as
+# part of the context of an operation than to look it up directly, but when
+# that is too hard, it is better to use this variable than to make a branch new
+# global variable.
# If using this variable by looking it up (because it can't be easily obtained)
# it is important to store the reference you get, rather than looking it up
# repeatedly; that way your code will behave properly in the bzrlib test suite
@@ -144,82 +144,6 @@
global_state = None
-class BzrLibraryState(object):
- """The state about how bzrlib has been configured.
-
- :ivar saved_state: The bzrlib.global_state at the time __enter__ was
- called.
- :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
- should occur when the use of bzrlib is completed. This is initialised
- in __enter__ and executed in __exit__.
- """
-
- def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
- """Create library start for normal use of bzrlib.
-
- Most applications that embed bzrlib, including bzr itself, should just
- call bzrlib.initialize(), but it is possible to use the state class
- directly.
-
- More options may be added in future so callers should use named
- arguments.
-
- BzrLibraryState implements the Python 2.5 Context Manager protocol
- PEP343, and can be used with the with statement. Upon __enter__ the
- global variables in use by bzr are set, and they are cleared on
- __exit__.
-
- :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`.
- """
- self.setup_ui = setup_ui
- self.stdin = stdin
- self.stdout = stdout
- self.stderr = stderr
-
- def __enter__(self):
- # 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
- # TestRunBzrSubprocess may fail.
- if version_info[3] == 'final':
- from bzrlib.symbol_versioning import suppress_deprecation_warnings
- suppress_deprecation_warnings(override=True)
-
- import bzrlib.cleanup
- import bzrlib.trace
- self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
- bzrlib.trace.enable_default_logging()
-
- if self.setup_ui:
- import bzrlib.ui
- stdin = self.stdin or sys.stdin
- stdout = self.stdout or sys.stdout
- stderr = self.stderr or sys.stderr
- bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
- stdin, stdout, stderr)
- global global_state
- self.saved_state = global_state
- global_state = self
- return self # This is bound to the 'as' clause in a with statement.
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.cleanups.cleanup_now()
- import bzrlib.ui
- bzrlib.trace._flush_stdout_stderr()
- bzrlib.trace._flush_trace()
- import bzrlib.osutils
- bzrlib.osutils.report_extension_load_failures()
- bzrlib.ui.ui_factory.__exit__(None, None, None)
- bzrlib.ui.ui_factory = None
- global global_state
- global_state = self.saved_state
- return False # propogate exceptions.
-
-
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
"""Set up everything needed for normal use of bzrlib.
@@ -239,7 +163,8 @@
otherwise stopping use of bzrlib. Advanced callers can use
BzrLibraryState directly.
"""
- return BzrLibraryState(setup_ui=setup_ui, stdin=stdin,
+ import bzrlib.library_state
+ return bzrlib.library_state.BzrLibraryState(setup_ui=setup_ui, stdin=stdin,
stdout=stdout, stderr=stderr)
=== added file 'bzrlib/library_state.py'
--- a/bzrlib/library_state.py 1970-01-01 00:00:00 +0000
+++ b/bzrlib/library_state.py 2010-06-25 06:12:56 +0000
@@ -0,0 +1,109 @@
+# Copyright (C) 2010 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""The core state needed to make use of bzr is managed here."""
+
+__all__ = [
+ 'BzrLibraryState',
+ ]
+
+import sys
+
+import bzrlib
+
+
+class BzrLibraryState(object):
+ """The state about how bzrlib has been configured.
+
+ 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
+ should occur when the use of bzrlib is completed. This is initialised
+ in __enter__ and executed in __exit__.
+ """
+
+ def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
+ """Create library start for normal use of bzrlib.
+
+ Most applications that embed bzrlib, including bzr itself, should just
+ call bzrlib.initialize(), but it is possible to use the state class
+ directly.
+
+ More options may be added in future so callers should use named
+ arguments.
+
+ BzrLibraryState implements the Python 2.5 Context Manager protocol
+ PEP343, and can be used with the with statement. Upon __enter__ the
+ global variables in use by bzr are set, and they are cleared on
+ __exit__.
+
+ :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`.
+ """
+ self.setup_ui = setup_ui
+ self.stdin = stdin
+ self.stdout = stdout
+ self.stderr = stderr
+
+ def __enter__(self):
+ # 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
+ # TestRunBzrSubprocess may fail.
+ import bzrlib
+ if bzrlib.version_info[3] == 'final':
+ from bzrlib.symbol_versioning import suppress_deprecation_warnings
+ warning_cleanup = suppress_deprecation_warnings(override=True)
+ else:
+ warning_cleanup = None
+
+ import bzrlib.cleanup
+ import bzrlib.trace
+ self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
+ if warning_cleanup:
+ self.cleanups.add_cleanup(warning_cleanup)
+ bzrlib.trace.enable_default_logging()
+
+ if self.setup_ui:
+ import bzrlib.ui
+ stdin = self.stdin or sys.stdin
+ stdout = self.stdout or sys.stdout
+ stderr = self.stderr or sys.stderr
+ bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
+ stdin, stdout, stderr)
+ self.saved_state = bzrlib.global_state
+ bzrlib.global_state = self
+ return self # This is bound to the 'as' clause in a with statement.
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.cleanups.cleanup_now()
+ import bzrlib.ui
+ bzrlib.trace._flush_stdout_stderr()
+ bzrlib.trace._flush_trace()
+ import bzrlib.osutils
+ bzrlib.osutils.report_extension_load_failures()
+ bzrlib.ui.ui_factory.__exit__(None, None, None)
+ bzrlib.ui.ui_factory = None
+ global global_state
+ global_state = self.saved_state
+ return False # propogate exceptions.
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2010-06-25 01:49:49 +0000
+++ b/bzrlib/tests/__init__.py 2010-06-25 06:12:56 +0000
@@ -3733,6 +3733,7 @@
'bzrlib.tests.test_knit',
'bzrlib.tests.test_lazy_import',
'bzrlib.tests.test_lazy_regex',
+ 'bzrlib.tests.test_library_state',
'bzrlib.tests.test_lock',
'bzrlib.tests.test_lockable_files',
'bzrlib.tests.test_lockdir',
=== added file 'bzrlib/tests/test_library_state.py'
--- a/bzrlib/tests/test_library_state.py 1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/test_library_state.py 2010-06-25 06:12:56 +0000
@@ -0,0 +1,28 @@
+# Copyright (C) 2010 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""Tests for BzrLibraryState."""
+
+import bzrlib
+from bzrlib import (
+ library_state,
+ tests,
+ )
+
+
+class TestLibraryState(tests.TestCase):
+
+ pass
More information about the bazaar-commits
mailing list