Browse Source

PYADPT-116: refactor blob.data as setter/getter (getter added)

PYADPT-116
heck 4 years ago
parent
commit
bc645f5da2
  1. 42
      src/pEp/_pEp/message.cc
  2. 6
      src/pEp/_pEp/message.hh

42
src/pEp/_pEp/message.cc

@ -34,24 +34,8 @@ namespace pEp {
if (!_bl) if (!_bl)
throw bad_alloc(); throw bad_alloc();
Py_buffer src;
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO);
if (result)
throw invalid_argument("need a contiguous buffer to read");
char *mem = (char *) malloc(src.len);
if (!mem) {
PyBuffer_Release(&src);
throw bad_alloc();
}
memcpy(mem, src.buf, src.len);
free(_bl->value);
_bl->size = src.len;
_bl->value = mem;
PyBuffer_Release(&src);
this->data(data);
this->mime_type(mime_type); this->mime_type(mime_type);
this->filename(filename); this->filename(filename);
} }
@ -110,6 +94,30 @@ namespace pEp {
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags);
} }
object Message::Blob::data() {
return object(handle<>(PyBytes_FromString(_bl->value)));
}
void Message::Blob::data(object data) {
Py_buffer src;
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO);
if (result)
throw invalid_argument("need a contiguous buffer to read");
char *mem = (char *) malloc(src.len);
if (!mem) {
PyBuffer_Release(&src);
throw bad_alloc();
}
memcpy(mem, src.buf, src.len);
free(_bl->value);
_bl->size = src.len;
_bl->value = mem;
PyBuffer_Release(&src);
}
string Message::Blob::decode(string encoding) { string Message::Blob::decode(string encoding) {
if (encoding == "") { if (encoding == "") {
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; string _mime_type = _bl->mime_type ? _bl->mime_type : "";

6
src/pEp/_pEp/message.hh

@ -59,6 +59,10 @@ namespace pEp {
size_t size() { return _bl->size; } size_t size() { return _bl->size; }
object data();
void data(object data);
string decode(string encoding); string decode(string encoding);
string decode() { return decode(""); } string decode() { return decode(""); }
@ -191,4 +195,4 @@ namespace pEp {
} /* namespace PythonAdapter */ } /* namespace PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */
#endif /* MESSAGE_HH */ #endif /* MESSAGE_HH */

Loading…
Cancel
Save