[issue1152248] Add support for reading records with arbitrary separators to the standard IO stack

Akira Li report at bugs.python.org
Mon Jul 28 10:01:37 CEST 2014


Akira Li added the comment:

> Akira, your patch does this:
>
> -        self._writetranslate = newline != ''
> -        self._writenl = newline or os.linesep
> +        self._writetranslate = newline in (None, '\r', '\r\n')
> +        self._writenl = newline if newline is not None else os.linesep
>
> Any reason you made the second change? Why change the value assigned
> to _writenl for newline='\n' when you don't want to actually change
> the behavior for those cases? Just so you can double-check at write
> time that _writetranslate is never set unless _writenl is '\r',
> \r\n', or os.linesep?

If newline='\n' then writenl is '\n' with and without the patch.
If newline='\n' then write('\n\r') writes '\n\r' with and without the
patch.

If newline='\n' then writetranslate=False (with the patch). It does not
change the result for newline='\n' as it is documented now [1]:

  [newline] can be None, '', '\n', '\r', and '\r\n'.
  ...
  If newline is any of the other legal values [namely '\r', '\n',
  '\r\n'], any '\n' characters written are translated to the given
  string.

[...] are added by me for clarity.

[1] https://docs.python.org/3.4/library/io.html#io.TextIOWrapper

writetranslate=False so that if newline='\0' then write('\0\n') would
write '\0\n' i.e., embed '\n' are not corrupted if newline='\0'. That is
why it is the "no translation" patch:

+    When writing output to the stream:
+
+    - if newline is None, any '\n' characters written are translated to
+      the system default line separator, os.linesep
+    - if newline is '\r' or '\r\n', any '\n' characters written are
+      translated to the given string
+    - no translation takes place for any other newline value [any string].

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1152248>
_______________________________________


More information about the Python-bugs-list mailing list