Skip to content

Commit 643c0c9

Browse files
committed
Fix for astropy#1942. Added pickling test class for coordinates as well
1 parent d0406ea commit 643c0c9

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎astropy/coordinates/angles.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,24 @@ def __setitem__(self, item, value):
622622
super(Longitude, self).__setitem__(item, value)
623623
self._wrap_internal()
624624

625+
def __reduce__(self):
626+
# patch to pickle Quantity objects (ndarray subclasses),
627+
# see http://www.mail-archive.com/[email protected]/msg02446.html
628+
object_state = list(np.ndarray.__reduce__(self))
629+
subclass_state = (self._unit, self.wrap_angle)
630+
object_state[2] = (object_state[2], subclass_state)
631+
return tuple(object_state)
632+
633+
def __setstate__(self, state):
634+
# patch to unpickle Quantity objects (ndarray subclasses),
635+
# see http://www.mail-archive.com/[email protected]/msg02446.html
636+
nd_state, own_state = state
637+
np.ndarray.__setstate__(self, nd_state)
638+
639+
unit, wrap_angle = own_state
640+
self._unit = unit
641+
self.wrap_angle = wrap_angle
642+
625643
def _wrap_internal(self):
626644
"""
627645
Wrap the internal values in the Longitude object. Using the `Angle`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from ...extern.six.moves import cPickle as pickle
2+
from ...coordinates import Longitude
3+
4+
def test_basic():
5+
lon1 = Longitude(1.23, "radian", wrap_angle='180d')
6+
s = pickle.dumps(lon1)
7+
lon2 = pickle.loads(s)
8+
9+
def test_pickle_longitude_wrap_angle():
10+
a = Longitude(1.23, "radian", wrap_angle='180d')
11+
s = pickle.dumps(a)
12+
b = pickle.loads(s)
13+
14+
assert a.rad == b.rad
15+
assert a.wrap_angle == b.wrap_angle

0 commit comments

Comments
 (0)