def test_bad_file_mode(self): with open(os.devnull) as f: with self.assertRaises(OSError): _kastore.dump({}, f) with open(os.devnull, "w") as f: with self.assertRaises(OSError): _kastore.load(f)
def test_fileno_round_trip(self): with tempfile.TemporaryFile() as f: data = {"a": np.arange(10)} _kastore.dump(data, f.fileno()) f.seek(0) x = _kastore.load(f.fileno()) self.assertDataEqual(x, data)
def test_same_file_round_trip(self): with tempfile.TemporaryFile() as f: for j in range(10): start = f.tell() data = {"a": np.arange(j * 100)} _kastore.dump(data, f) f.seek(start) x = _kastore.load(f) self.assertDataEqual(x, data)
def test_bad_fd(self): bad_fd = 10000 with self.assertRaises(OSError): _kastore.dump({}, bad_fd) with self.assertRaises(OSError): _kastore.load(bad_fd) bad_fd = -1 with self.assertRaises(ValueError): _kastore.dump({}, bad_fd) with self.assertRaises(ValueError): _kastore.load(bad_fd)
def dump(data, filename, key_encoding="utf-8", engine=PY_ENGINE): """ Dumps a store to the specified file. :param str filename: The path of the file to write the store to. :param str key_encoding: The encoding to use when converting the keys to raw bytes. :param str engine: The underlying implementation to use. """ if engine == PY_ENGINE: store.dump(data, filename, key_encoding) elif engine == C_ENGINE: _check_low_level_module() _kastore.dump(data, filename) else: _raise_unknown_engine()
def test_bad_dtype(self): with open(os.devnull, "w") as f: with self.assertRaises(ValueError): # complex number array = np.array([0], dtype="c16") _kastore.dump({"a": array}, f)
def test_bad_numeric_fd(self): for fd in ["1", 1.0, 2.0]: with self.assertRaises(TypeError): _kastore.dump({}, fd) with self.assertRaises(TypeError): _kastore.load(fd)