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

Skip to content

Commit 0ffd14c

Browse files
committed
Started updating information about defining attributes on types.
There's still a long way to go, but we're starting to see some real content in the docs.
1 parent bdcb1c4 commit 0ffd14c

1 file changed

Lines changed: 171 additions & 7 deletions

File tree

Doc/ext/newtypes.tex

Lines changed: 171 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ \subsection{Object Representation}
430430
newdatatype_repr(newdatatypeobject * obj)
431431
{
432432
return PyString_FromFormat("Repr-ified_newdatatype{{size:\%d}}",
433-
obj->obj_UnderlyingDatatypePtr->size);
433+
obj->obj_UnderlyingDatatypePtr->size);
434434
}
435435
\end{verbatim}
436436

@@ -453,8 +453,7 @@ \subsection{Object Representation}
453453
newdatatype_str(newdatatypeobject * obj)
454454
{
455455
return PyString_FromFormat("Stringified_newdatatype{{size:\%d}}",
456-
obj->obj_UnderlyingDatatypePtr->size
457-
);
456+
obj->obj_UnderlyingDatatypePtr->size);
458457
}
459458
\end{verbatim}
460459

@@ -494,20 +493,185 @@ \subsection{Object Representation}
494493

495494
\subsection{Attribute Management Functions}
496495

496+
For every object which can support attributes, the corresponding type
497+
must provide the functions that control how the attributes are
498+
resolved. There needs to be a function which can retrieve attributes
499+
(if any are defined), and another to set attributes (if setting
500+
attributes is allowed). Removing an attribute is a special case, for
501+
which the new value passed to the handler is \NULL.
502+
503+
Python supports two pairs of attribute handlers; a type that supports
504+
attributes only needs to implement the functions for one pair. The
505+
difference is that one pair takes the name of the attribute as a
506+
\ctype{char*}, while the other accepts a \ctype{PyObject*}. Each type
507+
can use whichever pair makes more sense for the implementation's
508+
convenience.
509+
497510
\begin{verbatim}
498-
getattrfunc tp_getattr;
499-
setattrfunc tp_setattr;
511+
getattrfunc tp_getattr; /* char * version */
512+
setattrfunc tp_setattr;
513+
/* ... */
514+
getattrofunc tp_getattrofunc; /* PyObject * version */
515+
setattrofunc tp_setattrofunc;
500516
\end{verbatim}
501517

