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

Skip to content

Commit ee48519

Browse files
committed
Modernize the minimal example of an extension type.
1 parent 28de8d4 commit ee48519

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Doc/ext/noddy.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@ staticforward PyTypeObject noddy_NoddyType;
44

55
typedef struct {
66
PyObject_HEAD
7+
/* Type-specific fields go here. */
78
} noddy_NoddyObject;
89

910
static PyObject*
1011
noddy_new_noddy(PyObject* self, PyObject* args)
1112
{
1213
noddy_NoddyObject* noddy;
1314

14-
if (!PyArg_ParseTuple(args,":new_noddy"))
15-
return NULL;
16-
1715
noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
1816

17+
/* Initialize type-specific fields here. */
18+
1919
return (PyObject*)noddy;
2020
}
2121

2222
static void
2323
noddy_noddy_dealloc(PyObject* self)
2424
{
25+
/* Free any external resources here;
26+
* if the instance owns references to any Python
27+
* objects, call Py_DECREF() on them here.
28+
*/
2529
PyObject_Del(self);
2630
}
2731

28-
static PyTypeObject noddy_NoddyType = {
32+
statichere PyTypeObject noddy_NoddyType = {
2933
PyObject_HEAD_INIT(NULL)
3034
0,
3135
"Noddy",
@@ -44,15 +48,19 @@ static PyTypeObject noddy_NoddyType = {
4448
};
4549

4650
static PyMethodDef noddy_methods[] = {
47-
{"new_noddy", noddy_new_noddy, METH_VARARGS,
51+
{"new_noddy", noddy_new_noddy, METH_NOARGS,
4852
"Create a new Noddy object."},
49-
{NULL, NULL, 0, NULL}
53+
54+
{NULL} /* Sentinel */
5055
};
5156

5257
DL_EXPORT(void)
5358
initnoddy(void)
5459
{
5560
noddy_NoddyType.ob_type = &PyType_Type;
61+
if (PyType_Ready(&noddy_NoddyType))
62+
return;
5663

57-
Py_InitModule("noddy", noddy_methods);
64+
Py_InitModule3("noddy", noddy_methods
65+
"Example module that creates an extension type.");
5866
}

0 commit comments

Comments
 (0)