forked from tomerfiliba-org/rpyc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement python3 comparison protocol
py3 does not handle `__cmp__`. Note further that python3 implicitly adds `__hash__=None` to the class members during class construction if there is an `__eq__` defined. The result is that we see errors like this: class X: def __hash__(self): return hash(self.val) def __eq__(self, other): return self.val == other.val >>> hash(conn.modules.__main__.X()) TypeError: unhashable instance To fix this we need to either have `__hash__` AND the comparison methods in `_local_netref_attrs`or neither of them. Fixes tomerfiliba-org#280, tomerfiliba-org#293, tomerfiliba-org#267 Closes tomerfiliba-org#281 Supersedes tomerfiliba-org#268
- Loading branch information
Showing
3 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import sys | ||
import rpyc | ||
import unittest | ||
|
||
is_py3 = sys.version_info >= (3,) | ||
|
||
class Meta(type): | ||
|
||
def __hash__(self): | ||
return 4321 | ||
|
||
Base = Meta('Base', (object,), {}) | ||
|
||
class Foo(Base): | ||
def __hash__(self): | ||
return 1234 | ||
|
||
class Bar(Foo): | ||
pass | ||
|
||
class Mux(Foo): | ||
def __eq__(self, other): | ||
return True | ||
|
||
|
||
class TestContextManagers(unittest.TestCase): | ||
def setUp(self): | ||
self.conn = rpyc.classic.connect_thread() | ||
|
||
def tearDown(self): | ||
self.conn.close() | ||
|
||
def test_hash_class(self): | ||
hesh = self.conn.builtins.hash | ||
mod = self.conn.modules.test_magic | ||
self.assertEqual(hash(mod.Base), 4321) | ||
self.assertEqual(hash(mod.Foo), 4321) | ||
self.assertEqual(hash(mod.Bar), 4321) | ||
self.assertEqual(hash(mod.Base().__class__), 4321) | ||
self.assertEqual(hash(mod.Foo().__class__), 4321) | ||
self.assertEqual(hash(mod.Bar().__class__), 4321) | ||
|
||
basecl_ = mod.Foo().__class__.__mro__[1] | ||
object_ = mod.Foo().__class__.__mro__[2] | ||
self.assertEqual(hash(basecl_), hesh(basecl_)) | ||
self.assertEqual(hash(object_), hesh(object_)) | ||
self.assertEqual(hash(object_), hesh(self.conn.builtins.object)) | ||
|
||
def test_hash_obj(self): | ||
hesh = self.conn.builtins.hash | ||
mod = self.conn.modules.test_magic | ||
obj = mod.Base() | ||
|
||
self.assertNotEqual(hash(obj), 1234) | ||
self.assertNotEqual(hash(obj), 4321) | ||
self.assertEqual(hash(obj), hesh(obj)) | ||
|
||
self.assertEqual(hash(mod.Foo()), 1234) | ||
self.assertEqual(hash(mod.Bar()), 1234) | ||
if is_py3: | ||
with self.assertRaises(TypeError): | ||
hash(mod.Mux()) | ||
else: | ||
self.assertEqual(hash(mod.Mux()), 1234) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |