Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddacf96

Browse files
committed
Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
1 parent d417a15 commit ddacf96

5 files changed

Lines changed: 20 additions & 23 deletions

File tree

Include/object.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,17 @@ typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
160160

161161
/* Flags for getting buffers */
162162
#define PyBUF_SIMPLE 0
163-
#define PyBUF_CHARACTER 1
164-
#define PyBUF_WRITABLE 0x0002
163+
#define PyBUF_WRITABLE 0x0001
165164
/* we used to include an E, backwards compatible alias */
166165
#define PyBUF_WRITEABLE PyBUF_WRITABLE
167-
#define PyBUF_LOCK 0x0004
168-
#define PyBUF_FORMAT 0x0008
169-
#define PyBUF_ND 0x0010
170-
#define PyBUF_STRIDES (0x0020 | PyBUF_ND)
171-
#define PyBUF_C_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
172-
#define PyBUF_F_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
173-
#define PyBUF_ANY_CONTIGUOUS (0x0100 | PyBUF_STRIDES)
174-
#define PyBUF_INDIRECT (0x0200 | PyBUF_STRIDES)
166+
#define PyBUF_LOCK 0x0002
167+
#define PyBUF_FORMAT 0x0004
168+
#define PyBUF_ND 0x0008
169+
#define PyBUF_STRIDES (0x0010 | PyBUF_ND)
170+
#define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
171+
#define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
172+
#define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
173+
#define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
175174

176175
#define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
177176
#define PyBUF_CONTIG_RO (PyBUF_ND)

Modules/arraymodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
385385
static struct arraydescr descriptors[] = {
386386
{'b', 1, b_getitem, b_setitem, "b"},
387387
{'B', 1, BB_getitem, BB_setitem, "B"},
388-
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "U"},
388+
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
389389
{'h', sizeof(short), h_getitem, h_setitem, "h"},
390390
{'H', sizeof(short), HH_getitem, HH_setitem, "H"},
391391
{'i', sizeof(int), i_getitem, i_setitem, "i"},
@@ -1784,11 +1784,6 @@ static const void *emptybuf = "";
17841784
static int
17851785
array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
17861786
{
1787-
if ((flags & PyBUF_CHARACTER)) {
1788-
PyErr_SetString(PyExc_TypeError,
1789-
"Cannot be a character buffer");
1790-
return -1;
1791-
}
17921787
if ((flags & PyBUF_LOCK)) {
17931788
PyErr_SetString(PyExc_BufferError,
17941789
"Cannot lock data");
@@ -1815,6 +1810,11 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
18151810
view->internal = NULL;
18161811
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
18171812
view->format = self->ob_descr->formats;
1813+
#ifdef Py_UNICODE_WIDE
1814+
if (self->ob_descr->typecode == 'u') {
1815+
view->formats = "w";
1816+
}
1817+
#endif
18181818
}
18191819

18201820
finish:

Objects/abstract.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ PyObject_AsCharBuffer(PyObject *obj,
236236
"expected an object with the buffer interface");
237237
return -1;
238238
}
239-
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_CHARACTER)) return -1;
239+
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
240240

241241
*buffer = view.buf;
242242
*buffer_len = view.len;

Objects/unicodeobject.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8117,10 +8117,6 @@ static int
81178117
unicode_buffer_getbuffer(PyUnicodeObject *self, Py_buffer *view, int flags)
81188118
{
81198119

8120-
if (flags & PyBUF_CHARACTER) {
8121-
PyErr_SetString(PyExc_SystemError, "can't use str as char buffer");
8122-
return -1;
8123-
}
81248120
return PyBuffer_FillInfo(view, (void *)self->str,
81258121
PyUnicode_GET_DATA_SIZE(self), 1, flags);
81268122
}

Python/getargs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,9 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12371237
(*pb->bf_releasebuffer)(arg, &view);
12381238
break;
12391239
}
1240-
1240+
1241+
/*TEO: This can be eliminated --- here only for backward
1242+
compatibility */
12411243
case 't': { /* 8-bit character buffer, read-only access */
12421244
char **p = va_arg(*p_va, char **);
12431245
PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
@@ -1253,7 +1255,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12531255
"string or read-only character buffer",
12541256
arg, msgbuf, bufsize);
12551257

1256-
if ((*pb->bf_getbuffer)(arg, &view, PyBUF_CHARACTER) != 0)
1258+
if ((*pb->bf_getbuffer)(arg, &view, PyBUF_SIMPLE) != 0)
12571259
return converterr("string or single-segment read-only buffer",
12581260
arg, msgbuf, bufsize);
12591261

0 commit comments

Comments
 (0)