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

Proposed by Brian Curtin on 2012-08-09
Status: Merged
Approved by: Alejandro J. Cura on 2012-08-16
Approved revision: 992
Merged at revision: 992
Proposed branch: lp:~brian.curtin/ubuntu-sso-client/py3-metaclass
Merge into: lp:ubuntu-sso-client
Diff against target: 46 lines (+11/-5)
2 files modified
ubuntu_sso/utils/ipc.py (+8/-2)
ubuntu_sso/utils/tests/test_ipc.py (+3/-3)
To merge this branch: bzr merge lp:~brian.curtin/ubuntu-sso-client/py3-metaclass
Reviewer Review Type Date Requested Status
Eric Casteleijn (community) Approve on 2012-08-16
Alejandro J. Cura (community) 2012-08-09 Approve on 2012-08-16
Review via email: mp+119049@code.launchpad.net

Commit Message

- Switch to Python 2 and 3 safe usage of a metaclass for IPC

Description of the Change

The IPC code makes use of a metaclass, and due to Python 3's changes to metaclasses, we need a version-safe way to continue using it. The easiest way is by using a base class that was created by calling the metaclass directly, which we get from a trivial function.

This is the same as what six.with_metaclass does.

To post a comment you must log in.
Alejandro J. Cura (alecu) wrote :

Nice branch!

Since the new "meta_base" function seems like it might be useful elsewhere, should it be moved to a more common module?

Or perhaps it makes sense that we should start depending on six, since it's being used for the protobuf port to python 3, and it may help us with other porting.

Anyway, I think we should discuss separately from this branch.

review: Approve
Alejandro J. Cura (alecu) wrote :

Sorry, I didn't notice that lint gave this error:

ubuntu_sso/utils/tests/test_ipc.py:
    508: [W0232, RemoteMetaTestCase.test_remote_calls_renamed.TestClass] Class has no __init__ method

review: Needs Fixing
992. By Brian Curtin on 2012-08-14

Get rid of a warning about __init__ not existing

Alejandro J. Cura (alecu) wrote :

+1

review: Approve
Eric Casteleijn (thisfred) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/utils/ipc.py'
2--- ubuntu_sso/utils/ipc.py 2012-06-22 16:51:06 +0000
3+++ ubuntu_sso/utils/ipc.py 2012-08-14 20:40:23 +0000
4@@ -172,10 +172,16 @@
5 return super(RemoteMeta, mcs).__new__(mcs, name, bases, attrs)
6
7
8-class RemoteService(Referenceable, SignalBroadcaster):
9+def meta_base(meta):
10+ """A function to return a metaclass which is called directly.
11+ This is safe for both Python 2 and 3."""
12+ return meta("MetaBase", (object,), {})
13+
14+
15+class RemoteService(meta_base(RemoteMeta),
16+ Referenceable, SignalBroadcaster):
17 """A remote service that provides the methods listed in remote_calls."""
18
19- __metaclass__ = RemoteMeta
20 remote_calls = []
21
22
23
24=== modified file 'ubuntu_sso/utils/tests/test_ipc.py'
25--- ubuntu_sso/utils/tests/test_ipc.py 2012-04-30 16:15:46 +0000
26+++ ubuntu_sso/utils/tests/test_ipc.py 2012-08-14 20:40:23 +0000
27@@ -505,16 +505,16 @@
28 """The remote_calls are renamed."""
29 test_token = object()
30
31- class TestClass(object):
32+ # pylint: disable=W0232
33+ class TestClass(ipc.meta_base(ipc.RemoteMeta)):
34 """A class for testing."""
35
36- __metaclass__ = ipc.RemoteMeta
37-
38 remote_calls = ['test_method']
39
40 def test_method(self):
41 """Fake call."""
42 return test_token
43+ # pylint: enable=W0232
44
45 tc = TestClass()
46 self.assertEquals(tc.test_method(), test_token)

Subscribers

People subscribed via source and target branches