Merge lp:~brian.curtin/ubuntu-sso-client/py3-winreg into lp:ubuntu-sso-client

Proposed by Brian Curtin
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 978
Merged at revision: 980
Proposed branch: lp:~brian.curtin/ubuntu-sso-client/py3-winreg
Merge into: lp:ubuntu-sso-client
Diff against target: 54 lines (+14/-6)
2 files modified
ubuntu_sso/main/tests/test_windows.py (+8/-3)
ubuntu_sso/main/windows.py (+6/-3)
To merge this branch: bzr merge lp:~brian.curtin/ubuntu-sso-client/py3-winreg
Reviewer Review Type Date Requested Status
Manuel de la Peña (community) Approve
Alejandro J. Cura (community) Approve
Review via email: mp+111725@code.launchpad.net

Commit message

Convert _winreg importing to use winreg on both Python 2 and 3

Description of the change

Python 3 changed the _winreg module to winreg. The easiest way to work with that change is to try to import _winreg as winreg, and if that fails, just import winreg since we're on Python 3. Since the names imported from the module are few, move back to importing the module and accessing the names on the module rather than directly importing names from the module. It's easier to maintain that way.

To post a comment you must log in.
Revision history for this message
dobey (dobey) wrote :

I think in these try/except import cases, we should be trying to import the new thing, and only importing the old one if it fails. Doing this also makes it clear by looking at the code, which one is the old one (the fallback).

Revision history for this message
Manuel de la Peña (mandel) wrote :

I agree with dobey, in theory we will be running this on python 3 in the near future.. so making the import fail on python 2 more often than on python 3 makes sense.

review: Needs Fixing
978. By Brian Curtin

Try the 3 way and fall back to the 2 way

Revision history for this message
Alejandro J. Cura (alecu) wrote :

Looks great!

review: Approve
Revision history for this message
Manuel de la Peña (mandel) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/main/tests/test_windows.py'
2--- ubuntu_sso/main/tests/test_windows.py 2012-04-30 16:15:46 +0000
3+++ ubuntu_sso/main/tests/test_windows.py 2012-06-27 20:19:18 +0000
4@@ -29,7 +29,10 @@
5 """Windows specific tests for the main module."""
6
7 # pylint: disable=F0401
8-from _winreg import REG_SZ
9+try:
10+ import winreg
11+except ImportError:
12+ import _winreg as winreg
13
14 from twisted.internet import defer
15
16@@ -111,8 +114,10 @@
17 def test_get_activation_cmdline(self):
18 """Test the get_activation_cmdline method."""
19 sample_value = "test 123"
20- self.patch(windows, "OpenKey", lambda key, subkey: sample_value)
21- self.patch(windows, "QueryValueEx", lambda key, v: (key, REG_SZ))
22+ self.patch(windows.winreg, "OpenKey",
23+ lambda key, subkey: sample_value)
24+ self.patch(windows.winreg, "QueryValueEx",
25+ lambda key, v: (key, winreg.REG_SZ))
26 cmdline = windows.get_activation_cmdline(
27 perspective_broker.SSO_SERVICE_NAME)
28 self.assertEqual(sample_value, cmdline)
29
30=== modified file 'ubuntu_sso/main/windows.py'
31--- ubuntu_sso/main/windows.py 2012-04-30 16:15:46 +0000
32+++ ubuntu_sso/main/windows.py 2012-06-27 20:19:18 +0000
33@@ -41,7 +41,10 @@
34 # pylint: disable=F0401
35 import win32process
36 import win32security
37-from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx
38+try:
39+ import winreg
40+except ImportError:
41+ import _winreg as winreg
42 # pylint: enable=F0401
43
44 from twisted.internet import defer
45@@ -83,9 +86,9 @@
46
47 def get_activation_cmdline(name):
48 """Find the path to the executable callback."""
49- key = OpenKey(HKEY_LOCAL_MACHINE, U1_REG_PATH)
50+ key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, U1_REG_PATH)
51 # pylint: disable=W0612
52- value, registry_type = QueryValueEx(key, "path-" + name)
53+ value, registry_type = winreg.QueryValueEx(key, "path-" + name)
54 return value
55
56

Subscribers

People subscribed via source and target branches