Merge lp:~mikemc/ubuntuone-client/add-no-reactor-test into lp:ubuntuone-client

Proposed by Mike McCracken on 2012-07-30
Status: Merged
Approved by: Mike McCracken on 2012-07-30
Approved revision: 1287
Merged at revision: 1282
Proposed branch: lp:~mikemc/ubuntuone-client/add-no-reactor-test
Merge into: lp:ubuntuone-client
Diff against target: 113 lines (+79/-0)
4 files modified
Makefile.am (+1/-0)
run-mac-tests (+2/-0)
run-tests.bat (+2/-0)
tests/platform/check_reactor_import.py (+74/-0)
To merge this branch: bzr merge lp:~mikemc/ubuntuone-client/add-no-reactor-test
Reviewer Review Type Date Requested Status
Brian Curtin (community) Approve on 2012-07-30
dobey (community) 2012-07-30 Approve on 2012-07-30
Review via email: mp+117328@code.launchpad.net

Commit Message

- Add test script that fails if reactor is imported from the wrong place (LP: #1030961)

Description of the Change

- Add test script that fails if reactor is imported from the wrong place (LP: #1030961)

To post a comment you must log in.
dobey (dobey) wrote :

As this is entirely new code, and it is Python, I think it would be good to use the new syntax for certain things, and import unicode_literals and print_statement from __future__.

review: Needs Fixing
1287. By Mike McCracken on 2012-07-30

use new-style print function, format syntax

dobey (dobey) wrote :

The %-notation for strings is still valid and acceptable, if you prefer it. I just meant the necessary things like unicode string literals (to expose potential issues when they come up) and the print function. Thanks.

review: Approve
Brian Curtin (brian.curtin) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile.am'
2--- Makefile.am 2012-07-11 17:23:37 +0000
3+++ Makefile.am 2012-07-30 21:47:17 +0000
4@@ -56,6 +56,7 @@
5 if test "x$(builddir)" == "x$(srcdir)"; then \
6 PYTHONPATH="$(PYTHONPATH)" u1trial -r $(REACTOR) -p tests/platform/windows,tests/proxy -i "test_windows.py,test_darwin.py,test_fsevents_daemon.py" tests || exit 1; \
7 PYTHONPATH="$(PYTHONPATH)" u1trial -r qt4 -p tests/platform/windows -i "test_windows.py,test_darwin.py" tests/proxy || exit 1; \
8+ PYTHONPATH="$(PYTHONPATH)" $(PYTHON) tests/platform/check_reactor_import.py || exit 1; \
9 fi
10 rm -rf _trial_temp
11
12
13=== modified file 'run-mac-tests'
14--- run-mac-tests 2012-06-27 19:48:24 +0000
15+++ run-mac-tests 2012-07-30 21:47:17 +0000
16@@ -45,3 +45,5 @@
17 python $u1trial --reactor=twisted -i "test_linux.py,test_windows.py" -p tests/platform/linux "$MODULE"
18 rm -rf _trial_temp
19 rm -rf build
20+
21+python tests/platform/check_reactor_import.py
22
23=== modified file 'run-tests.bat'
24--- run-tests.bat 2012-06-27 17:38:05 +0000
25+++ run-tests.bat 2012-07-30 21:47:17 +0000
26@@ -82,6 +82,8 @@
27 ECHO Performing style checks...
28 "%PYTHONEXEPATH%" "%LINTPATH%"
29
30+"%PYTHONEXEPATH%" tests\platform\check_reactor_import.py
31+
32 :: if pep8 is not present, move to the end
33 IF EXIST "%PEP8PATH%" (
34 "%PEP8PATH%" --repeat ubuntuone
35
36=== added file 'tests/platform/check_reactor_import.py'
37--- tests/platform/check_reactor_import.py 1970-01-01 00:00:00 +0000
38+++ tests/platform/check_reactor_import.py 2012-07-30 21:47:17 +0000
39@@ -0,0 +1,74 @@
40+#! /usr/bin/python
41+#
42+# Copyright (C) 2012 Canonical Ltd.
43+#
44+# This program is free software: you can redistribute it and/or modify it
45+# under the terms of the GNU General Public License version 3, as published
46+# by the Free Software Foundation.
47+#
48+# This program is distributed in the hope that it will be useful, but
49+# WITHOUT ANY WARRANTY; without even the implied warranties of
50+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
51+# PURPOSE. See the GNU General Public License for more details.
52+#
53+# You should have received a copy of the GNU General Public License along
54+# with this program. If not, see <http://www.gnu.org/licenses/>.
55+#
56+# In addition, as a special exception, the copyright holders give
57+# permission to link the code of portions of this program with the
58+# OpenSSL library under certain conditions as described in each
59+# individual source file, and distribute linked combinations
60+# including the two.
61+# You must obey the GNU General Public License in all respects
62+# for all of the code used other than OpenSSL. If you modify
63+# file(s) with this exception, you may extend this exception to your
64+# version of the file(s), but you are not obligated to do so. If you
65+# do not wish to do so, delete this exception statement from your
66+# version. If you delete this exception statement from all source
67+# files in the program, then also delete it here.
68+"""A script that checks for unintended imports of twisted.internet.reactor."""
69+
70+# NOTE: the goal of this script is to avoid a bug that affects
71+# ubuntuone-control-panel on windows and darwin. Those platforms use
72+# the qt4reactor, and will break if the default reactor is installed
73+# first. This can happen if a module used by control-panel (such as
74+# ubuntuone.platform.credentials), imports reactor. Only sub-modules
75+# that are not used by ubuntuone-control-panel can safely import
76+# reactor at module-level.
77+
78+from __future__ import (unicode_literals, print_function)
79+
80+import __builtin__
81+
82+import sys
83+import traceback
84+
85+
86+def fake_import(*args, **kwargs):
87+ """A wrapper for __import__ that dies when importing reactor."""
88+ imp_name_base = args[0]
89+
90+ if len(args) == 4 and args[3] is not None:
91+ imp_names = ["{0}.{1}".format(imp_name_base, sm)
92+ for sm in args[3]]
93+ else:
94+ imp_names = [imp_name_base]
95+
96+ for imp_name in imp_names:
97+ if 'twisted.internet.reactor' == imp_name:
98+ print("ERROR: should not import reactor here:")
99+ traceback.print_stack()
100+ sys.exit(1)
101+
102+ r = real_import(*args, **kwargs)
103+ return r
104+
105+
106+if __name__ == '__main__':
107+
108+ real_import = __builtin__.__import__
109+ __builtin__.__import__ = fake_import
110+
111+ subs = ["", ".tools", ".logger", ".credentials"]
112+ for module in ["ubuntuone.platform" + p for p in subs]:
113+ m = __import__(module)

Subscribers

People subscribed via source and target branches