Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3070)

Side by Side Diff: Objects/fileobject.c

Issue 1706039: Added clearerr() to clear EOF state
Patch Set: Created 2 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_file.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* File object implementation */ 1 /* File object implementation */
2 2
3 #define PY_SSIZE_T_CLEAN 3 #define PY_SSIZE_T_CLEAN
4 #include "Python.h" 4 #include "Python.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 6
7 #ifdef HAVE_SYS_TYPES_H 7 #ifdef HAVE_SYS_TYPES_H
8 #include <sys/types.h> 8 #include <sys/types.h>
9 #endif /* HAVE_SYS_TYPES_H */ 9 #endif /* HAVE_SYS_TYPES_H */
10 10
(...skipping 2497 matching lines...) Expand 10 before | Expand all | Expand 10 after
2508 int newlinetypes = 0; 2508 int newlinetypes = 0;
2509 int skipnextlf = 0; 2509 int skipnextlf = 0;
2510 int univ_newline = 1; 2510 int univ_newline = 1;
2511 2511
2512 if (fobj) { 2512 if (fobj) {
2513 if (!PyFile_Check(fobj)) { 2513 if (!PyFile_Check(fobj)) {
2514 errno = ENXIO; /* What can you do... */ 2514 errno = ENXIO; /* What can you do... */
2515 return NULL; 2515 return NULL;
2516 } 2516 }
2517 univ_newline = ((PyFileObject *)fobj)->f_univ_newline; 2517 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2518 » » if ( !univ_newline ) 2518 » » if ( !univ_newline ) {
2519 » » » /* Issue #1706039: Support continued reading from a file even after
2520 » » » * EOF was hit.
2521 » » » */
2522 » » » clearerr(stream);
2519 return fgets(buf, n, stream); 2523 return fgets(buf, n, stream);
2524 }
2520 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; 2525 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2521 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; 2526 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2522 } 2527 }
2523 FLOCKFILE(stream); 2528 FLOCKFILE(stream);
2529 /* Issue #1706039: Support continued reading from a file even after
2530 * EOF was hit.
2531 */
2532 clearerr(stream);
2524 c = 'x'; /* Shut up gcc warning */ 2533 c = 'x'; /* Shut up gcc warning */
2525 while (--n > 0 && (c = GETC(stream)) != EOF ) { 2534 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2526 if (skipnextlf ) { 2535 if (skipnextlf ) {
2527 skipnextlf = 0; 2536 skipnextlf = 0;
2528 if (c == '\n') { 2537 if (c == '\n') {
2529 /* Seeing a \n here with skipnextlf true 2538 /* Seeing a \n here with skipnextlf true
2530 ** means we saw a \r before. 2539 ** means we saw a \r before.
2531 */ 2540 */
2532 newlinetypes |= NEWLINE_CRLF; 2541 newlinetypes |= NEWLINE_CRLF;
2533 c = GETC(stream); 2542 c = GETC(stream);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2596 PyFileObject *f = (PyFileObject *)fobj; 2605 PyFileObject *f = (PyFileObject *)fobj;
2597 int newlinetypes, skipnextlf; 2606 int newlinetypes, skipnextlf;
2598 2607
2599 assert(buf != NULL); 2608 assert(buf != NULL);
2600 assert(stream != NULL); 2609 assert(stream != NULL);
2601 2610
2602 if (!fobj || !PyFile_Check(fobj)) { 2611 if (!fobj || !PyFile_Check(fobj)) {
2603 errno = ENXIO; /* What can you do... */ 2612 errno = ENXIO; /* What can you do... */
2604 return 0; 2613 return 0;
2605 } 2614 }
2615 /* Issue #1706039: Support continued reading from a file even after
2616 * EOF was hit.
2617 */
2618 clearerr(stream);
2606 if (!f->f_univ_newline) 2619 if (!f->f_univ_newline)
2607 return fread(buf, 1, n, stream); 2620 return fread(buf, 1, n, stream);
2608 newlinetypes = f->f_newlinetypes; 2621 newlinetypes = f->f_newlinetypes;
2609 skipnextlf = f->f_skipnextlf; 2622 skipnextlf = f->f_skipnextlf;
2610 /* Invariant: n is the number of bytes remaining to be filled 2623 /* Invariant: n is the number of bytes remaining to be filled
2611 * in the buffer. 2624 * in the buffer.
2612 */ 2625 */
2613 while (n) { 2626 while (n) {
2614 size_t nread; 2627 size_t nread;
2615 int shortread; 2628 int shortread;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2656 } 2669 }
2657 } 2670 }
2658 f->f_newlinetypes = newlinetypes; 2671 f->f_newlinetypes = newlinetypes;
2659 f->f_skipnextlf = skipnextlf; 2672 f->f_skipnextlf = skipnextlf;
2660 return dst - buf; 2673 return dst - buf;
2661 } 2674 }
2662 2675
2663 #ifdef __cplusplus 2676 #ifdef __cplusplus
2664 } 2677 }
2665 #endif 2678 #endif
OLDNEW
« no previous file with comments | « Lib/test/test_file.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7