Commit 27bbcfb8 authored by R David Murray's avatar R David Murray

Merge #15543: glossary entry for and 'universal newlines', and links to it.

Patch by Chris Jerdonek.
parents 592df20e ee0a945a
...@@ -706,6 +706,13 @@ Glossary ...@@ -706,6 +706,13 @@ Glossary
object has a type. An object's type is accessible as its object has a type. An object's type is accessible as its
:attr:`__class__` attribute or can be retrieved with ``type(obj)``. :attr:`__class__` attribute or can be retrieved with ``type(obj)``.
universal newlines
A manner of interpreting text streams in which all of the following are
recognized as ending a line: the Unix end-of-line convention ``'\n'``,
the Windows convention ``'\r\n'``, and the old Macintosh convention
``'\r'``. See :pep:`278` and :pep:`3116`, as well as
:func:`str.splitlines` for an additional use.
view view
The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and
:meth:`dict.items` are called dictionary views. They are lazy sequences :meth:`dict.items` are called dictionary views. They are lazy sequences
......
...@@ -46,6 +46,9 @@ Module Contents ...@@ -46,6 +46,9 @@ Module Contents
The :mod:`csv` module defines the following functions: The :mod:`csv` module defines the following functions:
.. index::
single: universal newlines; csv.reader function
.. function:: reader(csvfile, dialect='excel', **fmtparams) .. function:: reader(csvfile, dialect='excel', **fmtparams)
Return a reader object which will iterate over lines in the given *csvfile*. Return a reader object which will iterate over lines in the given *csvfile*.
...@@ -486,4 +489,5 @@ done:: ...@@ -486,4 +489,5 @@ done::
.. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields .. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields
will not be interpreted correctly, and on platforms that use ``\r\n`` linendings will not be interpreted correctly, and on platforms that use ``\r\n`` linendings
on write an extra ``\r`` will be added. It should always be safe to specify on write an extra ``\r`` will be added. It should always be safe to specify
``newline=''``, since the csv module does its own (universal) newline handling. ``newline=''``, since the csv module does its own
(:term:`universal <universal newlines>`) newline handling.
...@@ -819,7 +819,7 @@ are always available. They are listed here in alphabetical order. ...@@ -819,7 +819,7 @@ are always available. They are listed here in alphabetical order.
``'b'`` binary mode ``'b'`` binary mode
``'t'`` text mode (default) ``'t'`` text mode (default)
``'+'`` open a disk file for updating (reading and writing) ``'+'`` open a disk file for updating (reading and writing)
``'U'`` universal newline mode (for backwards compatibility; should ``'U'`` universal newlines mode (for backwards compatibility; should
not be used in new code) not be used in new code)
========= =============================================================== ========= ===============================================================
...@@ -874,14 +874,17 @@ are always available. They are listed here in alphabetical order. ...@@ -874,14 +874,17 @@ are always available. They are listed here in alphabetical order.
used. Any other error handling name that has been registered with used. Any other error handling name that has been registered with
:func:`codecs.register_error` is also valid. :func:`codecs.register_error` is also valid.
*newline* controls how universal newlines works (it only applies to text .. index::
mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It single: universal newlines; open() built-in function
works as follows:
*newline* controls how :term:`universal newlines` mode works (it only
applies to text mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and
``'\r\n'``. It works as follows:
* When reading input from the stream, if *newline* is ``None``, universal * When reading input from the stream, if *newline* is ``None``, universal
newlines mode is enabled. Lines in the input can end in ``'\n'``, newlines mode is enabled. Lines in the input can end in ``'\n'``,
``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` before ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` before
being returned to the caller. If it is ``''``, universal newline mode is being returned to the caller. If it is ``''``, universal newlines mode is
enabled, but line endings are returned to the caller untranslated. If it enabled, but line endings are returned to the caller untranslated. If it
has any of the other legal values, input lines are only terminated by the has any of the other legal values, input lines are only terminated by the
given string, and the line ending is returned to the caller untranslated. given string, and the line ending is returned to the caller untranslated.
......
...@@ -290,12 +290,16 @@ ABC hierarchy:: ...@@ -290,12 +290,16 @@ ABC hierarchy::
(e.g. built-in module). :exc:`ImportError` is raised if loader cannot (e.g. built-in module). :exc:`ImportError` is raised if loader cannot
find the requested module. find the requested module.
.. index::
single: universal newlines; importlib.abc.InspectLoader.get_source method
.. method:: get_source(fullname) .. method:: get_source(fullname)
An abstract method to return the source of a module. It is returned as An abstract method to return the source of a module. It is returned as
a text string with universal newlines. Returns ``None`` if no a text string using :term:`universal newlines`, translating all
source is available (e.g. a built-in module). Raises :exc:`ImportError` recognized line separators into ``'\n'`` characters. Returns ``None``
if the loader cannot find the module specified. if no source is available (e.g. a built-in module). Raises
:exc:`ImportError` if the loader cannot find the module specified.
.. method:: is_package(fullname) .. method:: is_package(fullname)
......
...@@ -768,16 +768,20 @@ Text I/O ...@@ -768,16 +768,20 @@ Text I/O
sequences) can be used. Any other error handling name that has been sequences) can be used. Any other error handling name that has been
registered with :func:`codecs.register_error` is also valid. registered with :func:`codecs.register_error` is also valid.
.. index::
single: universal newlines; io.TextIOWrapper class
*newline* controls how line endings are handled. It can be ``None``, *newline* controls how line endings are handled. It can be ``None``,
``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows: ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It works as follows:
* When reading input from the stream, if *newline* is ``None``, universal * When reading input from the stream, if *newline* is ``None``,
newlines mode is enabled. Lines in the input can end in ``'\n'``, :term:`universal newlines` mode is enabled. Lines in the input can end in
``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'`` before ``'\n'``, ``'\r'``, or ``'\r\n'``, and these are translated into ``'\n'``
being returned to the caller. If it is ``''``, universal newline mode is before being returned to the caller. If it is ``''``, universal newlines
enabled, but line endings are returned to the caller untranslated. If it mode is enabled, but line endings are returned to the caller untranslated.
has any of the other legal values, input lines are only terminated by the If it has any of the other legal values, input lines are only terminated
given string, and the line ending is returned to the caller untranslated. by the given string, and the line ending is returned to the caller
untranslated.
* When writing output to the stream, if *newline* is ``None``, any ``'\n'`` * When writing output to the stream, if *newline* is ``None``, any ``'\n'``
characters written are translated to the system default line separator, characters written are translated to the system default line separator,
...@@ -843,10 +847,13 @@ Text I/O ...@@ -843,10 +847,13 @@ Text I/O
output.close() output.close()
.. index::
single: universal newlines; io.IncrementalNewlineDecoder class
.. class:: IncrementalNewlineDecoder .. class:: IncrementalNewlineDecoder
A helper codec that decodes newlines for universal newlines mode. It A helper codec that decodes newlines for :term:`universal newlines` mode.
inherits :class:`codecs.IncrementalDecoder`. It inherits :class:`codecs.IncrementalDecoder`.
Performance Performance
......
...@@ -1349,10 +1349,13 @@ functions based on regular expressions. ...@@ -1349,10 +1349,13 @@ functions based on regular expressions.
``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``. ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
.. index::
single: universal newlines; str.splitlines method
.. method:: str.splitlines([keepends]) .. method:: str.splitlines([keepends])
Return a list of the lines in the string, breaking at line boundaries. Return a list of the lines in the string, breaking at line boundaries.
This method uses the universal newlines approach to splitting lines. This method uses the :term:`universal newlines` approach to splitting lines.
Line breaks are not included in the resulting list unless *keepends* is Line breaks are not included in the resulting list unless *keepends* is
given and true. given and true.
......
...@@ -285,9 +285,12 @@ default values. The arguments that are most commonly needed are: ...@@ -285,9 +285,12 @@ default values. The arguments that are most commonly needed are:
:data:`STDOUT`, which indicates that the stderr data from the child :data:`STDOUT`, which indicates that the stderr data from the child
process should be captured into the same file handle as for *stdout*. process should be captured into the same file handle as for *stdout*.
.. index::
single: universal newlines; subprocess module
If *universal_newlines* is ``True``, the file objects *stdin*, *stdout* If *universal_newlines* is ``True``, the file objects *stdin*, *stdout*
and *stderr* will be opened as text streams with universal newlines support, and *stderr* will be opened as text streams in :term:`universal newlines`
using the encoding returned by :func:`locale.getpreferredencoding`. mode using the encoding returned by :func:`locale.getpreferredencoding`.
For *stdin*, line ending characters ``'\n'`` in the input will be converted For *stdin*, line ending characters ``'\n'`` in the input will be converted
to the default line separator :data:`os.linesep`. For *stdout* and to the default line separator :data:`os.linesep`. For *stdout* and
*stderr*, all line endings in the output will be converted to ``'\n'``. *stderr*, all line endings in the output will be converted to ``'\n'``.
...@@ -508,7 +511,7 @@ functions. ...@@ -508,7 +511,7 @@ functions.
.. _side-by-side assembly: http://en.wikipedia.org/wiki/Side-by-Side_Assembly .. _side-by-side assembly: http://en.wikipedia.org/wiki/Side-by-Side_Assembly
If *universal_newlines* is ``True``, the file objects *stdin*, *stdout* If *universal_newlines* is ``True``, the file objects *stdin*, *stdout*
and *stderr* are opened as text files with universal newlines support, as and *stderr* are opened as text streams in universal newlines mode, as
described above in :ref:`frequently-used-arguments`. described above in :ref:`frequently-used-arguments`.
If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is
......
...@@ -197,14 +197,18 @@ ZipFile Objects ...@@ -197,14 +197,18 @@ ZipFile Objects
Return a list of archive members by name. Return a list of archive members by name.
.. index::
single: universal newlines; zipfile.ZipFile.open method
.. method:: ZipFile.open(name, mode='r', pwd=None) .. method:: ZipFile.open(name, mode='r', pwd=None)
Extract a member from the archive as a file-like object (ZipExtFile). *name* is Extract a member from the archive as a file-like object (ZipExtFile). *name*
the name of the file in the archive, or a :class:`ZipInfo` object. The *mode* is the name of the file in the archive, or a :class:`ZipInfo` object. The
parameter, if included, must be one of the following: ``'r'`` (the default), *mode* parameter, if included, must be one of the following: ``'r'`` (the
``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable universal newline default), ``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable
support in the read-only object. *pwd* is the password used for encrypted files. :term:`universal newlines` support in the read-only object. *pwd* is the
Calling :meth:`open` on a closed ZipFile will raise a :exc:`RuntimeError`. password used for encrypted files. Calling :meth:`open` on a closed
ZipFile will raise a :exc:`RuntimeError`.
.. note:: .. note::
......
...@@ -366,6 +366,9 @@ Under MacOS, :func:`os.listdir` may now return Unicode filenames. ...@@ -366,6 +366,9 @@ Under MacOS, :func:`os.listdir` may now return Unicode filenames.
.. ====================================================================== .. ======================================================================
.. index::
single: universal newlines; What's new
PEP 278: Universal Newline Support PEP 278: Universal Newline Support
================================== ==================================
...@@ -376,12 +379,12 @@ mark the ends of lines in text files. Unix uses the linefeed (ASCII character ...@@ -376,12 +379,12 @@ mark the ends of lines in text files. Unix uses the linefeed (ASCII character
10), MacOS uses the carriage return (ASCII character 13), and Windows uses a 10), MacOS uses the carriage return (ASCII character 13), and Windows uses a
two-character sequence of a carriage return plus a newline. two-character sequence of a carriage return plus a newline.
Python's file objects can now support end of line conventions other than the one Python's file objects can now support end of line conventions other than the
followed by the platform on which Python is running. Opening a file with the one followed by the platform on which Python is running. Opening a file with
mode ``'U'`` or ``'rU'`` will open a file for reading in universal newline mode. the mode ``'U'`` or ``'rU'`` will open a file for reading in :term:`universal
All three line ending conventions will be translated to a ``'\n'`` in the newlines` mode. All three line ending conventions will be translated to a
strings returned by the various file methods such as :meth:`read` and ``'\n'`` in the strings returned by the various file methods such as
:meth:`readline`. :meth:`read` and :meth:`readline`.
Universal newline support is also used when importing modules and when executing Universal newline support is also used when importing modules and when executing
a file with the :func:`execfile` function. This means that Python modules can a file with the :func:`execfile` function. This means that Python modules can
......
...@@ -411,6 +411,9 @@ error streams will be. You can provide a file object or a file descriptor, or ...@@ -411,6 +411,9 @@ error streams will be. You can provide a file object or a file descriptor, or
you can use the constant ``subprocess.PIPE`` to create a pipe between the you can use the constant ``subprocess.PIPE`` to create a pipe between the
subprocess and the parent. subprocess and the parent.
.. index::
single: universal newlines; What's new
The constructor has a number of handy options: The constructor has a number of handy options:
* *close_fds* requests that all file descriptors be closed before running the * *close_fds* requests that all file descriptors be closed before running the
...@@ -424,7 +427,7 @@ The constructor has a number of handy options: ...@@ -424,7 +427,7 @@ The constructor has a number of handy options:
* *preexec_fn* is a function that gets called before the child is started. * *preexec_fn* is a function that gets called before the child is started.
* *universal_newlines* opens the child's input and output using Python's * *universal_newlines* opens the child's input and output using Python's
universal newline feature. :term:`universal newlines` feature.
Once you've created the :class:`Popen` instance, you can call its :meth:`wait` Once you've created the :class:`Popen` instance, you can call its :meth:`wait`
method to pause until the subprocess has exited, :meth:`poll` to check if it's method to pause until the subprocess has exited, :meth:`poll` to check if it's
......
...@@ -1338,13 +1338,17 @@ complete list of changes, or look through the SVN logs for all the details. ...@@ -1338,13 +1338,17 @@ complete list of changes, or look through the SVN logs for all the details.
.. XXX need to provide some more detail here .. XXX need to provide some more detail here
.. index::
single: universal newlines; What's new
* The :mod:`fileinput` module was made more flexible. Unicode filenames are now * The :mod:`fileinput` module was made more flexible. Unicode filenames are now
supported, and a *mode* parameter that defaults to ``"r"`` was added to the supported, and a *mode* parameter that defaults to ``"r"`` was added to the
:func:`input` function to allow opening files in binary or universal-newline :func:`input` function to allow opening files in binary or :term:`universal
mode. Another new parameter, *openhook*, lets you use a function other than newlines` mode. Another new parameter, *openhook*, lets you use a function
:func:`open` to open the input files. Once you're iterating over the set of other than :func:`open` to open the input files. Once you're iterating over
files, the :class:`FileInput` object's new :meth:`fileno` returns the file the set of files, the :class:`FileInput` object's new :meth:`fileno` returns
descriptor for the currently opened file. (Contributed by Georg Brandl.) the file descriptor for the currently opened file. (Contributed by Georg
Brandl.)
* In the :mod:`gc` module, the new :func:`get_count` function returns a 3-tuple * In the :mod:`gc` module, the new :func:`get_count` function returns a 3-tuple
containing the current collection counts for the three GC generations. This is containing the current collection counts for the three GC generations. This is
......
...@@ -1071,9 +1071,12 @@ the :mod:`io` module: ...@@ -1071,9 +1071,12 @@ the :mod:`io` module:
The :class:`BytesIO` class supports reading, writing, and seeking The :class:`BytesIO` class supports reading, writing, and seeking
over an in-memory buffer. over an in-memory buffer.
.. index::
single: universal newlines; What's new
* :class:`TextIOBase`: Provides functions for reading and writing * :class:`TextIOBase`: Provides functions for reading and writing
strings (remember, strings will be Unicode in Python 3.0), strings (remember, strings will be Unicode in Python 3.0),
and supporting universal newlines. :class:`TextIOBase` defines and supporting :term:`universal newlines`. :class:`TextIOBase` defines
the :meth:`readline` method and supports iteration upon the :meth:`readline` method and supports iteration upon
objects. objects.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment