@@ -2518,21 +2518,26 @@ test_from_contiguous(PyObject* self, PyObject *noargs)
25182518
25192519 Py_RETURN_NONE ;
25202520}
2521-
2521+
2522+ extern PyTypeObject _PyBytesIOBuffer_Type ;
2523+
25222524static PyObject *
25232525test_pep3118_obsolete_write_locks (PyObject * self , PyObject * noargs )
25242526{
2527+ PyTypeObject * type = & _PyBytesIOBuffer_Type ;
25252528 PyObject * b ;
25262529 char * dummy [1 ];
25272530 int ret , match ;
25282531
2532+ /* PyBuffer_FillInfo() */
25292533 ret = PyBuffer_FillInfo (NULL , NULL , dummy , 1 , 0 , PyBUF_SIMPLE );
25302534 match = PyErr_Occurred () && PyErr_ExceptionMatches (PyExc_BufferError );
25312535 PyErr_Clear ();
25322536 if (ret != -1 || match == 0 )
25332537 goto error ;
25342538
2535- b = PyByteArray_FromStringAndSize ("" , 0 );
2539+ /* bytesiobuf_getbuffer() */
2540+ b = type -> tp_alloc (type , 0 );
25362541 if (b == NULL ) {
25372542 return NULL ;
25382543 }
@@ -2552,6 +2557,18 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
25522557 return NULL ;
25532558}
25542559
2560+ /* This tests functions that historically supported write locks. It is
2561+ wrong to call getbuffer() with view==NULL and a compliant getbufferproc
2562+ is entitled to segfault in that case. */
2563+ static PyObject *
2564+ getbuffer_with_null_view (PyObject * self , PyObject * obj )
2565+ {
2566+ if (PyObject_GetBuffer (obj , NULL , PyBUF_SIMPLE ) < 0 )
2567+ return NULL ;
2568+
2569+ Py_RETURN_NONE ;
2570+ }
2571+
25552572/* Test that the fatal error from not having a current thread doesn't
25562573 cause an infinite loop. Run via Lib/test/test_capi.py */
25572574static PyObject *
@@ -3213,6 +3230,7 @@ static PyMethodDef TestMethods[] = {
32133230 {"test_capsule" , (PyCFunction )test_capsule , METH_NOARGS },
32143231 {"test_from_contiguous" , (PyCFunction )test_from_contiguous , METH_NOARGS },
32153232 {"test_pep3118_obsolete_write_locks" , (PyCFunction )test_pep3118_obsolete_write_locks , METH_NOARGS },
3233+ {"getbuffer_with_null_view" , getbuffer_with_null_view , METH_O },
32163234 {"getargs_tuple" , getargs_tuple , METH_VARARGS },
32173235 {"getargs_keywords" , (PyCFunction )getargs_keywords ,
32183236 METH_VARARGS |METH_KEYWORDS },
0 commit comments