@@ -713,10 +713,26 @@ \subsection{Object Comparison}
713713
714714\subsection {Abstract Protocol Support }
715715
716+ Python supports a variety of \emph {abstract } `protocols;' the specific
717+ interfaces provided to use these interfaces are documented in the
718+ \citetitle [../api/api.html]{Python /C API Reference Manual } in the
719+ chapter `` \ulink {Abstract Objects Layer}{../api/abstract.html}.''
720+
721+ A number of these abstract interfaces were defined early in the
722+ development of the Python implementation. In particular, the number,
723+ mapping, and sequence protocols have been part of Python since the
724+ beginning. Other protocols have been added over time. For protocols
725+ which depend on several handler routines from the type implementation,
726+ the older protocols have been defined as optional blocks of handlers
727+ referenced by the type object, while newer protocols have been added
728+ using additional slots in the main type object, with a flag bit being
729+ set to indicate that the slots are present. (The flag bit does not
730+ indicate that the slot values are non-\NULL .)
731+
716732\begin {verbatim }
717- tp_as_number;
718- tp_as_sequence;
719- tp_as_mapping;
733+ PyNumberMethods tp_as_number;
734+ PySequenceMethods tp_as_sequence;
735+ PyMappingMethods tp_as_mapping;
720736\end {verbatim }
721737
722738If you wish your object to be able to act like a number, a sequence,
@@ -777,7 +793,7 @@ \subsection{Abstract Protocol Support}
777793 saying that keyword arguments are not supported.
778794\end {enumerate }
779795
780- Here is a desultory example of the implementation of call function.
796+ Here is a desultory example of the implementation of the call function.
781797
782798\begin {verbatim }
783799/* Implement the call function.
@@ -805,6 +821,46 @@ \subsection{Abstract Protocol Support}
805821}
806822\end {verbatim }
807823
824+ XXX some fields need to be added here...
825+
826+
827+ \begin {verbatim }
828+ /* Added in release 2.2 */
829+ /* Iterators */
830+ getiterfunc tp_iter;
831+ iternextfunc tp_iternext;
832+ \end {verbatim }
833+
834+ These functions provide support for the iterator protocol. Any object
835+ which wishes to support iteration over it's contents (which may be
836+ generated during iteration) must implement the \code {tp_iter}
837+ handler. Objects which are returned by a \code {tp_iter} handler must
838+ implement both the \code {tp_iter} and \code {tp_iternext} handlers.
839+ Both handlers take exactly one parameter, the instance for which they
840+ are being called, and return a new reference. In the case of an
841+ error, they should set an exception and return \NULL .
842+
843+ For an object which represents an iterable collection, the
844+ \code {tp_iter} handler must return an iterator object. The iterator
845+ object is responsible for maintaining the state of the iteration. For
846+ collections which can support multiple iterators which do not
847+ interfere with each other (as lists and tuples do), a new iterator
848+ should be created and returned. Objects which can only be iterated
849+ over once (usually due to side effects of iteration) should implement
850+ this handler by returning a new reference to themselves, and should
851+ also implement the \code {tp_iternext} handler. File objects are an
852+ example of such an iterator.
853+
854+ Iterator objects should implement both handlers. The \code {tp_iter}
855+ handler should return a new reference to the iterator (this is the
856+ same as the \code {tp_iter} handler for objects which can only be
857+ iterated over destructively). The \code {tp_iternext} handler should
858+ return a new reference to the next object in the iteration if there is
859+ one. If the iteration has reached the end, it may return \NULL {}
860+ without setting an exception or it may set \exception {StopIteration};
861+ avoiding the exception can yield slightly better performance. If an
862+ actual error occurs, it should set an exception and return \NULL .
863+
808864
809865\subsection {More Suggestions }
810866
0 commit comments