@@ -592,17 +592,34 @@ type objects) *must* have the :attr:`ob_size` field.
592592
593593 This field is inherited by subtypes.
594594
595- .. cmember :: PyNumberMethods * tp_as_number;
595+ .. cmember :: PyNumberMethods* tp_as_number
596596
597- XXX
597+ Pointer to an additional structure that contains fields relevant only to
598+ objects which implement the number protocol. These fields are documented in
599+ :ref: `number-structs `.
600+
601+ The :attr: `tp_as_number ` field is not inherited, but the contained fields are
602+ inherited individually.
603+
604+
605+ .. cmember :: PySequenceMethods* tp_as_sequence
606+
607+ Pointer to an additional structure that contains fields relevant only to
608+ objects which implement the sequence protocol. These fields are documented
609+ in :ref: `sequence-structs `.
610+
611+ The :attr: `tp_as_sequence ` field is not inherited, but the contained fields
612+ are inherited individually.
598613
599- .. cmember :: PySequenceMethods *tp_as_sequence;
600614
601- XXX
615+ .. cmember :: PyMappingMethods* tp_as_mapping
602616
603- .. cmember :: PyMappingMethods *tp_as_mapping;
617+ Pointer to an additional structure that contains fields relevant only to
618+ objects which implement the mapping protocol. These fields are documented in
619+ :ref: `mapping-structs `.
604620
605- XXX
621+ The :attr: `tp_as_mapping ` field is not inherited, but the contained fields are
622+ inherited individually.
606623
607624
608625.. cmember :: hashfunc PyTypeObject.tp_hash
@@ -1401,41 +1418,175 @@ objects on the thread which called tp_dealloc will not violate any assumptions
14011418of the library.
14021419
14031420
1421+ .. _number-structs :
1422+
1423+ Number Object Structures
1424+ ========================
1425+
1426+ .. sectionauthor :: Amaury Forgeot d'Arc
1427+
1428+
1429+ .. ctype :: PyNumberMethods
1430+
1431+ This structure holds pointers to the functions which an object uses to
1432+ implement the number protocol. Each function is used by the function of
1433+ similar name documented in the :ref: `number ` section.
1434+
1435+ Here is the structure definition::
1436+
1437+ typedef struct {
1438+ binaryfunc nb_add;
1439+ binaryfunc nb_subtract;
1440+ binaryfunc nb_multiply;
1441+ binaryfunc nb_remainder;
1442+ binaryfunc nb_divmod;
1443+ ternaryfunc nb_power;
1444+ unaryfunc nb_negative;
1445+ unaryfunc nb_positive;
1446+ unaryfunc nb_absolute;
1447+ inquiry nb_bool;
1448+ unaryfunc nb_invert;
1449+ binaryfunc nb_lshift;
1450+ binaryfunc nb_rshift;
1451+ binaryfunc nb_and;
1452+ binaryfunc nb_xor;
1453+ binaryfunc nb_or;
1454+ int nb_reserved; /* unused, must be zero */
1455+ unaryfunc nb_int;
1456+ unaryfunc nb_long;
1457+ unaryfunc nb_float;
1458+
1459+ unaryfunc nb_oct; /* not used anymore, must be zero */
1460+ unaryfunc nb_hex; /* not used anymore, must be zero */
1461+
1462+ binaryfunc nb_inplace_add;
1463+ binaryfunc nb_inplace_subtract;
1464+ binaryfunc nb_inplace_multiply;
1465+ binaryfunc nb_inplace_remainder;
1466+ ternaryfunc nb_inplace_power;
1467+ binaryfunc nb_inplace_lshift;
1468+ binaryfunc nb_inplace_rshift;
1469+ binaryfunc nb_inplace_and;
1470+ binaryfunc nb_inplace_xor;
1471+ binaryfunc nb_inplace_or;
1472+
1473+ binaryfunc nb_floor_divide;
1474+ binaryfunc nb_true_divide;
1475+ binaryfunc nb_inplace_floor_divide;
1476+ binaryfunc nb_inplace_true_divide;
1477+
1478+ unaryfunc nb_index;
1479+ } PyNumberMethods;
1480+
1481+ .. note ::
1482+
1483+ Binary and ternary functions must check the type of all their operands,
1484+ and implement the necessary conversions (at least one of the operands is
1485+ an instance of the defined type). If the operation is not defined for the
1486+ given operands, binary and ternary functions must return
1487+ ``Py_NotImplemented ``, if another error occurred they must return ``NULL ``
1488+ and set an exception.
1489+
1490+
14041491.. _mapping-structs :
14051492
14061493Mapping Object Structures
14071494=========================
14081495
1496+ .. sectionauthor :: Amaury Forgeot d'Arc
1497+
14091498
14101499.. ctype :: PyMappingMethods
14111500
1412- Structure used to hold pointers to the functions used to implement the mapping
1413- protocol for an extension type.
1501+ This structure holds pointers to the functions which an object uses to
1502+ implement the mapping protocol. It has three members:
14141503
1504+ .. cmember :: lenfunc PyMappingMethods.mp_length
14151505
1416- .. _number-structs :
1506+ This function is used by :cfunc: `PyMapping_Length ` and
1507+ :cfunc: `PyObject_Size `, and has the same signature. This slot may be set to
1508+ *NULL * if the object has no defined length.
14171509
1418- Number Object Structures
1419- ========================
1510+ .. cmember :: binaryfunc PyMappingMethods.mp_subscript
14201511
1512+ This function is used by :cfunc: `PyObject_GetItem ` and has the same
1513+ signature. This slot must be filled for the :cfunc: `PyMapping_Check `
1514+ function to return ``1 ``, it can be *NULL * otherwise.
14211515
1422- .. ctype :: PyNumberMethods
1516+ .. cmember :: objobjargproc PyMappingMethods.mp_ass_subscript
14231517
1424- Structure used to hold pointers to the functions an extension type uses to
1425- implement the number protocol.
1518+ This function is used by :cfunc: `PyObject_SetItem ` and has the same
1519+ signature. If this slot is *NULL *, the object does not support item
1520+ assignment.
14261521
14271522
14281523.. _sequence-structs :
14291524
14301525Sequence Object Structures
14311526==========================
14321527
1528+ .. sectionauthor :: Amaury Forgeot d'Arc
1529+
14331530
14341531.. ctype :: PySequenceMethods
14351532
1436- Structure used to hold pointers to the functions which an object uses to
1533+ This structure holds pointers to the functions which an object uses to
14371534 implement the sequence protocol.
14381535
1536+ .. cmember :: lenfunc PySequenceMethods.sq_length
1537+
1538+ This function is used by :cfunc: `PySequence_Size ` and :cfunc: `PyObject_Size `,
1539+ and has the same signature.
1540+
1541+ .. cmember :: binaryfunc PySequenceMethods.sq_concat
1542+
1543+ This function is used by :cfunc: `PySequence_Concat ` and has the same
1544+ signature. It is also used by the `+ ` operator, after trying the numeric
1545+ addition via the :attr: `tp_as_number.nb_add ` slot.
1546+
1547+ .. cmember :: ssizeargfunc PySequenceMethods.sq_repeat
1548+
1549+ This function is used by :cfunc: `PySequence_Repeat ` and has the same
1550+ signature. It is also used by the `* ` operator, after trying numeric
1551+ multiplication via the :attr: `tp_as_number.nb_mul ` slot.
1552+
1553+ .. cmember :: ssizeargfunc PySequenceMethods.sq_item
1554+
1555+ This function is used by :cfunc: `PySequence_GetItem ` and has the same
1556+ signature. This slot must be filled for the :cfunc: `PySequence_Check `
1557+ function to return ``1 ``, it can be *NULL * otherwise.
1558+
1559+ Negative indexes are handled as follows: if the :attr: `sq_length ` slot is
1560+ filled, it is called and the sequence length is used to compute a positive
1561+ index which is passed to :attr: `sq_item `. If :attr: `sq_length ` is *NULL *,
1562+ the index is passed as is to the function.
1563+
1564+ .. cmember :: ssizeobjargproc PySequenceMethods.sq_ass_item
1565+
1566+ This function is used by :cfunc: `PySequence_SetItem ` and has the same
1567+ signature. This slot may be left to *NULL * if the object does not support
1568+ item assignment.
1569+
1570+ .. cmember :: objobjproc PySequenceMethods.sq_contains
1571+
1572+ This function may be used by :cfunc: `PySequence_Contains ` and has the same
1573+ signature. This slot may be left to *NULL *, in this case
1574+ :cfunc: `PySequence_Contains ` simply traverses the sequence until it finds a
1575+ match.
1576+
1577+ .. cmember :: binaryfunc PySequenceMethods.sq_inplace_concat
1578+
1579+ This function is used by :cfunc: `PySequence_InPlaceConcat ` and has the same
1580+ signature. It should modify its first operand, and return it.
1581+
1582+ .. cmember :: ssizeargfunc PySequenceMethods.sq_inplace_repeat
1583+
1584+ This function is used by :cfunc: `PySequence_InPlaceRepeat ` and has the same
1585+ signature. It should modify its first operand, and return it.
1586+
1587+ .. XXX need to explain precedence between mapping and sequence
1588+ .. XXX explains when to implement the sq_inplace_* slots
1589+
14391590
14401591 .. _buffer-structs :
14411592
0 commit comments