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

Skip to content

Commit cfad543

Browse files
committed
Issue #22413: Document newline effect on StringIO initializer and getvalue
Also add to comment in the C code.
1 parent e44dba3 commit cfad543

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

Doc/library/io.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,17 +864,25 @@ Text I/O
864864
An in-memory stream for text I/O. The text buffer is discarded when the
865865
:meth:`~IOBase.close` method is called.
866866

867-
The initial value of the buffer (an empty string by default) can be set by
868-
providing *initial_value*. The *newline* argument works like that of
869-
:class:`TextIOWrapper`. The default is to consider only ``\n`` characters
870-
as end of lines and to do no newline translation.
867+
The initial value of the buffer can be set by providing *initial_value*.
868+
If newline translation is enabled, newlines will be encoded as if by
869+
:meth:`~TextIOBase.write`. The stream is positioned at the start of
870+
the buffer.
871+
872+
The *newline* argument works like that of :class:`TextIOWrapper`.
873+
The default is to consider only ``\n`` characters as ends of lines and
874+
to do no newline translation. If *newline* is set to ``None``,
875+
newlines are written as ``\n`` on all platforms, but universal
876+
newline decoding is still performed when reading.
871877

872878
:class:`StringIO` provides this method in addition to those from
873879
:class:`TextIOBase` and its parents:
874880

875881
.. method:: getvalue()
876882

877883
Return a ``str`` containing the entire contents of the buffer.
884+
Newlines are decoded as if by :meth:`~TextIOBase.read`, although
885+
the stream position is not changed.
878886

879887
Example usage::
880888

Modules/_io/_iomodule.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ extern PyObject *_PyIncrementalNewlineDecoder_decode(
5252
which can be safely put aside until another search.
5353
5454
NOTE: for performance reasons, `end` must point to a NUL character ('\0').
55-
Otherwise, the function will scan further and return garbage. */
55+
Otherwise, the function will scan further and return garbage.
56+
57+
There are three modes, in order of priority:
58+
* translated: Only find \n (assume newlines already translated)
59+
* universal: Use universal newlines algorithm
60+
* Otherwise, the line ending is specified by readnl, a str object */
5661
extern Py_ssize_t _PyIO_find_line_ending(
5762
int translated, int universal, PyObject *readnl,
5863
int kind, char *start, char *end, Py_ssize_t *consumed);

0 commit comments

Comments
 (0)