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
1=== modified file 'lib/lp/codehosting/__init__.py'
2--- lib/lp/codehosting/__init__.py 2017-09-27 02:12:20 +0000
3+++ lib/lp/codehosting/__init__.py 2017-12-19 13:14:50 +0000
4@@ -17,9 +17,11 @@
5 import os
6
7 import bzrlib
8+from bzrlib.branch import Branch
9 from bzrlib.plugin import load_plugins
10 # This import is needed so that bzr's logger gets registered.
11 import bzrlib.trace
12+from zope.security import checker
13
14 from lp.services.config import config
15
16@@ -63,3 +65,16 @@
17
18
19 load_bundled_plugin("weave_fmt")
20+
21+
22+def dont_wrap_class_and_subclasses(cls):
23+ checker.BasicTypes.update({cls: checker.NoProxy})
24+ for subcls in cls.__subclasses__():
25+ dont_wrap_class_and_subclasses(subcls)
26+
27+
28+# Don't wrap Branch or its subclasses in Zope security proxies. Make sure
29+# the various LoomBranch classes are present first.
30+import bzrlib.plugins.loom.branch
31+bzrlib.plugins.loom.branch
32+dont_wrap_class_and_subclasses(Branch)
33
34=== modified file 'lib/lp_sitecustomize.py'
35--- lib/lp_sitecustomize.py 2017-10-21 19:11:38 +0000
36+++ lib/lp_sitecustomize.py 2017-12-19 13:14:50 +0000
37@@ -8,8 +8,8 @@
38 import itertools
39 import logging
40 import os
41+import sys
42 import warnings
43-import sys
44
45 from twisted.internet.defer import (
46 Deferred,
47@@ -127,23 +127,6 @@
48 logging.getLogger('txn').addHandler(txn_handler)
49
50
51-def dont_wrap_class_and_subclasses(cls):
52- checker.BasicTypes.update({cls: checker.NoProxy})
53- for subcls in cls.__subclasses__():
54- dont_wrap_class_and_subclasses(subcls)
55-
56-
57-def dont_wrap_bzr_branch_classes():
58- from bzrlib.branch import Branch
59- # Load bzr plugins
60- import lp.codehosting
61- lp.codehosting
62- # Force LoomBranch classes to be listed as subclasses of Branch
63- import bzrlib.plugins.loom.branch
64- bzrlib.plugins.loom.branch
65- dont_wrap_class_and_subclasses(Branch)
66-
67-
68 def silence_warnings():
69 """Silence warnings across the entire Launchpad project."""
70 # pycrypto-2.0.1 on Python2.6:
71@@ -190,7 +173,6 @@
72 customizeMimetypes()
73 silence_warnings()
74 customize_logger()
75- dont_wrap_bzr_branch_classes()
76 checker.BasicTypes.update({defaultdict: checker.NoProxy})
77 checker.BasicTypes.update({Deferred: checker.NoProxy})
78 checker.BasicTypes.update({DeferredList: checker.NoProxy})