Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,17 @@ def test_timedelta_array_str(self):

def test_pickle(self):
# Check that pickle roundtripping works
dt = np.dtype('M8[7D]')
assert_equal(pickle.loads(pickle.dumps(dt)), dt)
dt = np.dtype('M8[W]')
assert_equal(pickle.loads(pickle.dumps(dt)), dt)
scalar = np.datetime64('2016-01-01T00:00:00.000000000')
assert_equal(pickle.loads(pickle.dumps(scalar)), scalar)
delta = scalar - np.datetime64('2015-01-01T00:00:00.000000000')
assert_equal(pickle.loads(pickle.dumps(delta)), delta)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
dt = np.dtype('M8[7D]')
assert_equal(pickle.loads(pickle.dumps(dt, protocol=proto)), dt)
dt = np.dtype('M8[W]')
assert_equal(pickle.loads(pickle.dumps(dt, protocol=proto)), dt)
scalar = np.datetime64('2016-01-01T00:00:00.000000000')
assert_equal(pickle.loads(pickle.dumps(scalar, protocol=proto)),
scalar)
delta = scalar - np.datetime64('2015-01-01T00:00:00.000000000')
assert_equal(pickle.loads(pickle.dumps(delta, protocol=proto)),
delta)

# Check that loading pickles from 1.6 works
pkl = b"cnumpy\ndtype\np0\n(S'M8'\np1\nI0\nI1\ntp2\nRp3\n" + \
Expand Down
32 changes: 18 additions & 14 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,12 +1372,13 @@ def test_view(self):

def test_pickle(self):
import pickle
for dt in [bytes, np.void, unicode]:
zs = self._zeros(10, dt)
p = pickle.dumps(zs)
zs2 = pickle.loads(p)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
for dt in [bytes, np.void, unicode]:
zs = self._zeros(10, dt)
p = pickle.dumps(zs, protocol=proto)
zs2 = pickle.loads(p)

assert_equal(zs.dtype, zs2.dtype)
assert_equal(zs.dtype, zs2.dtype)


class TestMethods(object):
Expand Down Expand Up @@ -3550,16 +3551,19 @@ def test_test_zero_rank(self):
class TestPickling(object):
def test_roundtrip(self):
import pickle
carray = np.array([[2, 9], [7, 0], [3, 8]])
DATA = [
carray,
np.transpose(carray),
np.array([('xxx', 1, 2.0)], dtype=[('a', (str, 3)), ('b', int),
('c', float)])
]
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
carray = np.array([[2, 9], [7, 0], [3, 8]])
DATA = [
carray,
np.transpose(carray),
np.array([('xxx', 1, 2.0)], dtype=[('a', (str, 3)), ('b', int),
('c', float)])
]

for a in DATA:
assert_equal(a, pickle.loads(a.dumps()), err_msg="%r" % a)
for a in DATA:
assert_equal(
a, pickle.loads(pickle.dumps(a, protocol=proto)),
err_msg="%r" % a)

def _loads(self, obj):
import pickle
Expand Down
23 changes: 14 additions & 9 deletions numpy/core/tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,22 +378,27 @@ def test_out_of_order_fields(self):
def test_pickle_1(self):
# Issue #1529
a = np.array([(1, [])], dtype=[('a', np.int32), ('b', np.int32, 0)])
assert_equal(a, pickle.loads(pickle.dumps(a)))
assert_equal(a[0], pickle.loads(pickle.dumps(a[0])))
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
assert_equal(a, pickle.loads(pickle.dumps(a, protocol=proto)))
assert_equal(a[0], pickle.loads(pickle.dumps(a[0],
protocol=proto)))

def test_pickle_2(self):
a = self.data
assert_equal(a, pickle.loads(pickle.dumps(a)))
assert_equal(a[0], pickle.loads(pickle.dumps(a[0])))
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
assert_equal(a, pickle.loads(pickle.dumps(a, protocol=proto)))
assert_equal(a[0], pickle.loads(pickle.dumps(a[0],
protocol=proto)))

