Browse Source

Merge branch 'PYADPT-116 - Blob is missing .data accessor' into Release_2.1

pull/5/head 2.1.7
heck 4 years ago
parent
commit
3c75e518fc
  1. 42
      src/pEp/_pEp/message.cc
  2. 6
      src/pEp/_pEp/message.hh
  3. 5
      src/pEp/_pEp/pEpmodule.cc
  4. 21
      tests/test_blob.py

42
src/pEp/_pEp/message.cc

@ -34,24 +34,8 @@ namespace pEp {
if (!_bl)
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->filename(filename);
}
@ -110,6 +94,30 @@ namespace pEp {
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) {
if (encoding == "") {
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; }
object data();
void data(object data);
string decode(string encoding);
string decode() { return decode(""); }
@ -191,4 +195,4 @@ namespace pEp {
} /* namespace PythonAdapter */
} /* namespace pEp */
#endif /* MESSAGE_HH */
#endif /* MESSAGE_HH */

5
src/pEp/_pEp/pEpmodule.cc

@ -399,7 +399,10 @@ namespace pEp {
"MIME type of object in Blob")
.add_property("filename", (string(Message::Blob::*)()) &Message::Blob::filename,
(void(Message::Blob::*)(string)) &Message::Blob::filename,
"filename of object in Blob");
"filename of object in Blob")
.add_property("data", (object(Message::Blob::*)()) &Message::Blob::data,
(void(Message::Blob::*)(object)) &Message::Blob::data,
"data of object in Blob");
((PyTypeObject *)(void *)blob_class.ptr())->tp_as_buffer = &Message::Blob::bp;

21
tests/test_blob.py

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# This file is under GNU Affero General Public License 3.0
# see LICENSE.txt
"""Blob unit tests."""
import pytest
from . import constants
from . import model
def test_blob_data_constructor(pEp, model):
bdata = bytes('this is test data', 'utf-8')
b = pEp.Blob(bdata)
assert(b.data == bdata)
def test_blob_data_property(pEp, model):
b = pEp.Blob(b'dummy')
bdata = bytes('this is test data', 'utf-8')
b.data = bdata
assert(b.data == bdata)
Loading…
Cancel
Save