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
084486b2
Commit
084486b2
authored
Dec 13, 2008
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #1706039: Support continued reading from a file even after
EOF was hit.
parent
710fb8bf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
17 deletions
+70
-17
Lib/test/test_file.py
Lib/test/test_file.py
+42
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/fileobject.c
Objects/fileobject.c
+25
-17
No files found.
Lib/test/test_file.py
View file @
084486b2
...
...
@@ -355,6 +355,48 @@ class StdoutTests(unittest.TestCase):
finally
:
sys
.
stdout
=
save_stdout
def
testReadAfterEOF
(
self
):
# Regression test for SF bug #1523853.
# Verify read works after hitting EOF
# Prepare the testfile
teststring
=
"spam"
bag
=
open
(
TESTFN
,
"w"
)
bag
.
write
(
teststring
)
bag
.
close
()
# And buf for readinto
buf
=
array
(
"c"
,
" "
*
len
(
teststring
))
# Test for appropriate errors mixing read* and iteration
methods
=
[(
"readline"
,
()),
(
"read"
,()),
(
"readlines"
,
()),
(
"readinto"
,
(
buf
,))]
for
attr
in
'r'
,
'rU'
:
for
methodname
,
args
in
methods
:
f
=
open
(
TESTFN
,
"rU"
)
f
.
seek
(
0
,
2
)
meth
=
getattr
(
f
,
methodname
)
meth
(
*
args
)
# hits EOF
try
:
# Writing the same file with another file descriptor
append
=
open
(
TESTFN
,
"a+"
)
append
.
write
(
teststring
)
append
.
flush
()
append
.
close
()
try
:
meth
=
getattr
(
f
,
methodname
)
if
methodname
==
'readlines'
:
self
.
failUnlessEqual
(
meth
(
*
args
),
[
teststring
])
elif
methodname
==
'readinto'
:
meth
(
*
args
)
self
.
failUnlessEqual
(
buf
.
tostring
(),
teststring
)
else
:
self
.
failUnlessEqual
(
meth
(
*
args
),
teststring
)
except
ValueError
:
self
.
fail
(
"read* failed after hitting EOF"
)
finally
:
f
.
close
()
def
test_main
():
# Historically, these tests have been sloppy about removing TESTFN.
...
...
Misc/NEWS
View file @
084486b2
...
...
@@ -12,6 +12,9 @@ What's New in Python 2.5.3?
Core and builtins
-----------------
- Issue #1706039: Support continued reading from a file even after
EOF was hit.
- Issue #1683: prevent forking from interfering in threading storage.
- Issue #4597: Fixed several opcodes that weren'
t
always
propagating
...
...
Objects/fileobject.c
View file @
084486b2
...
...
@@ -861,16 +861,16 @@ file_read(PyFileObject *f, PyObject *args)
buffersize
-
bytesread
,
f
->
f_fp
,
(
PyObject
*
)
f
);
Py_END_ALLOW_THREADS
if
(
chunksize
==
0
)
{
if
(
!
ferror
(
f
->
f_fp
))
if
(
!
PyErr_ExceptionMatches
(
PyExc_IOError
))
break
;
clearerr
(
f
->
f_fp
);
/* When in non-blocking mode, data shouldn't
* be discarded if a blocking signal was
* received. That will also happen if
* chunksize != 0, but bytesread < buffersize. */
if
(
bytesread
>
0
&&
BLOCKED_ERRNO
(
errno
))
if
(
bytesread
>
0
&&
BLOCKED_ERRNO
(
errno
))
{
PyErr_Clear
();
break
;
PyErr_SetFromErrno
(
PyExc_IOError
);
}
Py_DECREF
(
v
);
return
NULL
;
}
...
...
@@ -917,10 +917,8 @@ file_readinto(PyFileObject *f, PyObject *args)
(
PyObject
*
)
f
);
Py_END_ALLOW_THREADS
if
(
nnow
==
0
)
{
if
(
!
ferror
(
f
->
f_fp
))
if
(
!
PyErr_ExceptionMatches
(
PyExc_IOError
))
break
;
PyErr_SetFromErrno
(
PyExc_IOError
);
clearerr
(
f
->
f_fp
);
return
NULL
;
}
ndone
+=
nnow
;
...
...
@@ -1412,10 +1410,8 @@ file_readlines(PyFileObject *f, PyObject *args)
}
if
(
nread
==
0
)
{
sizehint
=
0
;
if
(
!
ferror
(
f
->
f_fp
))
if
(
!
PyErr_ExceptionMatches
(
PyExc_IOError
))
break
;
PyErr_SetFromErrno
(
PyExc_IOError
);
clearerr
(
f
->
f_fp
);
error:
Py_DECREF
(
list
);
list
=
NULL
;
...
...
@@ -1863,9 +1859,7 @@ readahead(PyFileObject *f, int bufsize)
f
->
f_buf
,
bufsize
,
f
->
f_fp
,
(
PyObject
*
)
f
);
Py_END_ALLOW_THREADS
if
(
chunksize
==
0
)
{
if
(
ferror
(
f
->
f_fp
))
{
PyErr_SetFromErrno
(
PyExc_IOError
);
clearerr
(
f
->
f_fp
);
if
(
PyErr_ExceptionMatches
(
PyExc_IOError
))
{
drop_readahead
(
f
);
return
-
1
;
}
...
...
@@ -2416,6 +2410,7 @@ Py_UniversalNewlineFread(char *buf, size_t n,
char
*
dst
=
buf
;
PyFileObject
*
f
=
(
PyFileObject
*
)
fobj
;
int
newlinetypes
,
skipnextlf
;
size_t
nread
;
assert
(
buf
!=
NULL
);
assert
(
stream
!=
NULL
);
...
...
@@ -2424,22 +2419,35 @@ Py_UniversalNewlineFread(char *buf, size_t n,
errno
=
ENXIO
;
/* What can you do... */
return
0
;
}
if
(
!
f
->
f_univ_newline
)
return
fread
(
buf
,
1
,
n
,
stream
);
if
(
!
f
->
f_univ_newline
)
{
nread
=
fread
(
buf
,
1
,
n
,
stream
);
if
(
nread
==
0
)
{
if
(
ferror
(
stream
))
PyErr_SetFromErrno
(
PyExc_IOError
);
clearerr
(
stream
);
}
return
nread
;
}
newlinetypes
=
f
->
f_newlinetypes
;
skipnextlf
=
f
->
f_skipnextlf
;
/* Invariant: n is the number of bytes remaining to be filled
* in the buffer.
*/
while
(
n
)
{
size_t
nread
;
int
shortread
;
char
*
src
=
dst
;
nread
=
fread
(
dst
,
1
,
n
,
stream
);
assert
(
nread
<=
n
);
if
(
nread
==
0
)
if
(
nread
==
0
)
{
if
(
ferror
(
stream
))
{
clearerr
(
stream
);
PyErr_SetFromErrno
(
PyExc_IOError
);
return
0
;
}
clearerr
(
stream
);
break
;
}
n
-=
nread
;
/* assuming 1 byte out for each in; will adjust */
shortread
=
n
!=
0
;
/* true iff EOF or error */
...
...
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