1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
From: Matthias Klose <doko@ubuntu.com>
Date: Wed, 9 Dec 2020 11:35:14 +0100
Subject: Fix compatibility with pybind11
---
src/qpdf/pipeline.cpp | 4 ++--
src/qpdf/qpdf_inputsource.h | 3 +--
src/qpdf/utils.cpp | 17 +++++++++++++++++
src/qpdf/utils.h | 3 +++
4 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/src/qpdf/pipeline.cpp b/src/qpdf/pipeline.cpp
index 1856987..8a4fe79 100644
--- a/src/qpdf/pipeline.cpp
+++ b/src/qpdf/pipeline.cpp
@@ -22,6 +22,7 @@
#include "pikepdf.h"
#include "pipeline.h"
+#include "utils.h"
void Pl_PythonOutput::write(unsigned char *buf, size_t len)
@@ -29,8 +30,7 @@ void Pl_PythonOutput::write(unsigned char *buf, size_t len)
py::gil_scoped_acquire gil;
ssize_t so_far = 0;
while (len > 0) {
- py::buffer_info buffer(buf, len);
- py::memoryview view_buffer(buffer);
+ py::memoryview view_buffer = memoryview_from_memory(buf, len);
py::object result = this->stream.attr("write")(view_buffer);
try {
so_far = result.cast<ssize_t>();
diff --git a/src/qpdf/qpdf_inputsource.h b/src/qpdf/qpdf_inputsource.h
index 4a96f6b..8ec6a55 100644
--- a/src/qpdf/qpdf_inputsource.h
+++ b/src/qpdf/qpdf_inputsource.h
@@ -82,8 +82,7 @@ public:
{
py::gil_scoped_acquire gil;
- py::buffer_info buffer_info(buffer, length);
- py::memoryview view_buffer_info(buffer_info);
+ py::memoryview view_buffer_info = memoryview_from_memory(buffer, length);
this->last_offset = this->tell();
py::object result = this->stream.attr("readinto")(view_buffer_info);
diff --git a/src/qpdf/utils.cpp b/src/qpdf/utils.cpp
index bf189f6..36769ca 100644
--- a/src/qpdf/utils.cpp
+++ b/src/qpdf/utils.cpp
@@ -46,3 +46,20 @@
}
#endif
+
+// Copied from pybind11 master branch (pre-2.6), can remove when we require
+// pybind11 v2.6 and replace with py::memoryview::from_memory
+py::memoryview memoryview_from_memory(void *mem, ssize_t size, bool readonly)
+{
+ PyObject* ptr = PyMemoryView_FromMemory(
+ reinterpret_cast<char*>(mem), size,
+ (readonly) ? PyBUF_READ : PyBUF_WRITE);
+ if (!ptr)
+ py::pybind11_fail("Could not allocate memoryview object!");
+ return py::reinterpret_steal<py::memoryview>(ptr);
+}
+
+py::memoryview memoryview_from_memory(const void *mem, ssize_t size)
+{
+ return memoryview_from_memory(const_cast<void*>(mem), size, true);
+}
diff --git a/src/qpdf/utils.h b/src/qpdf/utils.h
index 9f6a411..f26c5bd 100644
--- a/src/qpdf/utils.h
+++ b/src/qpdf/utils.h
@@ -17,3 +17,6 @@ inline bool str_startswith(T haystack, S needle)
{
return std::string(haystack).rfind(needle, 0) == 0;
}
+
+py::memoryview memoryview_from_memory(void *mem, ssize_t size, bool readonly = false);
+py::memoryview memoryview_from_memory(const void *mem, ssize_t size);
|