Message113895
mmap, buffer, bytearray, string and unicode objects set the char buffer callback (bf_getcharbuffer). The bytearray object sets also the release buffer callback (bf_releasebuffer).
In Python 2.7, PyObject_AsCharBuffer() accepts bytearray objects, whereas the "t#" format of PyArg_Parse functions rejects byte bytearray objects (expect an "pinned buffer object").
In Python 3.2, PyObject_AsCharBuffer() releases the buffer.
PyObject_AsCharBuffer() documentation (in 2.7 and 3.2) says that the function only accepts read-only objects.
Something is wrong here. If the caller doesn't hold a kind of lock, the object cannot be protected against futher modifications. The caller has to ensure that the object is not modifiable until it finishs to use the char* pointer.
I think that it would be safer to respect the documentation: PyObject_AsCharBuffer() should only accept read-only objects. The most important change is that functions using PyObject_AsCharBuffer() will not accept bytearray objects anymore.
Attached patch (for Python 2.7) changes PyObject_AsCharBuffer() to reject modifiable objects. It removes also the character buffer callback from the bytearray type. To avoid breaking compatibility too much, I patched int(), long() and float() to still support bytearray objects.
Examples of functions rejecting bytearray with the patch:
- int(), long(), float(), complex()
- many str methods: split, partition, rpartition, rsplit, index, find, count, translate, replace, startswith, endswith
- writelines() of file objects (eg. sys.stdout.writelines)
- writelines() method of a bz2 file
--
My patch breaks backward compatibility, and I don't know that it is acceptable in Python 2.7.
I will write a similar patch for Python 3.2. |
|
Date |
User |
Action |
Args |
2010-08-14 13:42:33 | vstinner | set | recipients:
+ vstinner, pitrou |
2010-08-14 13:42:32 | vstinner | set | messageid: <1281793352.06.0.62940034279.issue9602@psf.upfronthosting.co.za> |
2010-08-14 13:42:29 | vstinner | link | issue9602 messages |
2010-08-14 13:42:28 | vstinner | create | |
|