Commit 2cc9e465 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #361 from undingen/memoryview

Add bytearray and add a memoryview test
parents e1ba120c b27e16fd
...@@ -30,7 +30,9 @@ _getbytevalue(PyObject* arg, int *value) ...@@ -30,7 +30,9 @@ _getbytevalue(PyObject* arg, int *value)
long face_value; long face_value;
if (PyBytes_CheckExact(arg)) { if (PyBytes_CheckExact(arg)) {
if (Py_SIZE(arg) != 1) { // Pyston change: use correct size function
// if (Py_SIZE(arg) != 1) {
if (PyBytes_Size(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "string must be of size 1"); PyErr_SetString(PyExc_ValueError, "string must be of size 1");
return 0; return 0;
} }
......
...@@ -90,8 +90,56 @@ extern "C" int PyBuffer_IsContiguous(Py_buffer* view, char fort) noexcept { ...@@ -90,8 +90,56 @@ extern "C" int PyBuffer_IsContiguous(Py_buffer* view, char fort) noexcept {
return 0; return 0;
} }
/* view is not checked for consistency in either of these. It is
assumed that the size of the buffer is view->len in
view->len / view->itemsize elements.
*/
extern "C" int PyBuffer_ToContiguous(void* buf, Py_buffer* view, Py_ssize_t len, char fort) noexcept { extern "C" int PyBuffer_ToContiguous(void* buf, Py_buffer* view, Py_ssize_t len, char fort) noexcept {
Py_FatalError("unimplemented"); int k;
void (*addone)(int, Py_ssize_t*, const Py_ssize_t*);
Py_ssize_t* indices, elements;
char* dest, *ptr;
if (len > view->len) {
len = view->len;
}
if (PyBuffer_IsContiguous(view, fort)) {
/* simplest copy is all that is needed */
memcpy(buf, view->buf, len);
return 0;
}
/* Otherwise a more elaborate scheme is needed */
/* XXX(nnorwitz): need to check for overflow! */
indices = (Py_ssize_t*)PyMem_Malloc(sizeof(Py_ssize_t) * (view->ndim));
if (indices == NULL) {
PyErr_NoMemory();
return -1;
}
for (k = 0; k < view->ndim; k++) {
indices[k] = 0;
}
if (fort == 'F') {
addone = _Py_add_one_to_index_F;
} else {
addone = _Py_add_one_to_index_C;
}
dest = (char*)buf;
/* XXX : This is not going to be the fastest code in the world
several optimizations are possible.
*/
elements = len / view->itemsize;
while (elements--) {
addone(view->ndim, indices, view->shape);
ptr = (char*)PyBuffer_GetPointer(view, indices);
memcpy(dest, ptr, view->itemsize);
dest += view->itemsize;
}
PyMem_Free(indices);
return 0;
} }
extern "C" int PyBuffer_FillInfo(Py_buffer* view, PyObject* obj, void* buf, Py_ssize_t len, int readonly, extern "C" int PyBuffer_FillInfo(Py_buffer* view, PyObject* obj, void* buf, Py_ssize_t len, int readonly,
...@@ -135,9 +183,6 @@ extern "C" void PyBuffer_Release(Py_buffer* view) noexcept { ...@@ -135,9 +183,6 @@ extern "C" void PyBuffer_Release(Py_buffer* view) noexcept {
PyObject* obj = view->obj; PyObject* obj = view->obj;
if (obj) { if (obj) {
// This is a Pyston assert
assert(isSubclass(obj->cls, str_cls));
if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer) if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view); Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
Py_XDECREF(obj); Py_XDECREF(obj);
......
...@@ -1178,6 +1178,8 @@ void setupBuiltins() { ...@@ -1178,6 +1178,8 @@ void setupBuiltins() {
Py_TYPE(&PyMemoryView_Type) = &PyType_Type; Py_TYPE(&PyMemoryView_Type) = &PyType_Type;
PyType_Ready(&PyMemoryView_Type); PyType_Ready(&PyMemoryView_Type);
builtins_module->giveAttr("memoryview", memoryview_cls); builtins_module->giveAttr("memoryview", memoryview_cls);
PyType_Ready(&PyByteArray_Type);
builtins_module->giveAttr("bytearray", &PyByteArray_Type);
builtins_module->giveAttr( builtins_module->giveAttr(
"eval", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)eval, UNKNOWN, 1, 0, false, false), "eval")); "eval", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)eval, UNKNOWN, 1, 0, false, false), "eval"));
......
for d in ["teststring", 42]:
b = bytearray(d)
print b, len(b)
del b[1]
b.append('!')
print b
b.extend('?.')
print b
b.reverse()
print b
def test(v):
print len(v), v.readonly, v.itemsize, v.tobytes(), v.tolist()
for d in ["123456789", bytearray("123456789")]:
v = memoryview(d)
test(v)
s = v[1:len(v)-1]
test(s)
if not s.readonly:
s[1:4] = "abc"
test(s)
test(v)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment