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
901a41e7
Commit
901a41e7
authored
Jul 01, 2003
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
normalize markup for consistency
parent
754a174b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
15 deletions
+16
-15
Doc/ext/newtypes.tex
Doc/ext/newtypes.tex
+16
-15
No files found.
Doc/ext/newtypes.tex
View file @
901a41e7
...
...
@@ -158,7 +158,7 @@ to \class{noddy.Noddy}.
\end{verbatim}
This is so that Python knows how much memory to allocate when you call
\cfunction
{
PyObject
_
New
}
.
\cfunction
{
PyObject
_
New
()
}
.
\begin{verbatim}
0, /* tp
_
itemsize */
...
...
@@ -192,7 +192,7 @@ For now, all we want to be able to do is to create new \class{Noddy}
objects. To enable object creation, we have to provide a
\member
{
tp
_
new
}
implementation. In this case, we can just use the
default implementation provided by the API function
\cfunction
{
PyType
_
GenericNew
}
. We'd like to just assign this to the
\cfunction
{
PyType
_
GenericNew
()
}
. We'd like to just assign this to the
\member
{
tp
_
new
}
slot, but we can't, for portability sake, On some
platforms or compilers, we can't statically initialize a structure
member with a function defined in another C module, so, instead, we'll
...
...
@@ -209,7 +209,7 @@ All the other type methods are \NULL, so we'll go over them later
--- that's for a later section!
Everything else in the file should be familiar, except for some code
in
\cfunction
{
initnoddy
}
:
in
\cfunction
{
initnoddy
()
}
:
\begin{verbatim}
if (PyType
_
Ready(
&
noddy
_
NoddyType) < 0)
...
...
@@ -315,7 +315,7 @@ which is assigned to the \member{tp_dealloc} member:
\end
{
verbatim
}
This method decrements the reference counts of the two Python
attributes. We use
\cfunction
{
Py
_
XDECREF
}
here because the
attributes. We use
\cfunction
{
Py
_
XDECREF
()
}
here because the
\member
{
first
}
and
\member
{
last
}
members could be
\NULL
. It then
calls the
\member
{
tp
_
free
}
member of the object's type to free the
object's memory. Note that the object's type might not be
...
...
@@ -362,14 +362,14 @@ and install it in the \member{tp_new} member:
The new member is responsible for creating
(
as opposed to
initializing
)
objects of the type. It is exposed in Python as the
\method
{__
new
__}
method. See the paper titled ``Unifying types and
classes in Python'' for a detailed discussion of the
\method
{__
new
__}
\method
{__
new
__
()
}
method. See the paper titled ``Unifying types and
classes in Python'' for a detailed discussion of the
\method
{__
new
__
()
}
method. One reason to implement a new method is to assure the initial
values of instance variables. In this case, we use the new method to
make sure that the initial values of the members
\member
{
first
}
and
\member
{
last
}
are not
\NULL
. If we didn't care whether the initial
values were
\NULL
, we could have used
\cfunction
{
PyType
_
GenericNew
}
as
our new method, as we did before.
\cfunction
{
PyType
_
GenericNew
}
values were
\NULL
, we could have used
\cfunction
{
PyType
_
GenericNew
()
}
as
our new method, as we did before.
\cfunction
{
PyType
_
GenericNew
()
}
initializes all of the instance variable members to NULLs.
The new method is a static method that is passed the type being
...
...
@@ -422,7 +422,7 @@ by filling the \member{tp_init} slot.
\end
{
verbatim
}
The
\member
{
tp
_
init
}
slot is exposed in Python as the
\method
{__
init
__}
method. It is used to initialize an object after
\method
{__
init
__
()
}
method. It is used to initialize an object after
it's created. Unlike the new method, we can't guarantee that the
initializer is called. The initializer isn't called when unpickling
objects and it can be overridden. Our initializer accepts arguments
...
...
@@ -550,8 +550,8 @@ definition:
Py
_
TPFLAGS
_
DEFAULT | Py
_
TPFLAGS
_
BASETYPE,
/*
tp
_
flags
*/
\end
{
verbatim
}
We rename
\cfunction
{
initnoddy
}
to
\cfunction
{
initnoddy
2
}
and update the module name passed to
\cfunction
{
Py
_
InitModule
3
}
.
We rename
\cfunction
{
initnoddy
()
}
to
\cfunction
{
initnoddy
2
()
}
and update the module name passed to
\cfunction
{
Py
_
InitModule
3
()
}
.
Finally, we update our
\file
{
setup.py
}
file to build the new module:
...
...
@@ -661,7 +661,7 @@ static PyMemberDef Noddy_members[] = {
With these changes, we can assure that the
\member
{
first
}
and
\member
{
last
}
members are never NULL so we can remove checks for
\NULL
values in almost all cases. This means that most of the
\cfunction
{
Py
_
XDECREF
}
calls can be converted to
\cfunction
{
Py
_
DECREF
}
\cfunction
{
Py
_
XDECREF
()
}
calls can be converted to
\cfunction
{
Py
_
DECREF
()
}
calls. The only place we can't change these calls is in the
deallocator, where there is the possibility that the initialization of
these members failed in the constructor.
...
...
@@ -723,8 +723,8 @@ Noddy_traverse(Noddy *self, visitproc visit, void *arg)
\end
{
verbatim
}
For each subobject that can participate in cycles, we need to call the
\cfunction
{
visit
}
function, which is passed to the traversal method.
The
\cfunction
{
visit
}
function takes as arguments the subobject and
\cfunction
{
visit
()
}
function, which is passed to the traversal method.
The
\cfunction
{
visit
()
}
function takes as arguments the subobject and
the extra argument
\var
{
arg
}
passed to the traversal method.
We also need to provide a method for clearing any subobjects that can
...
...
@@ -751,7 +751,8 @@ Noddy_dealloc(Noddy* self)
}
\end
{
verbatim
}
Finally, we add the
\constant
{
Py
_
TPFLAGS
_
HAVE
_
GC
}
flag to the class flags:
Finally, we add the
\constant
{
Py
_
TPFLAGS
_
HAVE
_
GC
}
flag to the class
flags:
\begin
{
verbatim
}
Py
_
TPFLAGS
_
DEFAULT | Py
_
TPFLAGS
_
BASETYPE | Py
_
TPFLAGS
_
HAVE
_
GC,
/*
tp
_
flags
*/
...
...
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