Fixes for bugs found by Tim Hatch.

This commit is contained in:
Georg Brandl 2007-08-10 21:25:06 +00:00
parent d06a0780b2
commit c62294f360
19 changed files with 151 additions and 129 deletions

View File

@ -66,6 +66,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Manus Hand
* Gerhard Häring
* Travis B. Hartwell
* Tim Hatch
* Janko Hauser
* Bernhard Herzog
* Magnus L. Hetland

View File

@ -162,10 +162,10 @@ The following data items and methods are also supported:
.. method:: array.fromunicode(s)
Extends this array with data from the given unicode string. The array must be a
type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
``array.fromstring(ustr.decode(enc))`` to append Unicode data to an array of
some other type.
Extends this array with data from the given unicode string. The array must
be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
``array.fromstring(unicodestring.encode(enc))`` to append Unicode data to an
array of some other type.
.. method:: array.index(x)
@ -244,13 +244,13 @@ When an array object is printed or converted to a string, it is represented as
``array(typecode, initializer)``. The *initializer* is omitted if the array is
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
list of numbers. The string is guaranteed to be able to be converted back to an
array with the same type and value using reverse quotes (``````), so long as the
array with the same type and value using :func:`eval`, so long as the
:func:`array` function has been imported using ``from array import array``.
Examples::
array('l')
array('c', 'hello world')
array('u', u'hello \textbackslash u2641')
array('u', u'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])

View File

@ -220,17 +220,18 @@ For simple text calendars this module provides the following functions.
.. function:: leapdays(y1, y2)
Returns the number of leap years in the range [*y1*...*y2*), where *y1* and *y2*
are years.
Returns the number of leap years in the range from *y1* to *y2* (exclusive),
where *y1* and *y2* are years.
.. versionchanged:: 2.0
This function didn't work for ranges spanning a century change in Python 1.5.2.
This function didn't work for ranges spanning a century change in Python
1.5.2.
.. function:: weekday(year, month, day)
Returns the day of the week (``0`` is Monday) for *year* (``1970``\ --...),
*month* (``1``\ --\ ``12``), *day* (``1``\ --\ ``31``).
Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
*month* (``1``--``12``), *day* (``1``--``31``).
.. function:: weekheader(n)

View File

@ -22,10 +22,10 @@ Future editions of the standard library may include balanced trees and
ordered dictionaries.
.. versionchanged:: 2.5
Added defaultdict.
Added :class:`defaultdict`.
.. versionchanged:: 2.6
Added NamedTuple.
Added :class:`NamedTuple`.
.. _deque-objects:
@ -42,10 +42,10 @@ ordered dictionaries.
Deques are a generalization of stacks and queues (the name is pronounced "deck"
and is short for "double-ended queue"). Deques support thread-safe, memory
efficient appends and pops from either side of the deque with approximately the
same ``O(1)`` performance in either direction.
same O(1) performance in either direction.
Though :class:`list` objects support similar operations, they are optimized for
fast fixed-length operations and incur ``O(n)`` memory movement costs for
fast fixed-length operations and incur O(n) memory movement costs for
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
position of the underlying data representation.
@ -195,16 +195,16 @@ A roundrobin task server can be built from a :class:`deque` using
:meth:`popleft` to select the current task and :meth:`append` to add it back to
the tasklist if the input stream is not exhausted::
def roundrobin(*iterables):
pending = deque(iter(i) for i in iterables)
while pending:
task = pending.popleft()
try:
yield task.next()
except StopIteration:
continue
pending.append(task)
>>> def roundrobin(*iterables):
... pending = deque(iter(i) for i in iterables)
... while pending:
... task = pending.popleft()
... try:
... yield task.next()
... except StopIteration:
... continue
... pending.append(task)
...
>>> for value in roundrobin('abc', 'd', 'efgh'):
... print value
@ -226,13 +226,13 @@ queue.
For example, building a balanced binary tree of nested lists entails reducing
two adjacent nodes into one by grouping them in a list::
def maketree(iterable):
d = deque(iterable)
while len(d) > 1:
pair = [d.popleft(), d.popleft()]
d.append(pair)
return list(d)
>>> def maketree(iterable):
... d = deque(iterable)
... while len(d) > 1:
... pair = [d.popleft(), d.popleft()]
... d.append(pair)
... return list(d)
...
>>> print maketree('abcdefgh')
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
@ -298,8 +298,8 @@ sequence of key-value pairs into a dictionary of lists::
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
d[k].append(v)
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
@ -313,8 +313,8 @@ simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
>>> d = {}
>>> for k, v in s:
d.setdefault(k, []).append(v)
... d.setdefault(k, []).append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
@ -325,8 +325,8 @@ languages)::
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
d[k] += 1
... d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
@ -352,8 +352,8 @@ Setting the :attr:`default_factory` to :class:`set` makes the
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>> d = defaultdict(set)
>>> for k, v in s:
d[k].add(v)
... d[k].add(v)
...
>>> d.items()
[('blue', set([2, 4])), ('red', set([1, 3]))]

View File

@ -44,7 +44,7 @@ The :mod:`pprint` module defines one class:
controlled by *depth*; if the data structure being printed is too deep, the next
contained level is replaced by ``...``. By default, there is no constraint on
the depth of the objects being formatted. The desired output width is
constrained using the *width* parameter; the default is eighty characters. If a
constrained using the *width* parameter; the default is 80 characters. If a
structure cannot be formatted within the constrained width, a best effort will
be made. ::

View File

@ -75,7 +75,7 @@ See the source code for details. The public methods are:
ignored in that case).
.. versionadded:: 2.3
the timeout parameter.
The *timeout* parameter.
.. method:: Queue.put_nowait(item)
@ -93,7 +93,7 @@ See the source code for details. The public methods are:
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
.. versionadded:: 2.3
the timeout parameter.
The *timeout* parameter.
.. method:: Queue.get_nowait()

View File

@ -101,11 +101,11 @@ which format specific object types.
with ``level - 1`` for the value of *level* in the recursive call.
.. method:: Repr.repr_type(obj, level)
.. method:: Repr.repr_TYPE(obj, level)
:noindex:
Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, *type* is replaced by
based on the type name. In the method name, **TYPE** is replaced by
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
methods is handled by :meth:`repr1`. Type-specific methods which need to
recursively format a value should call ``self.repr1(subobj, level - 1)``.

View File

@ -88,16 +88,16 @@ operations:
| ``s.issuperset(t)`` | ``s >= t`` | test whether every element in |
| | | *t* is in *s* |
+-------------------------------+------------+---------------------------------+
| ``s.union(t)`` | *s* \| *t* | new set with elements from both |
| ``s.union(t)`` | ``s | t`` | new set with elements from both |
| | | *s* and *t* |
+-------------------------------+------------+---------------------------------+
| ``s.intersection(t)`` | *s* & *t* | new set with elements common to |
| ``s.intersection(t)`` | ``s & t`` | new set with elements common to |
| | | *s* and *t* |
+-------------------------------+------------+---------------------------------+
| ``s.difference(t)`` | *s* - *t* | new set with elements in *s* |
| ``s.difference(t)`` | ``s - t`` | new set with elements in *s* |
| | | but not in *t* |
+-------------------------------+------------+---------------------------------+
| ``s.symmetric_difference(t)`` | *s* ^ *t* | new set with elements in either |
| ``s.symmetric_difference(t)`` | ``s ^ t`` | new set with elements in either |
| | | *s* or *t* but not both |
+-------------------------------+------------+---------------------------------+
| ``s.copy()`` | | new set with a shallow copy of |
@ -277,7 +277,7 @@ lessons learned from the :mod:`sets` module. The key differences are:
* The built-in versions do not have a :meth:`union_update` method. Instead, use
the :meth:`update` method which is equivalent.
* The built-in versions do not have a :meth:`_repr(sorted=True)` method.
* The built-in versions do not have a ``_repr(sorted=True)`` method.
Instead, use the built-in :func:`repr` and :func:`sorted` functions:
``repr(sorted(s))``.

View File

@ -46,30 +46,32 @@ The module defines the following names:
.. index:: builtin: type
The type of type objects (such as returned by :func:`type`).
The type of type objects (such as returned by :func:`type`); alias of the
built-in :class:`type`.
.. data:: BooleanType
The type of the :class:`bool` values ``True`` and ``False``; this is an alias of
the built-in :func:`bool` function.
The type of the :class:`bool` values ``True`` and ``False``; alias of the
built-in :class:`bool`.
.. versionadded:: 2.3
.. data:: IntType
The type of integers (e.g. ``1``).
The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
.. data:: LongType
The type of long integers (e.g. ``1L``).
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
.. data:: FloatType
The type of floating point numbers (e.g. ``1.0``).
The type of floating point numbers (e.g. ``1.0``); alias of the built-in
:class:`float`.
.. data:: ComplexType
@ -80,28 +82,33 @@ The module defines the following names:
.. data:: StringType
The type of character strings (e.g. ``'Spam'``).
The type of character strings (e.g. ``'Spam'``); alias of the built-in
:class:`str`.
.. data:: UnicodeType
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
if Python was built without Unicode support.
if Python was built without Unicode support. It's an alias of the built-in
:class:`unicode`.
.. data:: TupleType
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``).
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
:class:`tuple`.
.. data:: ListType
The type of lists (e.g. ``[0, 1, 2, 3]``).
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
:class:`list`.
.. data:: DictType
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``).
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
built-in :class:`dict`.
.. data:: DictionaryType
@ -171,21 +178,24 @@ The module defines the following names:
.. data:: FileType
The type of open file objects such as ``sys.stdout``.
The type of open file objects such as ``sys.stdout``; alias of the built-in
:class:`file`.
.. data:: XRangeType
.. index:: builtin: xrange
The type of range objects returned by :func:`xrange`.
The type of range objects returned by :func:`xrange`; alias of the built-in
:class:`xrange`.
.. data:: SliceType
.. index:: builtin: slice
The type of objects returned by :func:`slice`.
The type of objects returned by :func:`slice`; alias of the built-in
:class:`slice`.
.. data:: EllipsisType
@ -250,4 +260,3 @@ The module defines the following names:
example: ``isinstance(s, types.StringTypes)``.
.. versionadded:: 2.2

View File

@ -95,8 +95,8 @@ The :mod:`UserList` module defines the :class:`UserList` class:
Class that simulates a list. The instance's contents are kept in a regular
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
instances. The instance's contents are initially set to a copy of *list*,
defaulting to the empty list ``[]``. *list* can be either a regular Python
list, or an instance of :class:`UserList` (or a subclass).
defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a
real Python list or a :class:`UserList` object.
In addition to supporting the methods and operations of mutable sequences (see
section :ref:`typesseq`), :class:`UserList` instances provide the following

View File

@ -66,6 +66,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Manus Hand
* Gerhard Häring
* Travis B. Hartwell
* Tim Hatch
* Janko Hauser
* Bernhard Herzog
* Magnus L. Hetland

View File

@ -162,10 +162,10 @@ The following data items and methods are also supported:
.. method:: array.fromunicode(s)
Extends this array with data from the given unicode string. The array must be a
type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
``array.fromstring(ustr.decode(enc))`` to append Unicode data to an array of
some other type.
Extends this array with data from the given unicode string. The array must
be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
``array.fromstring(unicodestring.encode(enc))`` to append Unicode data to an
array of some other type.
.. method:: array.index(x)
@ -244,13 +244,13 @@ When an array object is printed or converted to a string, it is represented as
``array(typecode, initializer)``. The *initializer* is omitted if the array is
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
list of numbers. The string is guaranteed to be able to be converted back to an
array with the same type and value using reverse quotes (``````), so long as the
array with the same type and value using :func:`eval`, so long as the
:func:`array` function has been imported using ``from array import array``.
Examples::
array('l')
array('c', 'hello world')
array('u', u'hello \textbackslash u2641')
array('u', u'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])

View File

@ -220,17 +220,18 @@ For simple text calendars this module provides the following functions.
.. function:: leapdays(y1, y2)
Returns the number of leap years in the range [*y1*...*y2*), where *y1* and *y2*
are years.
Returns the number of leap years in the range from *y1* to *y2* (exclusive),
where *y1* and *y2* are years.
.. versionchanged:: 2.0
This function didn't work for ranges spanning a century change in Python 1.5.2.
This function didn't work for ranges spanning a century change in Python
1.5.2.
.. function:: weekday(year, month, day)
Returns the day of the week (``0`` is Monday) for *year* (``1970``\ --...),
*month* (``1``\ --\ ``12``), *day* (``1``\ --\ ``31``).
Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
*month* (``1``--``12``), *day* (``1``--``31``).
.. function:: weekheader(n)

View File

@ -22,10 +22,10 @@ Future editions of the standard library may include balanced trees and
ordered dictionaries.
.. versionchanged:: 2.5
Added defaultdict.
Added :class:`defaultdict`.
.. versionchanged:: 2.6
Added NamedTuple.
Added :class:`NamedTuple`.
.. _deque-objects:
@ -42,10 +42,10 @@ ordered dictionaries.
Deques are a generalization of stacks and queues (the name is pronounced "deck"
and is short for "double-ended queue"). Deques support thread-safe, memory
efficient appends and pops from either side of the deque with approximately the
same ``O(1)`` performance in either direction.
same O(1) performance in either direction.
Though :class:`list` objects support similar operations, they are optimized for
fast fixed-length operations and incur ``O(n)`` memory movement costs for
fast fixed-length operations and incur O(n) memory movement costs for
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
position of the underlying data representation.
@ -195,16 +195,16 @@ A roundrobin task server can be built from a :class:`deque` using
:meth:`popleft` to select the current task and :meth:`append` to add it back to
the tasklist if the input stream is not exhausted::
def roundrobin(*iterables):
pending = deque(iter(i) for i in iterables)
while pending:
task = pending.popleft()
try:
yield next(task)
except StopIteration:
continue
pending.append(task)
>>> def roundrobin(*iterables):
... pending = deque(iter(i) for i in iterables)
... while pending:
... task = pending.popleft()
... try:
... yield next(task)
... except StopIteration:
... continue
... pending.append(task)
...
>>> for value in roundrobin('abc', 'd', 'efgh'):
... print value
@ -226,13 +226,13 @@ queue.
For example, building a balanced binary tree of nested lists entails reducing
two adjacent nodes into one by grouping them in a list::
def maketree(iterable):
d = deque(iterable)
while len(d) > 1:
pair = [d.popleft(), d.popleft()]
d.append(pair)
return list(d)
>>> def maketree(iterable):
... d = deque(iterable)
... while len(d) > 1:
... pair = [d.popleft(), d.popleft()]
... d.append(pair)
... return list(d)
...
>>> print maketree('abcdefgh')
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
@ -298,8 +298,8 @@ sequence of key-value pairs into a dictionary of lists::
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
d[k].append(v)
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
@ -313,8 +313,8 @@ simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
>>> d = {}
>>> for k, v in s:
d.setdefault(k, []).append(v)
... d.setdefault(k, []).append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
@ -325,8 +325,8 @@ languages)::
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
d[k] += 1
... d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
@ -352,8 +352,8 @@ Setting the :attr:`default_factory` to :class:`set` makes the
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>> d = defaultdict(set)
>>> for k, v in s:
d[k].add(v)
... d[k].add(v)
...
>>> d.items()
[('blue', set([2, 4])), ('red', set([1, 3]))]

View File

