Skip to content

Commit 784bfa9

Browse files
author
martin.v.loewis
committed
Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
by denying s# to parse objects that have a releasebuffer procedure, and introducing s*. More module might need to get converted to use s*. git-svn-id: http://svn.python.org/projects/python/trunk@65654 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 182dd6a commit 784bfa9

File tree

16 files changed

+459
-272
lines changed

16 files changed

+459
-272
lines changed

Doc/c-api/arg.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,25 @@ variable(s) whose address should be passed.
4040
other read-buffer compatible objects pass back a reference to the raw internal
4141
data representation.
4242

43+
``s*`` (string, Unicode, or any buffer compatible object) [Py_buffer \*]
44+
Similar to ``s#``, this code fills a Py_buffer structure provided by the caller.
45+
The buffer gets locked, so that the caller can subsequently use the buffer even
46+
inside a ``Py_BEGIN_ALLOW_THREADS`` block; the caller is responsible for calling
47+
``PyBuffer_Release`` with the structure after it has processed the data.
48+
49+
.. versionadded:: 2.6
50+
4351
``z`` (string or ``None``) [const char \*]
4452
Like ``s``, but the Python object may also be ``None``, in which case the C
4553
pointer is set to *NULL*.
4654

4755
``z#`` (string or ``None`` or any read buffer compatible object) [const char \*, int]
4856
This is to ``s#`` as ``z`` is to ``s``.
4957

58+
``z*`` (string or ``None`` or any buffer compatible object) [Py_buffer*]
59+
This is to ``s*`` as ``z`` is to ``s``.
60+
.. versionadded:: 2.6
61+
5062
``u`` (Unicode object) [Py_UNICODE \*]
5163
Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of
5264
16-bit Unicode (UTF-16) data. As with ``s``, there is no need to provide
@@ -240,6 +252,10 @@ variable(s) whose address should be passed.
240252
single-segment buffer objects are accepted; :exc:`TypeError` is raised for all
241253
others.
242254

255+
``w*`` (read-write byte-oriented buffer) [Py_buffer \*]
256+
This is to ``w`` what ``s*`` is to ``s``.
257+
.. versionadded:: 2.6
258+
243259
``(items)`` (tuple) [*matching-items*]
244260
The object must be a Python sequence whose length is the number of format units
245261
in *items*. The C arguments must correspond to the individual format units in

Doc/whatsnew/2.6.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ about the object's memory representation. Objects
10641064
can use this operation to lock memory in place
10651065
while an external caller could be modifying the contents,
10661066
so there's a corresponding
1067-
``PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)`` to
1067+
``PyBuffer_Release(Py_buffer *view)`` to
10681068
indicate that the external caller is done.
10691069

10701070
The **flags** argument to :cfunc:`PyObject_GetBuffer` specifies
@@ -2841,7 +2841,7 @@ Changes to Python's build process and to the C API include:
28412841

28422842
* The new buffer interface, previously described in
28432843
`the PEP 3118 section <#pep-3118-revised-buffer-protocol>`__,
2844-
adds :cfunc:`PyObject_GetBuffer` and :cfunc:`PyObject_ReleaseBuffer`,
2844+
adds :cfunc:`PyObject_GetBuffer` and :cfunc:`PyBuffer_Release`,
28452845
as well as a few other functions.
28462846

28472847
* Python's use of the C stdio library is now thread-safe, or at least

Include/abstract.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -549,24 +549,6 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
549549
*/
550550

551551

552-
PyAPI_FUNC(void) PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view);
553-
554-
555-
/* C-API version of the releasebuffer function call. It
556-
checks to make sure the object has the required function
557-
pointer and issues the call. The obj must have the buffer
558-
interface or this function will cause a segfault (i.e. it
559-
is assumed to be called only after a corresponding
560-
getbuffer which already verified the existence of the
561-
tp_as_buffer pointer).
562-
563-
Returns 0 on success and -1 (with an error raised) on
564-
failure. This function always succeeds (as a NO-OP) if
565-
there is no releasebuffer function for the object so that
566-
it can always be called when the consumer is done with the
567-
buffer
568-
*/
569-
570552
PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
571553

572554
/* Get the memory area pointed to by the indices for the buffer given.
@@ -623,7 +605,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
623605
per element.
624606
*/
625607

626-
PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, void *buf,
608+
PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
627609
Py_ssize_t len, int readonly,
628610
int flags);
629611

@@ -633,6 +615,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
633615
and -1 (with raising an error) on error.
634616
*/
635617

618+
PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
619+
620+
/* Releases a Py_buffer obtained from getbuffer ParseTuple's s*.
621+
*/
622+
636623
PyAPI_FUNC(PyObject *) PyObject_Format(PyObject* obj,
637624
PyObject *format_spec);
638625
/*

Include/object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
162162
/* Py3k buffer interface */
163163

164164
typedef struct bufferinfo {
165-
void *buf;
165+
void *buf;
166+
PyObject *obj; /* borrowed reference */
166167
Py_ssize_t len;
167168
Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
168169
pointed to by strides in simple case.*/

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.6 beta 3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
16+
by denying s# to parse objects that have a releasebuffer procedure,
17+
and introducing s*.
18+
1519
- Issue #3537: Fix an assertion failure when an empty but presized dict
1620
object was stored in the freelist.
1721

0 commit comments

Comments
 (0)