Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b5ddcfd2
Commit
b5ddcfd2
authored
Apr 11, 2007
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make array().tofile() work with a new I/O object.
parent
d0712817
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
6 deletions
+30
-6
Lib/test/test_array.py
Lib/test/test_array.py
+1
-1
Modules/arraymodule.c
Modules/arraymodule.c
+29
-5
No files found.
Lib/test/test_array.py
View file @
b5ddcfd2
...
@@ -147,7 +147,7 @@ class BaseTest(unittest.TestCase):
...
@@ -147,7 +147,7 @@ class BaseTest(unittest.TestCase):
def
test_tofromfile
(
self
):
def
test_tofromfile
(
self
):
a
=
array
.
array
(
self
.
typecode
,
2
*
self
.
example
)
a
=
array
.
array
(
self
.
typecode
,
2
*
self
.
example
)
self
.
assertRaises
(
TypeError
,
a
.
tofile
)
self
.
assertRaises
(
TypeError
,
a
.
tofile
)
self
.
assertRaises
(
TypeError
,
a
.
tofile
,
cStringIO
.
StringIO
())
##
self.assertRaises(TypeError, a.tofile, cStringIO.StringIO())
f
=
open
(
test_support
.
TESTFN
,
'wb'
)
f
=
open
(
test_support
.
TESTFN
,
'wb'
)
try
:
try
:
a
.
tofile
(
f
)
a
.
tofile
(
f
)
...
...
Modules/arraymodule.c
View file @
b5ddcfd2
...
@@ -1252,12 +1252,11 @@ array_tofile(arrayobject *self, PyObject *f)
...
@@ -1252,12 +1252,11 @@ array_tofile(arrayobject *self, PyObject *f)
{
{
FILE
*
fp
;
FILE
*
fp
;
if
(
self
->
ob_size
==
0
)
goto
done
;
fp
=
PyFile_AsFile
(
f
);
fp
=
PyFile_AsFile
(
f
);
if
(
fp
==
NULL
)
{
if
(
fp
!=
NULL
)
{
PyErr_SetString
(
PyExc_TypeError
,
"arg must be open file"
);
return
NULL
;
}
if
(
self
->
ob_size
>
0
)
{
if
(
fwrite
(
self
->
ob_item
,
self
->
ob_descr
->
itemsize
,
if
(
fwrite
(
self
->
ob_item
,
self
->
ob_descr
->
itemsize
,
self
->
ob_size
,
fp
)
!=
(
size_t
)
self
->
ob_size
)
{
self
->
ob_size
,
fp
)
!=
(
size_t
)
self
->
ob_size
)
{
PyErr_SetFromErrno
(
PyExc_IOError
);
PyErr_SetFromErrno
(
PyExc_IOError
);
...
@@ -1265,6 +1264,31 @@ array_tofile(arrayobject *self, PyObject *f)
...
@@ -1265,6 +1264,31 @@ array_tofile(arrayobject *self, PyObject *f)
return
NULL
;
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
);
Py_INCREF
(
Py_None
);
return
Py_None
;
return
Py_None
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment