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

Side by Side Diff: Modules/_io/textio.c

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 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 | « Modules/_io/fileio.c ('k') | Modules/md5module.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 An implementation of Text I/O as defined by PEP 3116 - "New I/O" 2 An implementation of Text I/O as defined by PEP 3116 - "New I/O"
3 3
4 Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper. 4 Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper.
5 5
6 Written by Amaury Forgeot d'Arc and Antoine Pitrou 6 Written by Amaury Forgeot d'Arc and Antoine Pitrou
7 */ 7 */
8 8
9 #define PY_SSIZE_T_CLEAN 9 #define PY_SSIZE_T_CLEAN
10 #include "Python.h" 10 #include "Python.h"
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 int detached; 646 int detached;
647 Py_ssize_t chunk_size; 647 Py_ssize_t chunk_size;
648 PyObject *buffer; 648 PyObject *buffer;
649 PyObject *encoding; 649 PyObject *encoding;
650 PyObject *encoder; 650 PyObject *encoder;
651 PyObject *decoder; 651 PyObject *decoder;
652 PyObject *readnl; 652 PyObject *readnl;
653 PyObject *errors; 653 PyObject *errors;
654 const char *writenl; /* utf-8 encoded, NULL stands for \n */ 654 const char *writenl; /* utf-8 encoded, NULL stands for \n */
655 char line_buffering; 655 char line_buffering;
656 char write_through;
657 char readuniversal; 656 char readuniversal;
658 char readtranslate; 657 char readtranslate;
659 char writetranslate; 658 char writetranslate;
660 char seekable; 659 char seekable;
661 char has_read1;
662 char telling; 660 char telling;
663 char deallocating; 661 char deallocating;
664 /* Specialized encoding func (see below) */ 662 /* Specialized encoding func (see below) */
665 encodefunc_t encodefunc; 663 encodefunc_t encodefunc;
666 /* Whether or not it's the start of the stream */ 664 /* Whether or not it's the start of the stream */
667 char encoding_start_of_stream; 665 char encoding_start_of_stream;
668 666
669 /* Reads and writes are internally buffered in order to speed things up. 667 /* Reads and writes are internally buffered in order to speed things up.
670 However, any read will first flush the write buffer if itsn't empty. 668 However, any read will first flush the write buffer if itsn't empty.
671 669
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 {"utf-32-le", (encodefunc_t) utf32le_encode}, 806 {"utf-32-le", (encodefunc_t) utf32le_encode},
809 {"utf-32", (encodefunc_t) utf32_encode}, 807 {"utf-32", (encodefunc_t) utf32_encode},
810 {NULL, NULL} 808 {NULL, NULL}
811 }; 809 };
812 810
813 811
814 static int 812 static int
815 textiowrapper_init(textio *self, PyObject *args, PyObject *kwds) 813 textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
816 { 814 {
817 char *kwlist[] = {"buffer", "encoding", "errors", 815 char *kwlist[] = {"buffer", "encoding", "errors",
818 "newline", "line_buffering", "write_through", 816 "newline", "line_buffering",
819 NULL}; 817 NULL};
820 PyObject *buffer, *raw; 818 PyObject *buffer, *raw;
821 char *encoding = NULL; 819 char *encoding = NULL;
822 char *errors = NULL; 820 char *errors = NULL;
823 char *newline = NULL; 821 char *newline = NULL;
824 int line_buffering = 0, write_through = 0; 822 int line_buffering = 0;
825 _PyIO_State *state = IO_STATE; 823 _PyIO_State *state = IO_STATE;
826 824
827 PyObject *res; 825 PyObject *res;
828 int r; 826 int r;
829 827
830 self->ok = 0; 828 self->ok = 0;
831 self->detached = 0; 829 self->detached = 0;
832 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzii:fileio", 830 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzi:fileio",
833 kwlist, &buffer, &encoding, &errors, 831 kwlist, &buffer, &encoding, &errors,
834 &newline, &line_buffering, &write_through)) 832 &newline, &line_buffering))
835 return -1; 833 return -1;
836 834
837 if (newline && newline[0] != '\0' 835 if (newline && newline[0] != '\0'
838 && !(newline[0] == '\n' && newline[1] == '\0') 836 && !(newline[0] == '\n' && newline[1] == '\0')
839 && !(newline[0] == '\r' && newline[1] == '\0') 837 && !(newline[0] == '\r' && newline[1] == '\0')
840 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) { 838 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
841 PyErr_Format(PyExc_ValueError, 839 PyErr_Format(PyExc_ValueError,
842 "illegal newline value: %s", newline); 840 "illegal newline value: %s", newline);
843 return -1; 841 return -1;
844 } 842 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 928
931 if (errors == NULL) 929 if (errors == NULL)
932 errors = "strict"; 930 errors = "strict";
933 self->errors = PyBytes_FromString(errors); 931 self->errors = PyBytes_FromString(errors);
934 if (self->errors == NULL) 932 if (self->errors == NULL)
935 goto error; 933 goto error;
936 934
937 self->chunk_size = 8192; 935 self->chunk_size = 8192;
938 self->readuniversal = (newline == NULL || newline[0] == '\0'); 936 self->readuniversal = (newline == NULL || newline[0] == '\0');
939 self->line_buffering = line_buffering; 937 self->line_buffering = line_buffering;
940 self->write_through = write_through;
941 self->readtranslate = (newline == NULL); 938 self->readtranslate = (newline == NULL);
942 if (newline) { 939 if (newline) {
943 self->readnl = PyUnicode_FromString(newline); 940 self->readnl = PyUnicode_FromString(newline);
944 if (self->readnl == NULL) 941 if (self->readnl == NULL)
945 return -1; 942 return -1;
946 } 943 }
947 self->writetranslate = (newline == NULL || newline[0] != '\0'); 944 self->writetranslate = (newline == NULL || newline[0] != '\0');
948 if (!self->readuniversal && self->readnl) { 945 if (!self->readuniversal && self->readnl) {
949 self->writenl = _PyUnicode_AsString(self->readnl); 946 self->writenl = _PyUnicode_AsString(self->readnl);
950 if (self->writenl == NULL) 947 if (self->writenl == NULL)
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 self->raw = raw; 1036 self->raw = raw;
1040 else 1037 else
1041 Py_DECREF(raw); 1038 Py_DECREF(raw);
1042 } 1039 }
1043 1040
1044 res = PyObject_CallMethod(buffer, "seekable", NULL); 1041 res = PyObject_CallMethod(buffer, "seekable", NULL);
1045 if (res == NULL) 1042 if (res == NULL)
1046 goto error; 1043 goto error;
1047 self->seekable = self->telling = PyObject_IsTrue(res); 1044 self->seekable = self->telling = PyObject_IsTrue(res);
1048 Py_DECREF(res); 1045 Py_DECREF(res);
1049
1050 self->has_read1 = PyObject_HasAttrString(buffer, "read1");
1051 1046
1052 self->encoding_start_of_stream = 0; 1047 self->encoding_start_of_stream = 0;
1053 if (self->seekable && self->encoder) { 1048 if (self->seekable && self->encoder) {
1054 PyObject *cookieObj; 1049 PyObject *cookieObj;
1055 int cmp; 1050 int cmp;
1056 1051
1057 self->encoding_start_of_stream = 1; 1052 self->encoding_start_of_stream = 1;
1058 1053
1059 cookieObj = PyObject_CallMethodObjArgs(buffer, _PyIO_str_tell, NULL); 1054 cookieObj = PyObject_CallMethodObjArgs(buffer, _PyIO_str_tell, NULL);
1060 if (cookieObj == NULL) 1055 if (cookieObj == NULL)
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 1280
1286 if (haslf && self->writetranslate && self->writenl != NULL) { 1281 if (haslf && self->writetranslate && self->writenl != NULL) {
1287 PyObject *newtext = PyObject_CallMethod( 1282 PyObject *newtext = PyObject_CallMethod(
1288 text, "replace", "ss", "\n", self->writenl); 1283 text, "replace", "ss", "\n", self->writenl);
1289 Py_DECREF(text); 1284 Py_DECREF(text);
1290 if (newtext == NULL) 1285 if (newtext == NULL)
1291 return NULL; 1286 return NULL;
1292 text = newtext; 1287 text = newtext;
1293 } 1288 }
1294 1289
1295 if (self->write_through) 1290 if (self->line_buffering &&
1296 needflush = 1;
1297 else if (self->line_buffering &&
1298 (haslf || 1291 (haslf ||
1299 findchar(PyUnicode_AS_UNICODE(text), 1292 findchar(PyUnicode_AS_UNICODE(text),
1300 PyUnicode_GET_SIZE(text), '\r'))) 1293 PyUnicode_GET_SIZE(text), '\r')))
1301 needflush = 1; 1294 needflush = 1;
1302 1295
1303 /* XXX What if we were just reading? */ 1296 /* XXX What if we were just reading? */
1304 if (self->encodefunc != NULL) { 1297 if (self->encodefunc != NULL) {
1305 b = (*self->encodefunc)((PyObject *) self, text); 1298 b = (*self->encodefunc)((PyObject *) self, text);
1306 self->encoding_start_of_stream = 0; 1299 self->encoding_start_of_stream = 0;
1307 } 1300 }
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 Py_INCREF(dec_buffer); 1428 Py_INCREF(dec_buffer);
1436 Py_INCREF(dec_flags); 1429 Py_INCREF(dec_flags);
1437 Py_DECREF(state); 1430 Py_DECREF(state);
1438 } 1431 }
1439 1432
1440 /* Read a chunk, decode it, and put the result in self._decoded_chars. */ 1433 /* Read a chunk, decode it, and put the result in self._decoded_chars. */
1441 chunk_size = PyLong_FromSsize_t(self->chunk_size); 1434 chunk_size = PyLong_FromSsize_t(self->chunk_size);
1442 if (chunk_size == NULL) 1435 if (chunk_size == NULL)
1443 goto fail; 1436 goto fail;
1444 input_chunk = PyObject_CallMethodObjArgs(self->buffer, 1437 input_chunk = PyObject_CallMethodObjArgs(self->buffer,
1445 (self->has_read1 ? _PyIO_str_read1: _PyIO_str_read), 1438 _PyIO_str_read1, chunk_size, NULL);
1446 chunk_size, NULL);
1447 Py_DECREF(chunk_size); 1439 Py_DECREF(chunk_size);
1448 if (input_chunk == NULL) 1440 if (input_chunk == NULL)
1449 goto fail; 1441 goto fail;
1450 assert(PyBytes_Check(input_chunk)); 1442 assert(PyBytes_Check(input_chunk));
1451 1443
1452 nbytes = PyBytes_Size(input_chunk); 1444 nbytes = PyBytes_Size(input_chunk);
1453 eof = (nbytes == 0); 1445 eof = (nbytes == 0);
1454 1446
1455 if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) { 1447 if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) {
1456 decoded_chars = _PyIncrementalNewlineDecoder_decode( 1448 decoded_chars = _PyIncrementalNewlineDecoder_decode(
(...skipping 1162 matching lines...) Expand 10 before | Expand all | Expand 10 after
2619 { 2611 {
2620 CHECK_INITIALIZED(self); 2612 CHECK_INITIALIZED(self);
2621 return PyLong_FromSsize_t(self->chunk_size); 2613 return PyLong_FromSsize_t(self->chunk_size);
2622 } 2614 }
2623 2615
2624 static int 2616 static int
2625 textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context) 2617 textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
2626 { 2618 {
2627 Py_ssize_t n; 2619 Py_ssize_t n;
2628 CHECK_INITIALIZED_INT(self); 2620 CHECK_INITIALIZED_INT(self);
2629 n = PyNumber_AsSsize_t(arg, PyExc_ValueError); 2621 n = PyNumber_AsSsize_t(arg, PyExc_TypeError);
2630 if (n == -1 && PyErr_Occurred()) 2622 if (n == -1 && PyErr_Occurred())
2631 return -1; 2623 return -1;
2632 if (n <= 0) { 2624 if (n <= 0) {
2633 PyErr_SetString(PyExc_ValueError, 2625 PyErr_SetString(PyExc_ValueError,
2634 "a strictly positive integer is required"); 2626 "a strictly positive integer is required");
2635 return -1; 2627 return -1;
2636 } 2628 }
2637 self->chunk_size = n; 2629 self->chunk_size = n;
2638 return 0; 2630 return 0;
2639 } 2631 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2712 textiowrapper_getset, /* tp_getset */ 2704 textiowrapper_getset, /* tp_getset */
2713 0, /* tp_base */ 2705 0, /* tp_base */
2714 0, /* tp_dict */ 2706 0, /* tp_dict */
2715 0, /* tp_descr_get */ 2707 0, /* tp_descr_get */
2716 0, /* tp_descr_set */ 2708 0, /* tp_descr_set */
2717 offsetof(textio, dict), /*tp_dictoffset*/ 2709 offsetof(textio, dict), /*tp_dictoffset*/
2718 (initproc)textiowrapper_init, /* tp_init */ 2710 (initproc)textiowrapper_init, /* tp_init */
2719 0, /* tp_alloc */ 2711 0, /* tp_alloc */
2720 PyType_GenericNew, /* tp_new */ 2712 PyType_GenericNew, /* tp_new */
2721 }; 2713 };
OLDNEW
« no previous file with comments | « Modules/_io/fileio.c ('k') | Modules/md5module.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+