Skip to content

Commit

Permalink
Pickleable rosbag exceptions (#1210 revisited). (#1652)
Browse files Browse the repository at this point in the history
* test_rosbag/test_bag.py: test, if rosbag exception can be pickled

* rosbag/bag.py: rosbag exceptions can now be unpickled

* pep8
  • Loading branch information
cwecht authored and dirk-thomas committed Apr 1, 2019
1 parent 9ae132c commit 6e76dea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions test/test_rosbag/test/test_bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,20 @@ def _print_bag_records(self, fn):

print(bag._OP_CODES.get(op, op))

# #1209
def test_rosbag_exceptions_are_pickleable(self):
#bag_exception = rosbag.ROSBagException("msg string")
def test(bag_exception):
import pickle
pickle_str = pickle.dumps(bag_exception)
unpickled = pickle.loads(pickle_str)
self.assertTrue(bag_exception.value == unpickled.value)
test(bag.ROSBagException("msg string"))
test(bag.ROSBagFormatException("msg string 2"))
test(bag.ROSBagUnindexedException())
test(bag.ROSBagEncryptNotSupportedException("msg string 3"))
test(bag.ROSBagEncryptException("msg string 4"))

if __name__ == '__main__':
import rostest
PKG='rosbag'
Expand Down
8 changes: 6 additions & 2 deletions tools/rosbag/src/rosbag/bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ class ROSBagException(Exception):
"""
Base class for exceptions in rosbag.
"""
def __init__(self, value):
def __init__(self, value=None):
self.value = value
#fix for #1209. needed in Python 2.7.
# For details: https://stackoverflow.com/questions/41808912/cannot-unpickle-exception-subclass
self.args = (value,)

def __str__(self):
return self.value
Expand All @@ -96,7 +99,8 @@ class ROSBagUnindexedException(ROSBagException):
"""
Exception for unindexed bags.
"""
def __init__(self):
def __init__(self, *args):
#*args needed for #1209
ROSBagException.__init__(self, 'Unindexed bag')

class ROSBagEncryptNotSupportedException(ROSBagException):
Expand Down

0 comments on commit 6e76dea

Please sign in to comment.