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

improved safety of function calls

parent 9e82ae1c
...@@ -112,9 +112,11 @@ class BakeProviderHandle(): ...@@ -112,9 +112,11 @@ class BakeProviderHandle():
region_size (int): size of the region to create. region_size (int): size of the region to create.
Returns: 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) rid = _pybakeclient.create(self._ph, bti._tid, region_size)
if(rid == None):
return None
return BakeRegionID(rid) return BakeRegionID(rid)
def write(self, rid, offset, data): def write(self, rid, offset, data):
...@@ -125,6 +127,8 @@ class BakeProviderHandle(): ...@@ -125,6 +127,8 @@ class BakeProviderHandle():
rid (BakeRegionID): region in which to write. rid (BakeRegionID): region in which to write.
offset (int): offset at which to write. offset (int): offset at which to write.
data (str): data 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) return _pybakeclient.write(self._ph, rid._rid, offset, data)
...@@ -136,6 +140,8 @@ class BakeProviderHandle(): ...@@ -136,6 +140,8 @@ class BakeProviderHandle():
rid (BakeRegionID): region in which to write. rid (BakeRegionID): region in which to write.
offset (int): offset at which to write. offset (int): offset at which to write.
data (numpy.ndarray): numpy array 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) return _pybakeclient.write_numpy(self._ph, rid._rid, offset, array)
...@@ -145,6 +151,8 @@ class BakeProviderHandle(): ...@@ -145,6 +151,8 @@ class BakeProviderHandle():
Args: Args:
rid (BakeRegionID): region to persist. rid (BakeRegionID): region to persist.
Returns:
True if the region was correctly persisted, False otherwise.
""" """
return _pybakeclient.persist(self._ph, rid._rid) return _pybakeclient.persist(self._ph, rid._rid)
...@@ -158,6 +166,8 @@ class BakeProviderHandle(): ...@@ -158,6 +166,8 @@ class BakeProviderHandle():
size (int): size of the region to create. size (int): size of the region to create.
offset (int): offset at which to write data in the region. offset (int): offset at which to write data in the region.
data (str): data to write. 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) rid = _pybakeclient.create_write_persist(self._ph, bti._tid, data)
if(rid is None): if(rid is None):
...@@ -174,6 +184,8 @@ class BakeProviderHandle(): ...@@ -174,6 +184,8 @@ class BakeProviderHandle():
size (int): size of the region to create. size (int): size of the region to create.
offset (int): offset at which to write data in the region. offset (int): offset at which to write data in the region.
array (numpy.ndarray): numpy array to write. 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) rid = _pybakeclient.create_write_persist_numpy(self._ph, bti._tid, array)
if(rid is None): if(rid is None):
...@@ -188,7 +200,7 @@ class BakeProviderHandle(): ...@@ -188,7 +200,7 @@ class BakeProviderHandle():
rid (BakeRegionID): region id. rid (BakeRegionID): region id.
Returns: 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) return _pybakeclient.get_size(self._ph, rid._rid)
...@@ -230,7 +242,7 @@ class BakeProviderHandle(): ...@@ -230,7 +242,7 @@ class BakeProviderHandle():
Returns: Returns:
A numpy array or None if it could not be read. 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): def remove(self, rid):
""" """
...@@ -239,4 +251,5 @@ class BakeProviderHandle(): ...@@ -239,4 +251,5 @@ class BakeProviderHandle():
Args: Args:
rid (BakeRegionID): region to remove. 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 ...@@ -31,22 +31,17 @@ static bake_provider_t pybake_provider_register(margo_instance_id mid, uint8_t p
else return provider; 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, bake_provider_t provider,
const std::string& target_name) { const std::string& target_name) {
bake_target_id_t target_id; bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(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); 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( static bool pybake_provider_remove_storage_target(
bake_provider_t provider, bake_provider_t provider,
bake_target_id_t target_id) bake_target_id_t target_id)
...@@ -92,8 +87,6 @@ BOOST_PYTHON_MODULE(_pybakeserver) ...@@ -92,8 +87,6 @@ BOOST_PYTHON_MODULE(_pybakeserver)
bpl::import("_pybaketarget"); bpl::import("_pybaketarget");
bpl::opaque<bake_server_context_t>(); 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("register", &pybake_provider_register, ret_policy_opaque);
bpl::def("add_storage_target", &pybake_provider_add_storage_target); bpl::def("add_storage_target", &pybake_provider_add_storage_target);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target); bpl::def("remove_storage_target", &pybake_provider_remove_storage_target);
......
...@@ -4,32 +4,69 @@ import _pybaketarget ...@@ -4,32 +4,69 @@ import _pybaketarget
import base64 import base64
class BakeTargetID(): 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): def __init__(self, tid):
"""
Constructor. Not supposed to be called by users.
"""
self._tid = tid self._tid = tid
def __str__(self): def __str__(self):
"""
Converts the BakeTargetID into a string.
"""
return str(self._tid) return str(self._tid)
@staticmethod @staticmethod
def from_str(string): 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): if(tid is None):
return None return None
else: else:
return BakeTargetID(tid) return BakeTargetID(tid)
# ================================================================== #
# ================================================================== #
# ================================================================== #
class BakeRegionID(): 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): def __init__(self, rid):
"""
Constructor. Not supposed to be called by users.
"""
self._rid = rid self._rid = rid
def __str__(self): def __str__(self):
"""
Converts the BakeRegionID into a string.
"""
return base64.b64encode(str(self._rid)) return base64.b64encode(str(self._rid))
@staticmethod @staticmethod
def from_str(string): 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): if(rid is None):
return None return None
else: 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