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

Skip to content

Commit 2cc30da

Browse files
committed
Merged revisions 58742-58816 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r58745 | georg.brandl | 2007-11-01 10:19:33 -0700 (Thu, 01 Nov 2007) | 2 lines #1364: os.lstat is available on Windows too, as an alias to os.stat. ........ r58750 | christian.heimes | 2007-11-01 12:48:10 -0700 (Thu, 01 Nov 2007) | 1 line Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342 ........ r58751 | christian.heimes | 2007-11-01 13:11:06 -0700 (Thu, 01 Nov 2007) | 1 line Removed non ASCII text from test as requested by Guido. Sorry :/ ........ r58753 | georg.brandl | 2007-11-01 13:37:02 -0700 (Thu, 01 Nov 2007) | 2 lines Fix markup glitch. ........ r58757 | gregory.p.smith | 2007-11-01 14:08:14 -0700 (Thu, 01 Nov 2007) | 4 lines Fix bug introduced in revision 58385. Database keys could no longer have NULL bytes in them. Replace the errant strdup with a malloc+memcpy. Adds a unit test for the correct behavior. ........ r58758 | gregory.p.smith | 2007-11-01 14:15:36 -0700 (Thu, 01 Nov 2007) | 3 lines Undo revision 58533 58534 fixes. Those were a workaround for a problem introduced by 58385. ........ r58759 | gregory.p.smith | 2007-11-01 14:17:47 -0700 (Thu, 01 Nov 2007) | 2 lines false "fix" undone as correct problem was found and fixed. ........ r58765 | mark.summerfield | 2007-11-02 01:24:59 -0700 (Fri, 02 Nov 2007) | 3 lines Added more file-handling related cross-references. ........ r58766 | nick.coghlan | 2007-11-02 03:09:12 -0700 (Fri, 02 Nov 2007) | 1 line Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate) ........ r58784 | thomas.heller | 2007-11-02 12:10:24 -0700 (Fri, 02 Nov 2007) | 4 lines Issue #1292: On alpha, arm, ppc, and s390 linux systems the --with-system-ffi configure option defaults to "yes" because the bundled libffi sources are too old. ........ r58785 | thomas.heller | 2007-11-02 12:11:23 -0700 (Fri, 02 Nov 2007) | 1 line Enable the full ctypes c_longdouble tests again. ........ r58796 | georg.brandl | 2007-11-02 13:06:17 -0700 (Fri, 02 Nov 2007) | 4 lines Make "hashable" a glossary entry and clarify docs on __cmp__, __eq__ and __hash__. I hope the concept of hashability is better understandable now. Thanks to Tim Hatch for pointing out the flaws here. ........
1 parent e845c0f commit 2cc30da

20 files changed

Lines changed: 142 additions & 88 deletions

Doc/c-api/concrete.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,8 +2223,8 @@ Dictionary Objects
22232223
.. cfunction:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
22242224

22252225
Insert *value* into the dictionary *p* with a key of *key*. *key* must be
2226-
hashable; if it isn't, :exc:`TypeError` will be raised. Return ``0`` on success
2227-
or ``-1`` on failure.
2226+
:term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return ``0``
2227+
on success or ``-1`` on failure.
22282228

22292229

22302230
.. cfunction:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)

Doc/glossary.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ Glossary
140140
in the past to create a "free-threaded" interpreter (one which locks
141141
shared data at a much finer granularity), but performance suffered in the
142142
common single-processor case.
143+
144+
hashable
145+
An object is *hashable* if it has a hash value that never changes during
146+
its lifetime (it needs a :meth:`__hash__` method), and can be compared to
147+
other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method).
148+
Hashable objects that compare equal must have the same hash value.
149+
150+
Hashability makes an object usable as a dictionary key and a set member,
151+
because these data structures use the hash value internally.
152+
153+
All of Python's immutable built-in objects are hashable, while all mutable
154+
containers (such as lists or dictionaries) are not. Objects that are
155+
instances of user-defined classes are hashable by default; they all
156+
compare unequal, and their hash value is their :func:`id`.
143157

144158
IDLE
145159
An Integrated Development Environment for Python. IDLE is a basic editor

Doc/library/datetime.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ compared to an object of a different type, :exc:`TypeError` is raised unless the
260260
comparison is ``==`` or ``!=``. The latter cases return :const:`False` or
261261
:const:`True`, respectively.
262262

263-
:class:`timedelta` objects are hashable (usable as dictionary keys), support
263+
:class:`timedelta` objects are :term:`hashable` (usable as dictionary keys), support
264264
efficient pickling, and in Boolean contexts, a :class:`timedelta` object is
265265
considered to be true if and only if it isn't equal to ``timedelta(0)``.
266266

Doc/library/difflib.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
1818
.. class:: SequenceMatcher
1919