@ -44,7 +44,7 @@ The :mod:`pprint` module defines one class:
controlled by *depth*; if the data structure being printed is too deep, the next
contained level is replaced by ``...``. By default, there is no constraint on
the depth of the objects being formatted. The desired output width is
constrained using the *width* parameter; the default is eighty characters. If a
constrained using the *width* parameter; the default is 80 characters. If a
structure cannot be formatted within the constrained width, a best effort will
be made. ::

View File

@ -75,7 +75,7 @@ See the source code for details. The public methods are:
ignored in that case).
.. versionadded:: 2.3
the timeout parameter.
The *timeout* parameter.
.. method:: Queue.put_nowait(item)
@ -93,7 +93,7 @@ See the source code for details. The public methods are:
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
.. versionadded:: 2.3
the timeout parameter.
The *timeout* parameter.
.. method:: Queue.get_nowait()

View File

@ -101,11 +101,11 @@ which format specific object types.
with ``level - 1`` for the value of *level* in the recursive call.
.. method:: Repr.repr_type(obj, level)
.. method:: Repr.repr_TYPE(obj, level)
:noindex:
Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, *type* is replaced by
based on the type name. In the method name, **TYPE** is replaced by
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
methods is handled by :meth:`repr1`. Type-specific methods which need to
recursively format a value should call ``self.repr1(subobj, level - 1)``.

View File

@ -46,30 +46,32 @@ The module defines the following names:
.. index:: builtin: type
The type of type objects (such as returned by :func:`type`).
The type of type objects (such as returned by :func:`type`); alias of the
built-in :class:`type`.
.. data:: BooleanType
The type of the :class:`bool` values ``True`` and ``False``; this is an alias of
the built-in :func:`bool` function.
The type of the :class:`bool` values ``True`` and ``False``; alias of the
built-in :class:`bool`.
.. versionadded:: 2.3
.. data:: IntType
The type of integers (e.g. ``1``).
The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
.. data:: LongType
The type of long integers (e.g. ``1L``).
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
.. data:: FloatType
The type of floating point numbers (e.g. ``1.0``).
The type of floating point numbers (e.g. ``1.0``); alias of the built-in
:class:`float`.
.. data:: ComplexType
@ -80,28 +82,33 @@ The module defines the following names:
.. data:: StringType
The type of character strings (e.g. ``'Spam'``).
The type of character strings (e.g. ``'Spam'``); alias of the built-in
:class:`str`.
.. data:: UnicodeType
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
if Python was built without Unicode support.
if Python was built without Unicode support. It's an alias of the built-in
:class:`unicode`.
.. data:: TupleType
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``).
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
:class:`tuple`.
.. data:: ListType
The type of lists (e.g. ``[0, 1, 2, 3]``).
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
:class:`list`.
.. data:: DictType
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``).
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
built-in :class:`dict`.
.. data:: DictionaryType
@ -166,21 +173,24 @@ The module defines the following names:
.. data:: FileType
The type of open file objects such as ``sys.stdout``.
The type of open file objects such as ``sys.stdout``; alias of the built-in
:class:`file`.
.. data:: RangeType
.. index:: builtin: range
The type of range objects returned by :func:`range`.
The type of range objects returned by :func:`range`; alias of the built-in
:class:`range`.
.. data:: SliceType
.. index:: builtin: slice
The type of objects returned by :func:`slice`.
The type of objects returned by :func:`slice`; alias of the built-in
:class:`slice`.
.. data:: EllipsisType
@ -245,4 +255,3 @@ The module defines the following names:
example: ``isinstance(s, types.StringTypes)``.
.. versionadded:: 2.2

View File

@ -95,8 +95,8 @@ The :mod:`UserList` module defines the :class:`UserList` class:
Class that simulates a list. The instance's contents are kept in a regular
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
instances. The instance's contents are initially set to a copy of *list*,
defaulting to the empty list ``[]``. *list* can be either a regular Python
list, or an instance of :class:`UserList` (or a subclass).
defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a
real Python list or a :class:`UserList` object.
In addition to supporting the methods and operations of mutable sequences (see
section :ref:`typesseq`), :class:`UserList` instances provide the following