@@ -2468,6 +2468,56 @@ make_memoryview_from_NULL_pointer(PyObject *self)
24682468 return NULL ;
24692469 return PyMemoryView_FromBuffer (& info );
24702470}
2471+
2472+ static PyObject *
2473+ test_from_contiguous (PyObject * self , PyObject * noargs )
2474+ {
2475+ int data [9 ] = {-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 };
2476+ int init [5 ] = {0 , 1 , 2 , 3 , 4 };
2477+ Py_ssize_t itemsize = sizeof (int );
2478+ Py_ssize_t shape = 5 ;
2479+ Py_ssize_t strides = 2 * itemsize ;
2480+ Py_buffer view = {
2481+ data ,
2482+ NULL ,
2483+ 5 * itemsize ,
2484+ itemsize ,
2485+ 1 ,
2486+ 1 ,
2487+ NULL ,
2488+ & shape ,
2489+ & strides ,
2490+ NULL ,
2491+ NULL
2492+ };
2493+ int * ptr ;
2494+ int i ;
2495+
2496+ PyBuffer_FromContiguous (& view , init , view .len , 'C' );
2497+ ptr = view .buf ;
2498+ for (i = 0 ; i < 5 ; i ++ ) {
2499+ if (ptr [2 * i ] != i ) {
2500+ PyErr_SetString (TestError ,
2501+ "test_from_contiguous: incorrect result" );
2502+ return NULL ;
2503+ }
2504+ }
2505+
2506+ view .buf = & data [8 ];
2507+ view .strides [0 ] = -2 * itemsize ;
2508+
2509+ PyBuffer_FromContiguous (& view , init , view .len , 'C' );
2510+ ptr = view .buf ;
2511+ for (i = 0 ; i < 5 ; i ++ ) {
2512+ if (* (ptr - 2 * i ) != i ) {
2513+ PyErr_SetString (TestError ,
2514+ "test_from_contiguous: incorrect result" );
2515+ return NULL ;
2516+ }
2517+ }
2518+
2519+ Py_RETURN_NONE ;
2520+ }
24712521
24722522/* Test that the fatal error from not having a current thread doesn't
24732523 cause an infinite loop. Run via Lib/test/test_capi.py */
@@ -3128,6 +3178,7 @@ static PyMethodDef TestMethods[] = {
31283178 {"test_string_to_double" , (PyCFunction )test_string_to_double , METH_NOARGS },
31293179 {"test_unicode_compare_with_ascii" , (PyCFunction )test_unicode_compare_with_ascii , METH_NOARGS },
31303180 {"test_capsule" , (PyCFunction )test_capsule , METH_NOARGS },
3181+ {"test_from_contiguous" , (PyCFunction )test_from_contiguous , METH_NOARGS },
31313182 {"getargs_tuple" , getargs_tuple , METH_VARARGS },
31323183 {"getargs_keywords" , (PyCFunction )getargs_keywords ,
31333184 METH_VARARGS |METH_KEYWORDS },
0 commit comments