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

Skip to content

Commit 70a2371

Browse files
committed
Remove the buffer API from PyUnicode as specified by PEP 3137. Also,
fix the error message of the 't' format unit, in getargs.c, so that it asks for bytes, instead of string.
1 parent 659e7f4 commit 70a2371

4 files changed

Lines changed: 13 additions & 17 deletions

File tree

Modules/_sre.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,15 @@ getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
16741674
void* ptr;
16751675
Py_buffer view;
16761676

1677+
/* Unicode objects do not support the buffer API. So, get the data
1678+
directly instead. */
1679+
if (PyUnicode_Check(string)) {
1680+
ptr = (void *)PyUnicode_AS_DATA(string);
1681+
*p_length = PyUnicode_GET_SIZE(string);
1682+
*p_charsize = sizeof(Py_UNICODE);
1683+
return ptr;
1684+
}
1685+
16771686
/* get pointer to string buffer */
16781687
view.len = -1;
16791688
buffer = Py_Type(string)->tp_as_buffer;

Modules/posixmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,8 @@ posix_listdir(PyObject *self, PyObject *args)
21352135
FILEFINDBUF3 ep;
21362136
APIRET rc;
21372137

2138-
if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
2138+
if (!PyArg_ParseTuple(args, "et#:listdir",
2139+
Py_FileSystemDefaultEncoding, &name, &len))
21392140
return NULL;
21402141
if (len >= MAX_PATH) {
21412142
PyErr_SetString(PyExc_ValueError, "path too long");

Objects/unicodeobject.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8113,15 +8113,6 @@ static PyMappingMethods unicode_as_mapping = {
81138113
};
81148114

81158115

8116-
static int
8117-
unicode_buffer_getbuffer(PyUnicodeObject *self, Py_buffer *view, int flags)
8118-
{
8119-
8120-
return PyBuffer_FillInfo(view, (void *)self->str,
8121-
PyUnicode_GET_DATA_SIZE(self), 1, flags);
8122-
}
8123-
8124-
81258116
/* Helpers for PyUnicode_Format() */
81268117

81278118
static PyObject *
@@ -8815,11 +8806,6 @@ PyObject *PyUnicode_Format(PyObject *format,
88158806
return NULL;
88168807
}
88178808

8818-
static PyBufferProcs unicode_as_buffer = {
8819-
(getbufferproc) unicode_buffer_getbuffer,
8820-
NULL,
8821-
};
8822-
88238809
static PyObject *
88248810
unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
88258811

@@ -8903,7 +8889,7 @@ PyTypeObject PyUnicode_Type = {
89038889
(reprfunc) unicode_str, /* tp_str */
89048890
PyObject_GenericGetAttr, /* tp_getattro */
89058891
0, /* tp_setattro */
8906-
&unicode_as_buffer, /* tp_as_buffer */
8892+
0, /* tp_as_buffer */
89078893
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
89088894
Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */
89098895
unicode_doc, /* tp_doc */

Python/getargs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12521252
arg, msgbuf, bufsize);
12531253
if (pb == NULL || pb->bf_getbuffer == NULL)
12541254
return converterr(
1255-
"string or read-only character buffer",
1255+
"bytes or read-only character buffer",
12561256
arg, msgbuf, bufsize);
12571257

12581258
if ((*pb->bf_getbuffer)(arg, &view, PyBUF_SIMPLE) != 0)

0 commit comments

Comments
 (0)