Merge lp:~cjwatson/launchpad/avoid-importing-bzr-plugins-from-site into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 18518
Proposed branch: lp:~cjwatson/launchpad/avoid-importing-bzr-plugins-from-site
Merge into: lp:launchpad
Diff against target: 78 lines (+16/-19)
2 files modified
lib/lp/codehosting/__init__.py (+15/-0)
lib/lp_sitecustomize.py (+1/-19)
To merge this branch: bzr merge lp:~cjwatson/launchpad/avoid-importing-bzr-plugins-from-site
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+335379@code.launchpad.net

Commit message

Suppress Branch security proxies in lp.codehosting, not lp_sitecustomize.

The latter runs too early to be able to safely import Bazaar plugins.

Description of the change

Python doesn't set up sys.getfilesystemencoding() until shortly after it's finished importing site, so if we try to import Bazaar plugins from a sitecustomize hook then any initialisation calls they make to bzrlib.i18n.load_plugin_translations will fail, causing great confusion. Anything that needs Bazaar plugins is already supposed to import lp.codehosting, so this should be a safe rearrangement.

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve
Revision history for this message
Colin Watson (cjwatson) wrote :

Self-reviewing since this is a testfix and the test suite should catch anything overly interesting.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/codehosting/__init__.py'
--- lib/lp/codehosting/__init__.py 2017-09-27 02:12:20 +0000
+++ lib/lp/codehosting/__init__.py 2017-12-19 13:14:50 +0000
@@ -17,9 +17,11 @@
17import os17import os
1818
19import bzrlib19import bzrlib
20from bzrlib.branch import Branch
20from bzrlib.plugin import load_plugins21from bzrlib.plugin import load_plugins
21# This import is needed so that bzr's logger gets registered.22# This import is needed so that bzr's logger gets registered.
22import bzrlib.trace23import bzrlib.trace
24from zope.security import checker
2325
24from lp.services.config import config26from lp.services.config import config
2527
@@ -63,3 +65,16 @@
6365
6466
65load_bundled_plugin("weave_fmt")67load_bundled_plugin("weave_fmt")
68
69
70def dont_wrap_class_and_subclasses(cls):
71 checker.BasicTypes.update({cls: checker.NoProxy})
72 for subcls in cls.__subclasses__():
73 dont_wrap_class_and_subclasses(subcls)
74
75
76# Don't wrap Branch or its subclasses in Zope security proxies. Make sure
77# the various LoomBranch classes are present first.
78import bzrlib.plugins.loom.branch
79bzrlib.plugins.loom.branch
80dont_wrap_class_and_subclasses(Branch)
6681
=== modified file 'lib/lp_sitecustomize.py'
--- lib/lp_sitecustomize.py 2017-10-21 19:11:38 +0000
+++ lib/lp_sitecustomize.py 2017-12-19 13:14:50 +0000
@@ -8,8 +8,8 @@
8import itertools8import itertools
9import logging9import logging
10import os10import os
11import sys
11import warnings12import warnings
12import sys
1313
14from twisted.internet.defer import (14from twisted.internet.defer import (
15 Deferred,15 Deferred,
@@ -127,23 +127,6 @@
127 logging.getLogger('txn').addHandler(txn_handler)127 logging.getLogger('txn').addHandler(txn_handler)
128128
129129
130def dont_wrap_class_and_subclasses(cls):
131 checker.BasicTypes.update({cls: checker.NoProxy})
132 for subcls in cls.__subclasses__():
133 dont_wrap_class_and_subclasses(subcls)
134
135
136def dont_wrap_bzr_branch_classes():
137 from bzrlib.branch import Branch
138 # Load bzr plugins
139 import lp.codehosting
140 lp.codehosting
141 # Force LoomBranch classes to be listed as subclasses of Branch
142 import bzrlib.plugins.loom.branch
143 bzrlib.plugins.loom.branch
144 dont_wrap_class_and_subclasses(Branch)
145
146
147def silence_warnings():130def silence_warnings():
148 """Silence warnings across the entire Launchpad project."""131 """Silence warnings across the entire Launchpad project."""
149 # pycrypto-2.0.1 on Python2.6:132 # pycrypto-2.0.1 on Python2.6:
@@ -190,7 +173,6 @@
190 customizeMimetypes()173 customizeMimetypes()
191 silence_warnings()174 silence_warnings()
192 customize_logger()175 customize_logger()
193 dont_wrap_bzr_branch_classes()
194 checker.BasicTypes.update({defaultdict: checker.NoProxy})176 checker.BasicTypes.update({defaultdict: checker.NoProxy})
195 checker.BasicTypes.update({Deferred: checker.NoProxy})177 checker.BasicTypes.update({Deferred: checker.NoProxy})
196 checker.BasicTypes.update({DeferredList: checker.NoProxy})178 checker.BasicTypes.update({DeferredList: checker.NoProxy})