URL: https://github.com/freeipa/freeipa/pull/259
Author: tiran
 Title: #259: Minor fixes for IPAVersion class
Action: opened

PR body:
"""
Py3: classes with __eq__ must provide __hash__ function or set __hash__
to None.
Comparison function like __eq__ must signal unsupported types by
returning NotImplemented. Python turns this in a proper TypeError.
Make the version member read-only and cache _bytes represention.

https://fedorahosted.org/freeipa/ticket/6473

Signed-off-by: Christian Heimes <chei...@redhat.com>
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/259/head:pr259
git checkout pr259
From d9d55de476d0a0ca152f7825b7e4f921b5461728 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 18 Nov 2016 12:24:09 +0100
Subject: [PATCH] Minor fixes for IPAVersion class

Py3: classes with __eq__ must provide __hash__ function or set __hash__
to None.
Comparison function like __eq__ must signal unsupported types by
returning NotImplemented. Python turns this in a proper TypeError.
Make the version member read-only and cache _bytes represention.

https://fedorahosted.org/freeipa/ticket/6473

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipaplatform/redhat/tasks.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/ipaplatform/redhat/tasks.py b/ipaplatform/redhat/tasks.py
index dbe005a..5d627be 100644
--- a/ipaplatform/redhat/tasks.py
+++ b/ipaplatform/redhat/tasks.py
@@ -83,20 +83,26 @@ def selinux_enabled():
 class IPAVersion(object):
 
     def __init__(self, version):
-        self.version = version
+        self._version = version
+        self._bytes = version.encode('utf-8')
 
     @property
-    def _bytes(self):
-        return self.version.encode('utf-8')
+    def version(self):
+        return self._version
 
     def __eq__(self, other):
-        assert isinstance(other, IPAVersion)
+        if not isinstance(other, IPAVersion):
+            return NotImplemented
         return _librpm.rpmvercmp(self._bytes, other._bytes) == 0
 
     def __lt__(self, other):
-        assert isinstance(other, IPAVersion)
+        if not isinstance(other, IPAVersion):
+            return NotImplemented
         return _librpm.rpmvercmp(self._bytes, other._bytes) < 0
 
+    def __hash__(self):
+        return hash(self._version)
+
 
 class RedHatTaskNamespace(BaseTaskNamespace):
 
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to