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

Skip to content

Commit 7a70a3a

Browse files
committed
Merged revisions 65810 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r65810 | nick.coghlan | 2008-08-18 23:14:22 +1000 (Mon, 18 Aug 2008) | 1 line Issue 2235: document PyObject_HashNotImplemented ........
1 parent 36f4952 commit 7a70a3a

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Doc/c-api/object.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ is considered sufficient for this determination.
257257
This is the equivalent of the Python expression ``hash(o)``.
258258

259259

260+
.. cfunction:: long PyObject_HashNotImplemented(PyObject *o)
261+
262+
Set a TypeError indicating that ``type(o)`` is not hashable and return ``-1``.
263+
This function receives special treatment when stored in a ``tp_hash`` slot,
264+
allowing a type to explicit indicate to the interpreter that it is not
265+
hashable.
266+
267+
.. versionadded:: 2.6
268+
269+
260270
.. cfunction:: int PyObject_IsTrue(PyObject *o)
261271

262272
Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.

Doc/c-api/typeobj.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ type objects) *must* have the :attr:`ob_size` field.
321321
error occurs during the computation of the hash value, the function should set
322322
an exception and return ``-1``.
323323

324+
This field can be set explicitly to :cfunc:`PyObject_HashNotImplemented` to
325+
block inheritance of the hash method from a parent type. This is interpreted
326+
as the equivalent of ``__hash__ = None`` at the Python level, causing
327+
``isinstance(o, collections.Hashable)`` to correctly return ``False``. Note
328+
that the converse is also true - setting ``__hash__ = None`` on a class at
329+
the Python level will result in the ``tp_hash`` slot being set to
330+
:cfunc:`PyObject_HashNotImplemented`.
331+
324332
When this field is not set, two possibilities exist: if the :attr:`tp_compare`
325333
and :attr:`tp_richcompare` fields are both *NULL*, a default hash value based on
326334
the object's address is returned; otherwise, a :exc:`TypeError` is raised.

0 commit comments

Comments
 (0)