Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
make exception hashable, an addition to to #184
Browse files Browse the repository at this point in the history
  • Loading branch information
lxyu committed Feb 16, 2016
1 parent d87c9a5 commit 40219d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def test_obj_equalcheck():
assert ab.Person(name="hello") == ab2.Person(name="hello")


def test_exc_equalcheck():
ab = thriftpy.load("addressbook.thrift")

assert ab.PersonNotExistsError("exc") != ab.PersonNotExistsError("exc")


def test_cls_equalcheck():
ab = thriftpy.load("addressbook.thrift")
ab2 = thriftpy.load("addressbook.thrift")
Expand All @@ -30,9 +36,13 @@ def test_isinstancecheck():
assert isinstance(ab.PersonNotExistsError(), ab2.PersonNotExistsError)


def test_unhashable():
def test_hashable():
ab = thriftpy.load("addressbook.thrift")

# exception is hashable
hash(ab.PersonNotExistsError("test error"))

# container struct is not hashable
with pytest.raises(TypeError):
hash(ab.Person(name="Tom"))

Expand Down
8 changes: 7 additions & 1 deletion thriftpy/thrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def handle_exception(self, e, result):
setattr(result, exc_name, e)
break
else:
raise
raise e

def process(self, iprot, oprot):
api, seqid, result, call = self.process_in(iprot)
Expand Down Expand Up @@ -328,6 +328,12 @@ def get_processor(self):
class TException(TPayload, Exception):
"""Base class for all thrift exceptions."""

def __hash__(self):
return id(self)

def __eq__(self, other):
return id(self) == id(other)


class TDecodeException(TException):
def __init__(self, name, fid, field, value, ttype, spec=None):
Expand Down

0 comments on commit 40219d4

Please sign in to comment.