summaryrefslogtreecommitdiffstats
path: root/Modules/_sre.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_sre.c')
-rw-r--r--Modules/_sre.c44
1 files changed, 19 insertions, 25 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 012f127..295bc93 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1672,44 +1672,38 @@ getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
Py_ssize_t size, bytes;
int charsize;
void* ptr;
-
-#if defined(HAVE_UNICODE)
- if (PyUnicode_Check(string)) {
- /* unicode strings doesn't always support the buffer interface */
- ptr = (void*) PyUnicode_AS_DATA(string);
- bytes = PyUnicode_GET_DATA_SIZE(string);
- size = PyUnicode_GET_SIZE(string);
- charsize = sizeof(Py_UNICODE);
-
- } else {
-#endif
+ PyBuffer view;
/* get pointer to string buffer */
+ view.len = -1;
buffer = Py_Type(string)->tp_as_buffer;
- if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount ||
- buffer->bf_getsegcount(string, NULL) != 1) {
- PyErr_SetString(PyExc_TypeError, "expected string or buffer");
- return NULL;
+ if (!buffer || !buffer->bf_getbuffer ||
+ (*buffer->bf_getbuffer)(string, &view, PyBUF_SIMPLE) < 0) {
+ PyErr_SetString(PyExc_TypeError, "expected string or buffer");
+ return NULL;
}
/* determine buffer size */
- bytes = buffer->bf_getreadbuffer(string, 0, &ptr);
+ bytes = view.len;
+ ptr = view.buf;
+
+ /* Release the buffer immediately --- possibly dangerous
+ but doing something else would require some re-factoring
+ */
+ PyObject_ReleaseBuffer(string, &view);
+
if (bytes < 0) {
PyErr_SetString(PyExc_TypeError, "buffer has negative size");
return NULL;
}
/* determine character size */
-#if PY_VERSION_HEX >= 0x01060000
size = PyObject_Size(string);
-#else
- size = PyObject_Length(string);
-#endif
if (PyString_Check(string) || bytes == size)
charsize = 1;
#if defined(HAVE_UNICODE)
- else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
+ else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
charsize = sizeof(Py_UNICODE);
#endif
else {
@@ -1717,13 +1711,13 @@ getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
return NULL;
}
-#if defined(HAVE_UNICODE)
- }
-#endif
-
*p_length = size;
*p_charsize = charsize;
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "Buffer is NULL");
+ }
return ptr;
}