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

Skip to content

Commit b5ddcfd

Browse files
committed
Make array().tofile() work with a new I/O object.
1 parent d071281 commit b5ddcfd

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

Lib/test/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_insert(self):
147147
def test_tofromfile(self):
148148
a = array.array(self.typecode, 2*self.example)
149149
self.assertRaises(TypeError, a.tofile)
150-
self.assertRaises(TypeError, a.tofile, cStringIO.StringIO())
150+
##self.assertRaises(TypeError, a.tofile, cStringIO.StringIO())
151151
f = open(test_support.TESTFN, 'wb')
152152
try:
153153
a.tofile(f)

Modules/arraymodule.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,19 +1252,43 @@ array_tofile(arrayobject *self, PyObject *f)
12521252
{
12531253
FILE *fp;
12541254

1255+
if (self->ob_size == 0)
1256+
goto done;
1257+
12551258
fp = PyFile_AsFile(f);
1256-
if (fp == NULL) {
1257-
PyErr_SetString(PyExc_TypeError, "arg must be open file");
1258-
return NULL;
1259-
}
1260-
if (self->ob_size > 0) {
1259+
if (fp != NULL) {
12611260
if (fwrite(self->ob_item, self->ob_descr->itemsize,
12621261
self->ob_size, fp) != (size_t)self->ob_size) {
12631262
PyErr_SetFromErrno(PyExc_IOError);
12641263
clearerr(fp);
12651264
return NULL;
12661265
}
12671266
}
1267+
else {
1268+
Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize;
1269+
/* Write 64K blocks at a time */
1270+
/* XXX Make the block size settable */
1271+
int BLOCKSIZE = 64*1024;
1272+
Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1273+
Py_ssize_t i;
1274+
for (i = 0; i < nblocks; i++) {
1275+
char* ptr = self->ob_item + i*BLOCKSIZE;
1276+
Py_ssize_t size = BLOCKSIZE;
1277+
PyObject *bytes, *res;
1278+
if (i*BLOCKSIZE + size > nbytes)
1279+
size = nbytes - i*BLOCKSIZE;
1280+
bytes = PyBytes_FromStringAndSize(ptr, size);
1281+
if (bytes == NULL)
1282+
return NULL;
1283+
res = PyObject_CallMethod(f, "write", "O",
1284+
bytes);
1285+
Py_DECREF(bytes);
1286+
if (res == NULL)
1287+
return NULL;
1288+
}
1289+
}
1290+
1291+
done:
12681292
Py_INCREF(Py_None);
12691293
return Py_None;
12701294
}

0 commit comments

Comments
 (0)