Commit 45f06aa9 authored by Matthieu Dorier's avatar Matthieu Dorier

improved safety of function calls

parent 9e82ae1c
......@@ -112,9 +112,11 @@ class BakeProviderHandle():
region_size (int): size of the region to create.
Returns:
A BakeRegionID object representing the region.
A BakeRegionID object representing the region. None if an error occured.
"""
rid = _pybakeclient.create(self._ph, bti._tid, region_size)
if(rid == None):
return None
return BakeRegionID(rid)
def write(self, rid, offset, data):
......@@ -125,6 +127,8 @@ class BakeProviderHandle():
rid (BakeRegionID): region in which to write.
offset (int): offset at which to write.
data (str): data to write.
Returns:
True if the data was correctly written, False otherwise.
"""
return _pybakeclient.write(self._ph, rid._rid, offset, data)
......@@ -136,6 +140,8 @@ class BakeProviderHandle():
rid (BakeRegionID): region in which to write.
offset (int): offset at which to write.
data (numpy.ndarray): numpy array to write.
Returns:
True if the data was correctly written, False otherwise.
"""
return _pybakeclient.write_numpy(self._ph, rid._rid, offset, array)
......@@ -145,6 +151,8 @@ class BakeProviderHandle():
Args:
rid (BakeRegionID): region to persist.
Returns:
True if the region was correctly persisted, False otherwise.
"""
return _pybakeclient.persist(self._ph, rid._rid)
......@@ -158,6 +166,8 @@ class BakeProviderHandle():
size (int): size of the region to create.
offset (int): offset at which to write data in the region.
data (str): data to write.
Returns:
True if the region was correctly written and persisted, False otherwise.
"""
rid = _pybakeclient.create_write_persist(self._ph, bti._tid, data)
if(rid is None):
......@@ -174,6 +184,8 @@ class BakeProviderHandle():
size (int): size of the region to create.
offset (int): offset at which to write data in the region.
array (numpy.ndarray): numpy array to write.
Returns:
True if the region was correctly written and persisted, False otherwise.
"""
rid = _pybakeclient.create_write_persist_numpy(self._ph, bti._tid, array)
if(rid is None):
......@@ -188,7 +200,7 @@ class BakeProviderHandle():
rid (BakeRegionID): region id.
Returns:
The size (ind) of the provided region.
The size (int) of the provided region. None in case of error.
"""
return _pybakeclient.get_size(self._ph, rid._rid)
......@@ -230,7 +242,7 @@ class BakeProviderHandle():
Returns:
A numpy array or None if it could not be read.
"""
return _pybakeclient.read_numpy(self._ph, rid._rid, offset, shape, dtype)
return _pybakeclient.read_numpy(self._ph, rid._rid, offset, tuple(shape), dtype)
def remove(self, rid):
"""
......@@ -239,4 +251,5 @@ class BakeProviderHandle():
Args:
rid (BakeRegionID): region to remove.
"""
_pybakeclient.remove(self._ph, rid._rid)
ret = _pybakeclient.remove(self._ph, rid._rid)
return (ret == 0)
......@@ -31,22 +31,17 @@ static bake_provider_t pybake_provider_register(margo_instance_id mid, uint8_t p
else return provider;
}
static bake_target_id_t pybake_provider_add_storage_target(
static bpl::object pybake_provider_add_storage_target(
bake_provider_t provider,
const std::string& target_name) {
bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(target_id));
bake_provider_add_storage_target(
int ret = bake_provider_add_storage_target(
provider, target_name.c_str(), &target_id);
return target_id;
if(ret != 0) return bpl::object();
return bpl::object(target_id);
}
#if 0
static std::string pybake_target_id_to_string(bake_target_id_t tid) {
char id[37];
uuid_unparse(tid.id, id);
return std::string(id);
}
#endif
static bool pybake_provider_remove_storage_target(
bake_provider_t provider,
bake_target_id_t target_id)
......@@ -92,8 +87,6 @@ BOOST_PYTHON_MODULE(_pybakeserver)
bpl::import("_pybaketarget");
bpl::opaque<bake_server_context_t>();
// bpl::class_<bake_target_id_t>("bake_target_id", bpl::no_init)
// .def("__str__", pybake_target_id_to_string);
bpl::def("register", &pybake_provider_register, ret_policy_opaque);
bpl::def("add_storage_target", &pybake_provider_add_storage_target);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target);
......
......@@ -4,32 +4,69 @@ import _pybaketarget
import base64
class BakeTargetID():
"""
The BakeTargetID class is a wrapper for a bake_target_id_t
object at C level. It can be used by client and server
interfaces. It can be converted into a string using the
__str__ method, and be deserialized from a string using
the BakeTargetID.from_str static method.
"""
def __init__(self, tid):
"""
Constructor. Not supposed to be called by users.
"""
self._tid = tid
def __str__(self):
"""
Converts the BakeTargetID into a string.
"""
return str(self._tid)
@staticmethod
def from_str(string):
tid = _pybaketarget.target_id_from_string(string)
"""
Converts a string representation of the BakeTargetID into
a concrete BakeTargetID object.
"""
tid = _pybaketarget.target_id_from_string(str(string))
if(tid is None):
return None
else:
return BakeTargetID(tid)
# ================================================================== #
# ================================================================== #
# ================================================================== #
class BakeRegionID():
"""
The BakeRegionID is a wrapper for a bake_region_id_t object at
C level. It provides functionalities to convert a BakeRegionID
from/to a string representation, and can be used by the server
and client API.
"""
def __init__(self, rid):
"""
Constructor. Not supposed to be called by users.
"""
self._rid = rid
def __str__(self):
"""
Converts the BakeRegionID into a string.
"""
return base64.b64encode(str(self._rid))
@staticmethod
def from_str(string):
rid = _pybaketarget.region_id_from_string(base64.b64decode(string))
"""
Converts a string representing a BakeRegionID into a concrete
BakeRegionID object.
"""
rid = _pybaketarget.region_id_from_string(base64.b64decode(str(string)))
if(rid is None):
return None
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment