Mercurial > cpython-lru_cache-weakref
diff Modules/arraymodule.c @ 41434:f390793e75de
Make array().tofile() work with a new I/O object.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 11 Apr 2007 17:08:28 +0000 |
| parents | 6ebad2a6b990 |
| children | 06145fbc7ab9 |
line wrap: on
line diff
--- a/Modules/arraymodule.c Wed Apr 11 16:32:43 2007 +0000 +++ b/Modules/arraymodule.c Wed Apr 11 17:08:28 2007 +0000 @@ -1252,12 +1252,11 @@ { FILE *fp; + if (self->ob_size == 0) + goto done; + fp = PyFile_AsFile(f); - if (fp == NULL) { - PyErr_SetString(PyExc_TypeError, "arg must be open file"); - return NULL; - } - if (self->ob_size > 0) { + if (fp != NULL) { if (fwrite(self->ob_item, self->ob_descr->itemsize, self->ob_size, fp) != (size_t)self->ob_size) { PyErr_SetFromErrno(PyExc_IOError); @@ -1265,6 +1264,31 @@ return NULL; } } + else { + Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize; + /* Write 64K blocks at a time */ + /* XXX Make the block size settable */ + int BLOCKSIZE = 64*1024; + Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; + Py_ssize_t i; + for (i = 0; i < nblocks; i++) { + char* ptr = self->ob_item + i*BLOCKSIZE; + Py_ssize_t size = BLOCKSIZE; + PyObject *bytes, *res; + if (i*BLOCKSIZE + size > nbytes) + size = nbytes - i*BLOCKSIZE; + bytes = PyBytes_FromStringAndSize(ptr, size); + if (bytes == NULL) + return NULL; + res = PyObject_CallMethod(f, "write", "O", + bytes); + Py_DECREF(bytes); + if (res == NULL) + return NULL; + } + } + + done: Py_INCREF(Py_None); return Py_None; }
