File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments