Skip to content

Commit 9fdb474

Browse files
committed
Fix py::object copied without GIL held
Identified by @rwgk. Closes #295.
1 parent f15197e commit 9fdb474

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/qpdf/mmap_inputsource-inl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@
4141
class MmapInputSource : public InputSource {
4242
public:
4343
MmapInputSource(
44-
py::object stream, const std::string &description, bool close_stream)
45-
: InputSource(), stream(stream), close_stream(close_stream)
44+
const py::object &stream, const std::string &description, bool close_stream)
45+
: InputSource(), close_stream(close_stream)
4646
{
4747
py::gil_scoped_acquire acquire;
48-
py::int_ fileno = stream.attr("fileno")();
48+
this->stream = stream;
49+
50+
py::int_ fileno = this->stream.attr("fileno")();
4951
int fd = fileno;
5052
auto mmap_module = py::module_::import("mmap");
5153
auto mmap_fn = mmap_module.attr("mmap");

src/qpdf/qpdf_inputsource-inl.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
class PythonStreamInputSource : public InputSource {
2929
public:
30-
PythonStreamInputSource(py::object stream, std::string name, bool close)
31-
: stream(stream), name(name), close(close)
30+
PythonStreamInputSource(const py::object &stream, std::string name, bool close)
31+
: name(name), close(close)
3232
{
3333
py::gil_scoped_acquire gil;
34-
if (!stream.attr("readable")().cast<bool>())
34+
this->stream = stream;
35+
if (!this->stream.attr("readable")().cast<bool>())
3536
throw py::value_error("not readable");
36-
if (!stream.attr("seekable")().cast<bool>())
37+
if (!this->stream.attr("seekable")().cast<bool>())
3738
throw py::value_error("not seekable");
3839
}
3940
virtual ~PythonStreamInputSource()

0 commit comments

Comments
 (0)