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

Skip to content

Commit db6a569

Browse files
author
Jim Fulton
committed
Changed the assignment of PyType_GenericNew to tp_new slot. Now do
this in module initialization before calling PyType_Ready. (Sorry Tim.) This is necessary to compile on cygwin. AFAIK, we support cygwin. If so, then we need to write extentions this way. Fixed bug in implementation of tp_init function. It should be an int function, not a PyObject *.
1 parent ded8e74 commit db6a569

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

Doc/ext/newtypes.tex

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,6 @@ \section{The Basics
9999
0, /*tp_as_buffer*/
100100
Py_TPFLAGS_DEFAULT, /*tp_flags*/
101101
"Noddy objects", /* tp_doc */
102-
0, /* tp_traverse */
103-
0, /* tp_clear */
104-
0, /* tp_richcompare */
105-
0, /* tp_weaklistoffset */
106-
0, /* tp_iter */
107-
0, /* tp_iternext */
108-
0, /* tp_methods */
109-
0, /* tp_members */
110-
0, /* tp_getset */
111-
0, /* tp_base */
112-
0, /* tp_dict */
113-
0, /* tp_descr_get */
114-
0, /* tp_descr_set */
115-
0, /* tp_dictoffset */
116-
0, /* tp_init */
117-
0, /* tp_alloc */
118-
PyType_GenericNew, /* tp_new */
119102
};
120103
\end{verbatim}
121104

@@ -209,10 +192,17 @@ \section{The Basics
209192
objects. To enable object creation, we have to provide a
210193
\member{tp_new} implementation. In this case, we can just use the
211194
default implementation provided by the API function
212-
\cfunction{PyType_GenericNew}.
195+
\cfunction{PyType_GenericNew}. We'd like to just assign this to the
196+
\member{tp_new} slot, but we can't, for portability sake, On some
197+
platforms or compilers, we can't statically initialize a structure
198+
member with a function defined in another C module, so, instead, we'll
199+
assign the \member{tp_new} slot in the module initialization function
200+
just before calling \cfunction{PyType_Ready()}:
213201

214202
\begin{verbatim}
215-
PyType_GenericNew, /* tp_new */
203+
noddy_NoddyType.tp_new = PyType_GenericNew;
204+
if (PyType_Ready(&noddy_NoddyType) < 0)
205+
return;
216206
\end{verbatim}
217207

218208
All the other type methods are \NULL, so we'll go over them later
@@ -397,7 +387,7 @@ \subsection{Adding data and methods to the Basic example}
397387
We provide an initialization function:
398388

399389
\begin{verbatim}
400-
static PyObject *
390+
static int
401391
Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
402392
{
403393
PyObject *first=NULL, *last=NULL;
@@ -407,7 +397,7 @@ \subsection{Adding data and methods to the Basic example}
407397
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
408398
&first, &last,
409399
&self->number))
410-
return NULL;
400+
return -1;
411401
412402
if (first) {
413403
Py_XDECREF(self->first);
@@ -421,8 +411,7 @@ \subsection{Adding data and methods to the Basic example}
421411
self->last = last;
422412
}
423413
424-
Py_INCREF(Py_None);
425-
return Py_None;
414+
return 0;
426415
}
427416
\end{verbatim}
428417

0 commit comments

Comments
 (0)