MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / dumpPropertyContent

Method dumpPropertyContent

src/App/PropertyContainerPyImp.cpp:522–585  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

520
521
522PyObject* PropertyContainerPy::dumpPropertyContent(PyObject* args, PyObject* kwds) const
523{
524 int compression = 3;
525 const char* property {};
526 static const std::array<const char*, 3> kwds_def {"Property", "Compression", nullptr};
527 PyErr_Clear();
528 if (!Base::Wrapped_ParseTupleAndKeywords(args,
529 kwds,
530 "s|i",
531 kwds_def,
532 &property,
533 &compression)) {
534 return nullptr;
535 }
536
537 Property* prop = getPropertyContainerPtr()->getPropertyByName(property);
538 if (!prop) {
539 PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", property);
540 return nullptr;
541 }
542
543 // setup the stream. the in flag is needed to make "read" work
544 std::stringstream stream(std::stringstream::out | std::stringstream::in
545 | std::stringstream::binary);
546 try {
547 prop->dumpToStream(stream, compression);
548 }
549 catch (...) {
550 PyErr_SetString(PyExc_IOError, "Unable to parse content into binary representation");
551 return nullptr;
552 }
553
554 // build the byte array with correct size
555 if (!stream.seekp(0, std::stringstream::end)) {
556 PyErr_SetString(PyExc_IOError, "Unable to find end of stream");
557 return nullptr;
558 }
559
560 std::stringstream::pos_type offset = stream.tellp();
561 if (!stream.seekg(0, std::stringstream::beg)) {
562 PyErr_SetString(PyExc_IOError, "Unable to find begin of stream");
563 return nullptr;
564 }
565
566 PyObject* ba = PyByteArray_FromStringAndSize(nullptr, offset);
567
568 // use the buffer protocol to access the underlying array and write into it
569 Py_buffer buf = Py_buffer();
570 PyObject_GetBuffer(ba, &buf, PyBUF_WRITABLE);
571 try {
572 if (!stream.read((char*)buf.buf, offset)) {
573 PyErr_SetString(PyExc_IOError, "Error copying data into byte array");
574 return nullptr;
575 }
576 PyBuffer_Release(&buf);
577 }
578 catch (...) {
579 PyBuffer_Release(&buf);

Callers 1

Calls 4

dumpToStreamMethod · 0.80
getPropertyByNameMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected