Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
cc9b9ccb
Commit
cc9b9ccb
authored
Feb 27, 2012
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some corrections for the Doc/extending documentation. Closes #14129
parent
d09461d5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
45 deletions
+23
-45
Doc/c-api/type.rst
Doc/c-api/type.rst
+2
-1
Doc/extending/extending.rst
Doc/extending/extending.rst
+0
-3
Doc/extending/newtypes.rst
Doc/extending/newtypes.rst
+21
-41
No files found.
Doc/c-api/type.rst
View file @
cc9b9ccb
...
...
@@ -75,7 +75,8 @@ Type Objects
.. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
XXX: Document.
Generic handler for the :attr:`tp_new` slot of a type object. Initialize
all instance variables to *NULL*.
.. c:function:: int PyType_Ready(PyTypeObject *type)
...
...
Doc/extending/extending.rst
View file @
cc9b9ccb
...
...
@@ -384,9 +384,6 @@ optionally followed by an import of the module::
imports it. */
PyImport_ImportModule("spam");
An example may be found in the file :file:`Demo/embed/demo.c` in the Python
source distribution.
.. note::
Removing entries from ``sys.modules`` or importing compiled modules into
...
...
Doc/extending/newtypes.rst
View file @
cc9b9ccb
...
...
@@ -26,11 +26,12 @@ The Basics
==========
The Python runtime sees all Python objects as variables of type
:c:type:`PyObject\*`. A :c:type:`PyObject` is not a very magnificent object - it
just contains the refcount and a pointer to the object's "type object". This is
where the action is; the type object determines which (C) functions get called
when, for instance, an attribute gets looked up on an object or it is multiplied
by another object. These C functions are called "type methods".
:c:type:`PyObject\*`, which serves as a "base type" for all Python objects.
:c:type:`PyObject` itself only contains the refcount and a pointer to the
object's "type object". This is where the action is; the type object determines
which (C) functions get called when, for instance, an attribute gets looked
up on an object or it is multiplied by another object. These C functions
are called "type methods".
So, if you want to define a new object type, you need to create a new type
object.
...
...
@@ -50,15 +51,15 @@ The first bit that will be new is::
PyObject_HEAD
} noddy_NoddyObject;
This is what a Noddy object will contain---in this case, nothing more than
every
Python object contains, namely a refcount and a pointer to a type object. These
are the fields the ``PyObject_HEAD`` macro brings in. The reason for the macro
is to standardize the layout and to enable special debugging fields in debug
builds. Note that there is no semicolon after the ``PyObject_HEAD`` macro; one
is included in the macro definition. Be wary of adding one by accident; it's
easy to do from habit, and your compiler might not complain, but someone else's
probably will! (On Windows, MSVC is known to call this an error and refuse to
compile the code.)
This is what a Noddy object will contain---in this case, nothing more than
what
every Python object contains---a refcount and a pointer to a type object.
These are the fields the ``PyObject_HEAD`` macro brings in. The reason for the
macro is to standardize the layout and to enable special debugging fields in
debug builds. Note that there is no semicolon after the ``PyObject_HEAD``
macro; one is included in the macro definition. Be wary of adding one by
accident; it's easy to do from habit, and your compiler might not complain,
but someone else's probably will! (On Windows, MSVC is known to call this an
error and refuse to
compile the code.)
For contrast, let's take a look at the corresponding definition for standard
Python floats::
...
...
@@ -224,7 +225,7 @@ doesn't do anything. It can't even be subclassed.
Adding data and methods to the Basic example
--------------------------------------------
Let's ex
p
end the basic example to add some data and methods. Let's also make
Let's ex
t
end the basic example to add some data and methods. Let's also make
the type usable as a base class. We'll create a new module, :mod:`noddy2` that
adds these capabilities:
...
...
@@ -325,8 +326,8 @@ any arguments passed when the type was called, and that returns the new object
created. New methods always accept positional and keyword arguments, but they
often ignore the arguments, leaving the argument handling to initializer
methods. Note that if the type supports subclassing, the type passed may not be
the type being defined. The new method calls the
tp_alloc slot to allocate
memory. We don't fill the :attr:`tp_alloc` slot ourselves. Rather
the type being defined. The new method calls the
:attr:`tp_alloc` slot to
allocate
memory. We don't fill the :attr:`tp_alloc` slot ourselves. Rather
:c:func:`PyType_Ready` fills it for us by inheriting it from our base class,
which is :class:`object` by default. Most types use the default allocation.
...
...
@@ -443,15 +444,6 @@ concatenation of the first and last names. ::
static PyObject *
Noddy_name(Noddy* self)
{
static PyObject *format = NULL;
PyObject *args, *result;
if (format == NULL) {
format = PyString_FromString("%s %s");
if (format == NULL)
return NULL;
}
if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first");
return NULL;
...
...
@@ -462,20 +454,13 @@ concatenation of the first and last names. ::
return NULL;
}
args = Py_BuildValue("OO", self->first, self->last);
if (args == NULL)
return NULL;
result = PyString_Format(format, args);
Py_DECREF(args);
return result;
return PyUnicode_FromFormat("%S %S", self->first, self->last);
}
The method is implemented as a C function that takes a :class:`Noddy` (or
:class:`Noddy` subclass) instance as the first argument. Methods always take an
instance as the first argument. Methods often take positional and keyword
arguments as well, but in this case
d
we don't take any and don't need to accept
arguments as well, but in this case we don't take any and don't need to accept
a positional argument tuple or keyword argument dictionary. This method is
equivalent to the Python method::
...
...
@@ -1122,9 +1107,6 @@ needed for methods inherited from a base type. One additional entry is needed
at the end; it is a sentinel that marks the end of the array. The
:attr:`ml_name` field of the sentinel must be *NULL*.
XXX Need to refer to some unified discussion of the structure fields, shared
with the next section.
The second table is used to define attributes which map directly to data stored
in the instance. A variety of primitive C types are supported, and access may
be read-only or read-write. The structures in the table are defined as::
...
...
@@ -1144,8 +1126,6 @@ type which will be able to extract a value from the instance structure. The
convert Python values to and from C values. The :attr:`flags` field is used to
store flags which control how the attribute can be accessed.
XXX Need to move some of this to a shared section!
The following flag constants are defined in :file:`structmember.h`; they may be
combined using bitwise-OR.
...
...
@@ -1370,7 +1350,7 @@ Here is a desultory example of the implementation of the call function. ::
return result;
}
XXX some fields need to be added here...
::
::
/* Iterators */
getiterfunc tp_iter;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment