diff --git a/panda/src/express/virtualFile.h b/panda/src/express/virtualFile.h index 0719f3f8d1..5322fce670 100644 --- a/panda/src/express/virtualFile.h +++ b/panda/src/express/virtualFile.h @@ -56,12 +56,12 @@ PUBLISHED: BLOCKING void ls(ostream &out = cout) const; BLOCKING void ls_all(ostream &out = cout) const; - EXTENSION(BLOCKING PyObject *read_file(bool auto_unwrap) const); + EXTENSION(PyObject *read_file(bool auto_unwrap) const); BLOCKING virtual istream *open_read_file(bool auto_unwrap) const; BLOCKING virtual void close_read_file(istream *stream) const; virtual bool was_read_successful() const; - EXTENSION(BLOCKING PyObject *write_file(PyObject *data, bool auto_wrap)); + EXTENSION(PyObject *write_file(PyObject *data, bool auto_wrap)); BLOCKING virtual ostream *open_write_file(bool auto_wrap, bool truncate); BLOCKING virtual ostream *open_append_file(); BLOCKING virtual void close_write_file(ostream *stream); diff --git a/panda/src/express/virtualFileSystem.h b/panda/src/express/virtualFileSystem.h index 22d6316ce7..2ff1fa4e95 100644 --- a/panda/src/express/virtualFileSystem.h +++ b/panda/src/express/virtualFileSystem.h @@ -95,11 +95,11 @@ PUBLISHED: static VirtualFileSystem *get_global_ptr(); - EXTENSION(BLOCKING PyObject *read_file(const Filename &filename, bool auto_unwrap) const); + EXTENSION(PyObject *read_file(const Filename &filename, bool auto_unwrap) const); BLOCKING istream *open_read_file(const Filename &filename, bool auto_unwrap) const; BLOCKING static void close_read_file(istream *stream); - EXTENSION(BLOCKING PyObject *write_file(const Filename &filename, PyObject *data, bool auto_wrap)); + EXTENSION(PyObject *write_file(const Filename &filename, PyObject *data, bool auto_wrap)); BLOCKING ostream *open_write_file(const Filename &filename, bool auto_wrap, bool truncate); BLOCKING ostream *open_append_file(const Filename &filename); BLOCKING static void close_write_file(ostream *stream); diff --git a/panda/src/express/virtualFileSystem_ext.cxx b/panda/src/express/virtualFileSystem_ext.cxx index dc50751c52..6bc375c0d9 100644 --- a/panda/src/express/virtualFileSystem_ext.cxx +++ b/panda/src/express/virtualFileSystem_ext.cxx @@ -26,9 +26,22 @@ */ PyObject *Extension:: read_file(const Filename &filename, bool auto_unwrap) const { + // Release the GIL while we do this potentially slow operation. +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyThreadState *_save; + Py_UNBLOCK_THREADS +#endif + vector_uchar pv; bool okflag = _this->read_file(filename, pv, auto_unwrap); - nassertr(okflag, NULL); + +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + Py_BLOCK_THREADS +#endif + + if (!okflag) { + return PyErr_Format(PyExc_IOError, "Failed to read file: '%s'", filename.c_str()); + } #if PY_MAJOR_VERSION >= 3 if (pv.empty()) { diff --git a/panda/src/express/virtualFile_ext.cxx b/panda/src/express/virtualFile_ext.cxx index d81672cc50..db1a9a62ab 100644 --- a/panda/src/express/virtualFile_ext.cxx +++ b/panda/src/express/virtualFile_ext.cxx @@ -26,9 +26,23 @@ */ PyObject *Extension:: read_file(bool auto_unwrap) const { + // Release the GIL while we do this potentially slow operation. +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyThreadState *_save; + Py_UNBLOCK_THREADS +#endif + vector_uchar pv; bool okflag = _this->read_file(pv, auto_unwrap); - nassertr(okflag, NULL); + +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + Py_BLOCK_THREADS +#endif + + if (!okflag) { + Filename fn = _this->get_filename(); + return PyErr_Format(PyExc_IOError, "Failed to read file: '%s'", fn.c_str()); + } #if PY_MAJOR_VERSION >= 3 if (pv.empty()) { @@ -68,7 +82,18 @@ write_file(PyObject *data, bool auto_wrap) { } #endif + // Release the GIL while we do this potentially slow operation. +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyThreadState *_save; + Py_UNBLOCK_THREADS +#endif + bool result = _this->write_file((const unsigned char *)buffer, length, auto_wrap); + +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + Py_BLOCK_THREADS +#endif + return PyBool_FromLong(result); }