summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2008-04-06 23:11:17 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2008-04-06 23:11:17 (GMT)
commitaa63d0d4af3db832b390ac74517af5eb799540e5 (patch)
tree86818f84f404fef73ee3f56eb0945334b4d52cb8 /Doc
parentee76777846124bb5b7e08feb488d5bab949c76a5 (diff)
downloadcpython-aa63d0d4af3db832b390ac74517af5eb799540e5.zip
cpython-aa63d0d4af3db832b390ac74517af5eb799540e5.tar.gz
cpython-aa63d0d4af3db832b390ac74517af5eb799540e5.tar.bz2
Make file objects as thread safe as the underlying libc FILE* implementation.
close() will now raise an IOError if any operations on the file object are currently in progress in other threads. Most code was written by Antoine Pitrou (pitrou). Additional testing, documentation and test suite cleanup done by me (gregory.p.smith). Fixes issue 815646 and 595601 (as well as many other bugs and references to this problem dating back to the dawn of Python).
Diffstat (limited to 'Doc')
-rw-r--r--Doc/c-api/file.rst34
1 files changed, 33 insertions, 1 deletions
diff --git a/Doc/c-api/file.rst b/Doc/c-api/file.rst
index 1bb5b22..e107268 100644
--- a/Doc/c-api/file.rst
+++ b/Doc/c-api/file.rst
@@ -58,10 +58,42 @@ change in future releases of Python.
closed. Return *NULL* on failure.
-.. cfunction:: FILE* PyFile_AsFile(PyObject *p)
+.. cfunction:: FILE* PyFile_AsFile(PyObject \*p)
Return the file object associated with *p* as a :ctype:`FILE\*`.
+ If the caller will ever use the returned :ctype:`FILE\*` object while
+ the GIL is released it must also call the `PyFile_IncUseCount` and
+ `PyFile_DecUseCount` functions described below as appropriate.
+
+
+.. cfunction:: void PyFile_IncUseCount(PyFileObject \*p)
+
+ Increments the PyFileObject's internal use count to indicate
+ that the underlying :ctype:`FILE\*` is being used.
+ This prevents Python from calling f_close() on it from another thread.
+ Callers of this must call `PyFile_DecUseCount` when they are
+ finished with the :ctype:`FILE\*`. Otherwise the file object will
+ never be closed by Python.
+
+ The GIL must be held while calling this function.
+
+ The suggested use is to call this after `PyFile_AsFile` just before
+ you release the GIL.
+
+ .. versionadded:: 2.6
+
+
+.. cfunction:: void PyFile_DecUseCount(PyFileObject \*p)
+
+ Decrements the PyFileObject's internal unlocked_count member to
+ indicate that the caller is done with its own use of the :ctype:`FILE\*`.
+ This may only be called to undo a prior call to `PyFile_IncUseCount`.
+
+ The GIL must be held while calling this function.
+
+ .. versionadded:: 2.6
+
.. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n)