2020
This is a flexible class for comparing pairs of sequences of any type, so long
21-
as the sequence elements are hashable. The basic algorithm predates, and is a
21+
as the sequence elements are :term:`hashable`. The basic algorithm predates, and is a
2222
little fancier than, an algorithm published in the late 1980's by Ratcliff and
2323
Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to
2424
find the longest contiguous matching subsequence that contains no "junk"
@@ -305,7 +305,7 @@ The :class:`SequenceMatcher` class has this constructor:
305305
on blanks or hard tabs.
306306

307307
The optional arguments *a* and *b* are sequences to be compared; both default to
308-
empty strings. The elements of both sequences must be hashable.
308+
empty strings. The elements of both sequences must be :term:`hashable`.
309309

310310
:class:`SequenceMatcher` objects have the following methods:
311311

Doc/library/functions.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,9 @@ available. They are listed here in alphabetical order.
747747
value of ``None`` (if no newlines have been seen yet), ``'\n'``,
748748
``'\r'``, ``'\r\n'``, or a tuple containing all the newline types seen.
749749

750-
See also the :mod:`fileinput` module, the file-related functions in the
751-
:mod:`os` module, and the :mod:`os.path` module.
752-
750+
Python provides many file handling modules including
751+
:mod:`fileinput`, :mod:`os`, :mod:`os.path`, :mod:`tempfile`, and
752+
:mod:`shutil`.
753753

754754
.. function:: ord(c)
755755

Doc/library/os.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ functionality than importing a operating system dependent built-in module like
1111
:mod:`posix` or :mod:`nt`. If you just want to read or write a file see
1212
:func:`open`, if you want to manipulate paths, see the :mod:`os.path`
1313
module, and if you want to read all the lines in all the files on the
14-
command line see the :mod:`fileinput` module.
14+
command line see the :mod:`fileinput` module. For creating temporary
15+
files and directories see the :mod:`tempfile` module, and for high-level
16+
file and directory handling see the :mod:`shutil` module.
1517

1618
This module searches for an operating system dependent built-in module like
1719
:mod:`mac` or :mod:`posix` and exports the same functions and data as found
@@ -800,8 +802,9 @@ Files and Directories
800802

801803
.. function:: lstat(path)
802804

803-
Like :func:`stat`, but do not follow symbolic links. Availability: Macintosh,
804-
Unix.
805+
Like :func:`stat`, but do not follow symbolic links. This is an alias for
806+
:func:`stat` on platforms that do not support symbolic links, such as
807+
Windows.
805808

806809

807810
.. function:: mkfifo(path[, mode])
@@ -852,6 +855,9 @@ Files and Directories
852855
``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
853856
current umask value is first masked out. Availability: Macintosh, Unix, Windows.
854857

858+
It is also possible to create temporary directories; see the
859+
:mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
860+
855861

856862
.. function:: makedirs(path[, mode])
857863

Doc/library/random.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Bookkeeping functions:
5454
.. function:: seed([x])
5555