502-
The \member{tp_getattr} handle is called when the object requires an
518+
If accessing attributes of an object is always a simple operation
519+
(this will be explained shortly), there are generic implementations
520+
which can be used to provide the \ctype{PyObject*} version of the
521+
attribute management functions. The actual need for type-specific
522+
attribute handlers almost completely disappeared starting with Python
523+
2.2, though there are many examples which have not been updated to use
524+
some of the new generic mechanism that is available.
525+
526+
527+
\subsubsection{Generic Attribute Management}
528+
529+
\versionadded{2.2}
530+
531+
Most extension types only use \emph{simple} attributes. So, what
532+
makes the attributes simple? There are only a couple of conditions
533+
that must be met:
534+
535+
\begin{enumerate}
536+
\item The name of the attributes must be known when
537+
\cfunction{PyType_Ready()} is called.
538+
539+
\item No special processing is need to record that an attribute
540+
was looked up or set, nor do actions need to be taken based
541+
on the value.
542+
\end{enumerate}
543+
544+
Note that this list does not place any restrictions on the values of
545+
the attributes, when the values are computed, or how relevant data is
546+
stored.
547+
548+
When \cfunction{PyType_Ready()} is called, it uses three tables
549+
referenced by the type object to create \emph{descriptors} which are
550+
placed in the dictionary of the type object. Each descriptor controls
551+
access to one attribute of the instance object. Each of the tables is
552+
optional; if all three are \NULL, instances of the type will only have
553+
attributes that are inherited from their base type, and should leave
554+
the \member{tp_getattro} and \member{tp_setattro} fields \NULL{} as
555+
well, allowing the base type to handle attributes.
556+
557+
The tables are declared as three fields of the type object:
558+
559+
\begin{verbatim}
560+
struct PyMethodDef *tp_methods;
561+
struct PyMemberDef *tp_members;
562+
struct PyGetSetDef *tp_getset;
563+
\end{verbatim}
564+
565+
If \member{tp_methods} is not \NULL, it must refer to an array of
566+
\ctype{PyMethodDef} structures. Each entry in the table is an
567+
instance of this structure:
568+
569+
\begin{verbatim}
570+
typedef struct PyMethodDef {
571+
char *ml_name; /* method name */
572+
PyCFunction ml_meth; /* implementation function */
573+
int ml_flags; /* flags */
574+
char *ml_doc; /* docstring */
575+
} PyMethodDef;
576+
\end{verbatim}
577+
578+
One entry should be defined for each method provided by the type; no
579+
entries are needed for methods inherited from a base type. One
580+
additional entry is needed at the end; it is a sentinel that marks the
581+
end of the array. The \member{ml_name} field of the sentinel must be
582+
\NULL.
583+
584+
XXX Need to refer to some unified discussion of the structure fields,
585+
shared with the next section.
586+
587+
The second table is used to define attributes which map directly to
588+
data stored in the instance. A variety of primitive C types are
589+
supported, and access may be read-only or read-write. The structures
590+
in the table are defined as:
591+
592+
\begin{verbatim}
593+
typedef struct PyMemberDef {
594+
char *name;
595+
int type;
596+
int offset;
597+
int flags;
598+
char *doc;
599+
} PyMemberDef;
600+
\end{verbatim}
601+
602+
For each entry in the table, a descriptor will be constructed and
603+
added to the type which will be able to extract a value from the
604+
instance structure. The \member{type} field should contain one of the
605+
type codes defined in the \file{structmember.h} header; the value will
606+
be used to determine how to convert Python values to and from C
607+
values. The \member{flags} field is used to store flags which control
608+
how the attribute can be accessed.
609+
610+
XXX Need to move some of this to a shared section!
611+
612+
The following flag constants are defined in \file{structmember.h};
613+
they may be combined using bitwise-OR.
614+
615+
\begin{tableii}{l|l}{constant}{Constant}{Meaning}
616+
\lineii{READONLY \ttindex{READONLY}}
617+
{Never writable.}
618+
\lineii{RO \ttindex{RO}}
619+
{Shorthand for \constant{READONLY}.}
620+
\lineii{READ_RESTRICTED \ttindex{READ_RESTRICTED}}
621+
{Not readable in restricted mode.}
622+
\lineii{WRITE_RESTRICTED \ttindex{WRITE_RESTRICTED}}
623+
{Not writable in restricted mode.}
624+
\lineii{RESTRICTED \ttindex{RESTRICTED}}
625+
{Not readable or writable in restricted mode.}
626+
\end{tableii}
627+
628+
An interesting advantage of using the \member{tp_members} table to
629+
build descriptors that are used at runtime is that any attribute
630+
defined this way can have an associated docstring simply by providing
631+
the text in the table. An application can use the introspection API
632+
to retrieve the descriptor from the class object, and get the
633+
docstring using its \member{__doc__} attribute.
634+
635+
As with the \member{tp_methods} table, a sentinel entry with a
636+
\member{name} value of \NULL{} is required.
637+
638+
639+
% XXX Descriptors need to be explained in more detail somewhere, but
640+
% not here.
641+
%
642+
% Descriptor objects have two handler functions which correspond to
643+
% the \member{tp_getattro} and \member{tp_setattro} handlers. The
644+
% \method{__get__()} handler is a function which is passed the
645+
% descriptor, instance, and type objects, and returns the value of the
646+
% attribute, or it returns \NULL{} and sets an exception. The
647+
% \method{__set__()} handler is passed the descriptor, instance, type,
648+
% and new value;
649+
650+
651+
\subsubsection{Type-specific Attribute Management}
652+
653+
For simplicity, only the \ctype{char*} version will be demonstrated
654+
here; the type of the name parameter is the only difference between
655+
the \ctype{char*} and \ctype{PyObject*} flavors of the interface.
656+
This example effectively does the same thing as the generic example
657+
above, but does not use the generic support added in Python 2.2. The
658+
value in showing this is two-fold: it demonstrates how basic attribute
659+
management can be done in a way that is portable to older versions of
660+
Python, and explains how the handler functions are called, so that if
661+
you do need to extend their functionality, you'll understand what
662+
needs to be done.
663+
664+
The \member{tp_getattr} handler is called when the object requires an
503665
attribute look-up. It is called in the same situations where the
504666
\method{__getattr__()} method of a class would be called.
505667

506668
A likely way to handle this is (1) to implement a set of functions
507669
(such as \cfunction{newdatatype_getSize()} and
508670
\cfunction{newdatatype_setSize()} in the example below), (2) provide a
509671
method table listing these functions, and (3) provide a getattr
510-
function that returns the result of a lookup in that table.
672+
function that returns the result of a lookup in that table. The
673+
method table uses the same structure as the \member{tp_methods} field
674+
of the type object.
511675

512676
Here is an example:
513677

0 commit comments

Comments
 (0)