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

Skip to content

Commit 27bbcfb

Browse files
committed
Merge #15543: glossary entry for and 'universal newlines', and links to it.
Patch by Chris Jerdonek.
2 parents 592df20 + ee0a945 commit 27bbcfb

12 files changed

Lines changed: 89 additions & 41 deletions

File tree

Doc/glossary.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,13 @@ Glossary
706706
object has a type. An object's type is accessible as its
707707
:attr:`__class__` attribute or can be retrieved with ``type(obj)``.
708708

709+
universal newlines
710+
A manner of interpreting text streams in which all of the following are
711+
recognized as ending a line: the Unix end-of-line convention ``'\n'``,
712+
the Windows convention ``'\r\n'``, and the old Macintosh convention
713+
``'\r'``. See :pep:`278` and :pep:`3116`, as well as
714+
:func:`str.splitlines` for an additional use.
715+
709716
view
710717
The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and
711718
:meth:`dict.items` are called dictionary views. They are lazy sequences

Doc/library/csv.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Module Contents
4646
The :mod:`csv` module defines the following functions:
4747

4848

49+
.. index::
50+
single: universal newlines; csv.reader function
51+
4952
.. function:: reader(csvfile, dialect='excel', **fmtparams)
5053

5154
Return a reader object which will iterate over lines in the given *csvfile*.
@@ -486,4 +489,5 @@ done::
486489
.. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields
487490
will not be interpreted correctly, and on platforms that use ``\r\n`` linendings
488491
on write an extra ``\r`` will be added. It should always be safe to specify
489-
``newline=''``, since the csv module does its own (universal) newline handling.
492+
``newline=''``, since the csv module does its own
493+
(:term:`universal <universal newlines>`) newline handling.

Doc/library/functions.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ are always available. They are listed here in alphabetical order.
819819
``'b'`` binary mode
820820
``'t'`` text mode (default)
821821
``'+'`` open a disk file for updating (reading and writing)
822-
``'U'`` universal newline mode (for backwards compatibility; should
822+
``'U'`` universal newlines mode (for backwards compatibility; should
823823
not be used in new code)
824824
========= ===============================================================
825825

@@ -874,14 +874,17 @@ are always available. They are listed here in alphabetical order.
874874
used. Any other error handling name that has been registered with
875875
:func:`codecs.register_error` is also valid.
876876

877-
*newline* controls how universal newlines works (it only applies to text
878-
mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
879-
works as follows:
877+
.. index::
878+
single: universal newlines; open() built-in function
879+
880+
*newline* controls how :term:`universal newlines` mode works (it only
881+
applies to text mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and
882+
``'\r\n'``. It works as follows:
880883

881884
* When reading input from the stream, if *newline* is ``None``, universal
882885
newlines mode is enabled. Lines in the input can end in ``'\n'``,
883886
``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` before
884-
being returned to the caller. If it is ``''``, universal newline mode is
887+
being returned to the caller. If it is ``''``, universal newlines mode is
885888
enabled, but line endings are returned to the caller untranslated. If it
886889
has any of the other legal values, input lines are only terminated by the
887890
given string, and the line ending is returned to the caller untranslated.

Doc/library/importlib.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,16 @@ ABC hierarchy::
290290
(e.g. built-in module). :exc:`ImportError` is raised if loader cannot
291291
find the requested module.
292292

293+
.. index::
294+
single: universal newlines; importlib.abc.InspectLoader.get_source method
295+
293296
.. method:: get_source(fullname)
294297

295298
An abstract method to return the source of a module. It is returned as
296-
a text string with universal newlines. Returns ``None`` if no
297-
source is available (e.g. a built-in module). Raises :exc:`ImportError`
298-
if the loader cannot find the module specified.
299+
a text string using :term:`universal newlines`, translating all
300+
recognized line separators into ``'\n'`` characters. Returns ``None``
301+
if no source is available (e.g. a built-in module). Raises
302+
:exc:`ImportError` if the loader cannot find the module specified.
299303

300304
.. method:: is_package(fullname)
301305

Doc/library/io.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,20 @@ Text I/O
768768
sequences) can be used. Any other error handling name that has been
769769
registered with :func:`codecs.register_error` is also valid.
770770

771+
.. index::
772+
single: universal newlines; io.TextIOWrapper class
773+
771774
*newline* controls how line endings are handled. It can be ``None``,
772775
``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
773776

774-
* When reading input from the stream, if *newline* is ``None``, universal
775-
newlines mode is enabled. Lines in the input can end in ``'\n'``,
776-
``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` before
777-
being returned to the caller. If it is ``''``, universal newline mode is
778-
enabled, but line endings are returned to the caller untranslated. If it
779-
has any of the other legal values, input lines are only terminated by the
780-
given string, and the line ending is returned to the caller untranslated.
777+
* When reading input from the stream, if *newline* is ``None``,
778+
:term:`universal newlines` mode is enabled. Lines in the input can end in
779+
``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'``
780+
before being returned to the caller. If it is ``''``, universal newlines
781+
mode is enabled, but line endings are returned to the caller untranslated.
782+
If it has any of the other legal values, input lines are only terminated
783+
by the given string, and the line ending is returned to the caller
784+
untranslated.
781785

782786
* When writing output to the stream, if *newline* is ``None``, any ``'\n'``
783787
characters written are translated to the system default line separator,
@@ -843,10 +847,13 @@ Text I/O
843847
output.close()
844848

845849

850+
.. index::
851+
single: universal newlines; io.IncrementalNewlineDecoder class
852+
846853
.. class:: IncrementalNewlineDecoder
847854

848-
A helper codec that decodes newlines for universal newlines mode. It
849-
inherits :class:`codecs.IncrementalDecoder`.
855+
A helper codec that decodes newlines for :term:`universal newlines` mode.
856+
It inherits :class:`codecs.IncrementalDecoder`.
850857

851858

852859
Performance

Doc/library/stdtypes.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,10 +1349,13 @@ functions based on regular expressions.
13491349
``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
13501350

13511351

1352+
.. index::
1353+
single: universal newlines; str.splitlines method
1354+
13521355
.. method:: str.splitlines([keepends])
13531356

13541357
Return a list of the lines in the string, breaking at line boundaries.
1355-
This method uses the universal newlines approach to splitting lines.
1358+
This method uses the :term:`universal newlines` approach to splitting lines.
13561359
Line breaks are not included in the resulting list unless *keepends* is
13571360
given and true.
13581361

Doc/library/subprocess.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,12 @@ default values. The arguments that are most commonly needed are:
285285
:data:`STDOUT`, which indicates that the stderr data from the child
286286
process should be captured into the same file handle as for *stdout*.
287287

288+
.. index::
289+
single: universal newlines; subprocess module
290+
288291
If *universal_newlines* is ``True``, the file objects *stdin*, *stdout*
289-
and *stderr* will be opened as text streams with universal newlines support,
290-
using the encoding returned by :func:`locale.getpreferredencoding`.
292+
and *stderr* will be opened as text streams in :term:`universal newlines`
293+
mode using the encoding returned by :func:`locale.getpreferredencoding`.
291294
For *stdin*, line ending characters ``'\n'`` in the input will be converted
292295
to the default line separator :data:`os.linesep`. For *stdout* and
293296
*stderr*, all line endings in the output will be converted to ``'\n'``.
@@ -508,7 +511,7 @@ functions.
508511
.. _side-by-side assembly: http://en.wikipedia.org/wiki/Side-by-Side_Assembly
509512

510513
If *universal_newlines* is ``True``, the file objects *stdin*, *stdout*
511-
and *stderr* are opened as text files with universal newlines support, as
514+
and *stderr* are opened as text streams in universal newlines mode, as
512515
described above in :ref:`frequently-used-arguments`.
513516

514517
If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is

Doc/library/zipfile.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,18 @@ ZipFile Objects
197197
Return a list of archive members by name.
198198

199199

200+
.. index::
201+
single: universal newlines; zipfile.ZipFile.open method
202+
200203
.. method:: ZipFile.open(name, mode='r', pwd=None)
201204

202-
Extract a member from the archive as a file-like object (ZipExtFile). *name* is
203-
the name of the file in the archive, or a :class:`ZipInfo` object. The *mode*
204-
parameter, if included, must be one of the following: ``'r'`` (the default),
205-
``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable universal newline
206-
support in the read-only object. *pwd* is the password used for encrypted files.
207-
Calling :meth:`open` on a closed ZipFile will raise a :exc:`RuntimeError`.
205+
Extract a member from the archive as a file-like object (ZipExtFile). *name*
206+
is the name of the file in the archive, or a :class:`ZipInfo` object. The
207+
*mode* parameter, if included, must be one of the following: ``'r'`` (the
208+
default), ``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable
209+
:term:`universal newlines` support in the read-only object. *pwd* is the
210+
password used for encrypted files. Calling :meth:`open` on a closed
211+
ZipFile will raise a :exc:`RuntimeError`.
208212

209213
.. note::
210214

Doc/whatsnew/2.3.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ Under MacOS, :func:`os.listdir` may now return Unicode filenames.
366366
.. ======================================================================
367367
368368
369+
.. index::
370+
single: universal newlines; What's new
371+
369372
PEP 278: Universal Newline Support
370373
==================================
371374

@@ -376,12 +379,12 @@ mark the ends of lines in text files. Unix uses the linefeed (ASCII character
376379
10), MacOS uses the carriage return (ASCII character 13), and Windows uses a
377380
two-character sequence of a carriage return plus a newline.
378381

379-
Python's file objects can now support end of line conventions other than the one
380-
followed by the platform on which Python is running. Opening a file with the
381-
mode ``'U'`` or ``'rU'`` will open a file for reading in universal newline mode.
382-
All three line ending conventions will be translated to a ``'\n'`` in the
383-
strings returned by the various file methods such as :meth:`read` and
384-
:meth:`readline`.
382+
Python's file objects can now support end of line conventions other than the
383+
one followed by the platform on which Python is running. Opening a file with
384+
the mode ``'U'`` or ``'rU'`` will open a file for reading in :term:`universal
385+
newlines` mode. All three line ending conventions will be translated to a
386+
``'\n'`` in the strings returned by the various file methods such as
387+
:meth:`read` and :meth:`readline`.
385388

386389
Universal newline support is also used when importing modules and when executing
387390
a file with the :func:`execfile` function. This means that Python modules can

Doc/whatsnew/2.4.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ error streams will be. You can provide a file object or a file descriptor, or
411411
you can use the constant ``subprocess.PIPE`` to create a pipe between the
412412
subprocess and the parent.
413413

414+
.. index::
415+
single: universal newlines; What's new
416+
414417
The constructor has a number of handy options:
415418

416419
* *close_fds* requests that all file descriptors be closed before running the
@@ -424,7 +427,7 @@ The constructor has a number of handy options:
424427
* *preexec_fn* is a function that gets called before the child is started.
425428

426429
* *universal_newlines* opens the child's input and output using Python's
427-
universal newline feature.
430+
:term:`universal newlines` feature.
428431

429432
Once you've created the :class:`Popen` instance, you can call its :meth:`wait`
430433
method to pause until the subprocess has exited, :meth:`poll` to check if it's

0 commit comments

Comments
 (0)