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

Skip to content

Commit 2e6658b

Browse files
author
Stefan Krah
committed
Merge from 3.4.
2 parents 5b90f5d + 7213fcc commit 2e6658b

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

Modules/_testcapimodule.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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},

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
512512

513513
/* Otherwise a more elaborate scheme is needed */
514514

515-
/* XXX(nnorwitz): need to check for overflow! */
515+
/* view->ndim <= 64 */
516516
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
517517
if (indices == NULL) {
518518
PyErr_NoMemory();
@@ -534,10 +534,10 @@ PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
534534
*/
535535
elements = len / view->itemsize;
536536
while (elements--) {
537-
addone(view->ndim, indices, view->shape);
538537
ptr = PyBuffer_GetPointer(view, indices);
539538
memcpy(ptr, src, view->itemsize);
540539
src += view->itemsize;
540+
addone(view->ndim, indices, view->shape);
541541
}
542542

543543
PyMem_Free(indices);

0 commit comments

Comments
 (0)