def test_pickle_3(self):
# Issue #7140
a = self.data
pa = pickle.loads(pickle.dumps(a[0]))
assert_(pa.flags.c_contiguous)
assert_(pa.flags.f_contiguous)
assert_(pa.flags.writeable)
assert_(pa.flags.aligned)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
pa = pickle.loads(pickle.dumps(a[0], protocol=proto))
assert_(pa.flags.c_contiguous)
assert_(pa.flags.f_contiguous)
assert_(pa.flags.writeable)
assert_(pa.flags.aligned)

def test_objview_record(self):
# https://github.com/numpy/numpy/issues/2599
Expand Down
44 changes: 25 additions & 19 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,9 @@ def test_unicode_scalar(self):
# Ticket #600
x = np.array(["DROND", "DROND1"], dtype="U6")
el = x[1]
new = pickle.loads(pickle.dumps(el))
assert_equal(new, el)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
new = pickle.loads(pickle.dumps(el, protocol=proto))
assert_equal(new, el)

def test_arange_non_native_dtype(self):
# Ticket #616
Expand Down Expand Up @@ -1066,11 +1067,12 @@ def test_nonnative_endian_fill(self):
def test_dot_alignment_sse2(self):
# Test for ticket #551, changeset r5140
x = np.zeros((30, 40))
y = pickle.loads(pickle.dumps(x))
# y is now typically not aligned on a 8-byte boundary
z = np.ones((1, y.shape[0]))
# This shouldn't cause a segmentation fault:
np.dot(z, y)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
y = pickle.loads(pickle.dumps(x, protocol=proto))
# y is now typically not aligned on a 8-byte boundary
z = np.ones((1, y.shape[0]))
# This shouldn't cause a segmentation fault:
np.dot(z, y)

def test_astype_copy(self):
# Ticket #788, changeset r5155
Expand Down Expand Up @@ -1280,9 +1282,12 @@ def test_void_scalar_constructor(self):

assert_(test_record_void_scalar == test_record)

#Test pickle and unpickle of void and record scalars
assert_(pickle.loads(pickle.dumps(test_string)) == test_string)
assert_(pickle.loads(pickle.dumps(test_record)) == test_record)
# Test pickle and unpickle of void and record scalars
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
assert_(pickle.loads(
pickle.dumps(test_string, protocol=proto)) == test_string)
assert_(pickle.loads(
pickle.dumps(test_record, protocol=proto)) == test_record)

def test_blasdot_uninitialized_memory(self):
# Ticket #950
Expand Down Expand Up @@ -1925,11 +1930,12 @@ def test_pickle_string_overwrite(self):

def test_pickle_bytes_overwrite(self):
if sys.version_info[0] >= 3:
data = np.array([1], dtype='b')
data = pickle.loads(pickle.dumps(data))
data[0] = 0xdd
bytestring = "\x01 ".encode('ascii')
assert_equal(bytestring[0:1], '\x01'.encode('ascii'))
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
data = np.array([1], dtype='b')
data = pickle.loads(pickle.dumps(data, protocol=proto))
data[0] = 0xdd
bytestring = "\x01 ".encode('ascii')
assert_equal(bytestring[0:1], '\x01'.encode('ascii'))

def test_pickle_py2_array_latin1_hack(self):
# Check that unpickling hacks in Py3 that support
Expand Down Expand Up @@ -2231,10 +2237,10 @@ def __eq__(self, other):

def test_pickle_empty_string(self):
# gh-3926

import pickle
test_string = np.string_('')
assert_equal(pickle.loads(pickle.dumps(test_string)), test_string)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
test_string = np.string_('')
assert_equal(pickle.loads(
pickle.dumps(test_string, protocol=proto)), test_string)

def test_frompyfunc_many_args(self):
# gh-5672
Expand Down
9 changes: 5 additions & 4 deletions numpy/matrixlib/tests/test_masked_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ def test_matrix_indexing(self):
def test_pickling_subbaseclass(self):
# Test pickling w/ a subclass of ndarray
a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
a_pickled = pickle.loads(a.dumps())
assert_equal(a_pickled._mask, a._mask)
assert_equal(a_pickled, a)
assert_(isinstance(a_pickled._data, np.matrix))
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
assert_equal(a_pickled._mask, a._mask)
assert_equal(a_pickled, a)
assert_(isinstance(a_pickled._data, np.matrix))

def test_count_mean_with_matrix(self):
m = masked_array(np.matrix([[1, 2], [3, 4]]), mask=np.zeros((2, 2)))
Expand Down