5656
Initialize the basic random number generator. Optional argument *x* can be any
57-
hashable object. If *x* is omitted or ``None``, current system time is used;
57+
:term:`hashable` object. If *x* is omitted or ``None``, current system time is used;
5858
current system time is also used to initialize the generator when the module is
5959
first imported. If randomness sources are provided by the operating system,
6060
they are used instead of the system time (see the :func:`os.urandom` function
@@ -140,7 +140,7 @@ Functions for sequences:
140140
(the sample) to be partitioned into grand prize and second place winners (the
141141
subslices).
142142

143-
Members of the population need not be hashable or unique. If the population
143+
Members of the population need not be :term:`hashable` or unique. If the population
144144
contains repeats, then each occurrence is a possible selection in the sample.
145145

146146
To choose a sample from a range of integers, use an :func:`range` object as an

Doc/library/shutil.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
The :mod:`shutil` module offers a number of high-level operations on files and
1717
collections of files. In particular, functions are provided which support file
18-
copying and removal.
18+
copying and removal. For operations on individual files, see also the
19+
:mod:`os` module.
1920

2021
.. warning::
2122

Doc/library/stdtypes.rst

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ functions based on regular expressions.
874874
specified, then there is no limit on the number of splits (all possible
875875
splits are made).
876876

877-
If *sep is given, consecutive delimiters are not grouped together and are
877+
If *sep* is given, consecutive delimiters are not grouped together and are
878878
deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
879879
``['1', '', '2']``). The *sep* argument may consist of multiple characters
880880
(for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
@@ -1371,7 +1371,7 @@ Set Types --- :class:`set`, :class:`frozenset`
13711371

13721372
.. index:: object: set
13731373

1374-
A :dfn:`set` object is an unordered collection of distinct hashable objects.
1374+
A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
13751375
Common uses include membership testing, removing duplicates from a sequence, and
13761376
computing mathematical operations such as intersection, union, difference, and
13771377
symmetric difference.
@@ -1387,7 +1387,7 @@ There are currently two builtin set types, :class:`set` and :class:`frozenset`.
13871387
The :class:`set` type is mutable --- the contents can be changed using methods
13881388
like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value
13891389
and cannot be used as either a dictionary key or as an element of another set.
1390-
The :class:`frozenset` type is immutable and hashable --- its contents cannot be
1390+
The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be
13911391
altered after it is created; it can therefore be used as a dictionary key or as
13921392
an element of another set.
13931393

@@ -1487,8 +1487,7 @@ or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__` method.
14871487
Since sets only define partial ordering (subset relationships), the output of
14881488
the :meth:`list.sort` method is undefined for lists of sets.
14891489

1490-
Set elements are like dictionary keys; they need to define both :meth:`__hash__`
1491-
and :meth:`__eq__` methods.
1490+
Set elements, like dictionary keys, must be :term:`hashable`.
14921491

14931492
Binary operations that mix :class:`set` instances with :class:`frozenset` return
14941493
the type of the first operand. For example: ``frozenset('ab') | set('bc')``
@@ -1559,20 +1558,20 @@ Mapping Types --- :class:`dict`
15591558
statement: del
15601559
builtin: len
15611560

1562-
A :dfn:`mapping` object maps immutable values to arbitrary objects. Mappings
1563-
are mutable objects. There is currently only one standard mapping type, the
1564-
:dfn:`dictionary`.
1565-
(For other containers see the built in :class:`list`,
1566-
:class:`set`, and :class:`tuple` classes, and the :mod:`collections`
1567-
module.)
1568-
1569-
A dictionary's keys are *almost* arbitrary values. Only values containing
1570-
lists, dictionaries or other mutable types (that are compared by value rather
1571-
than by object identity) may not be used as keys. Numeric types used for keys
1572-
obey the normal rules for numeric comparison: if two numbers compare equal (such
1573-
as ``1`` and ``1.0``) then they can be used interchangeably to index the same
1574-
dictionary entry. (Note however, that since computers store floating-point
1575-
numbers as approximations it is usually unwise to use them as dictionary keys.)
1561+
A :dfn:`mapping` object maps :term:`hashable` values to arbitrary objects.
1562+
Mappings are mutable objects. There is currently only one standard mapping
1563+
type, the :dfn:`dictionary`. (For other containers see the built in
1564+
:class:`list`, :class:`set`, and :class:`tuple` classes, and the
1565+
:mod:`collections` module.)
1566+
1567+
A dictionary's keys are *almost* arbitrary values. Values that are not
1568+
:term:`hashable`, that is, values containing lists, dictionaries or other
1569+
mutable types (that are compared by value rather than by object identity) may
1570+
not be used as keys. Numeric types used for keys obey the normal rules for
1571+
numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
1572+
then they can be used interchangeably to index the same dictionary entry. (Note
1573+
however, that since computers store floating-point numbers as approximations it
1574+
is usually unwise to use them as dictionary keys.)
15761575

15771576
Dictionaries can be created by placing a comma-separated list of ``key: value``
15781577
pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
@@ -1821,7 +1820,10 @@ created with the built-in :func:`file` and (more usually) :func:`open`
18211820
constructors described in the :ref:`built-in-funcs` section. [#]_ File
18221821
objects are also returned by some other built-in functions and methods,
18231822
such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile`
1824-
method of socket objects.
1823+
method of socket objects. Temporary files can be created using the
1824+
:mod:`tempfile` module, and high-level file operations such as copying,
1825+
moving, and deleting files and directories can be achieved with the
1826+
:mod:`shutil` module.
18251827

18261828
When a file operation fails for an I/O-related reason, the exception
18271829
:exc:`IOError` is raised. This includes situations where the operation is not

Doc/library/weakref.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Extension types can easily be made to support weak references; see
8585
but cannot be propagated; they are handled in exactly the same way as exceptions
8686
raised from an object's :meth:`__del__` method.
8787

88-
Weak references are hashable if the *object* is hashable. They will maintain
88+
Weak references are :term:`hashable` if the *object* is hashable. They will maintain
8989
their hash value even after the *object* was deleted. If :func:`hash` is called
9090
the first time only after the *object* was deleted, the call will raise
9191
:exc:`TypeError`.
@@ -104,7 +104,7 @@ Extension types can easily be made to support weak references; see
104104
the proxy in most contexts instead of requiring the explicit dereferencing used
105105
with weak reference objects. The returned object will have a type of either
106106
``ProxyType`` or ``CallableProxyType``, depending on whether *object* is
107-
callable. Proxy objects are not hashable regardless of the referent; this
107+
callable. Proxy objects are not :term:`hashable` regardless of the referent; this
108108
avoids a number of problems related to their fundamentally mutable nature, and
109109
prevent their use as dictionary keys. *callback* is the same as the parameter
110110
of the same name to the :func:`ref` function.

0 commit comments

Comments
 (0)