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

Skip to content

Commit 2b65444

Browse files
committed
Converted to new style
1 parent 037b940 commit 2b65444

1 file changed

Lines changed: 70 additions & 70 deletions

File tree

Modules/xxmodule.c

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2323
******************************************************************/
2424

2525
/* Use this file as a template to start implementing a module that
26-
also declares objects types. All occurrences of 'xxo' should be changed
26+
also declares objects types. All occurrences of 'Xxo' should be changed
2727
to something reasonable for your objects. After that, all other
2828
occurrences of 'xx' should be changed to something reasonable for your
2929
module. If your module is named foo your sourcefile should be named
@@ -37,25 +37,25 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3737

3838
/* Xxo objects */
3939

40-
#include "allobjects.h"
40+
#include "Python.h"
4141

42-
static object *ErrorObject;
42+
static PyObject *ErrorObject;
4343

4444
typedef struct {
45-
OB_HEAD
46-
object *x_attr; /* Attributes dictionary */
47-
} xxoobject;
45+
PyObject_HEAD
46+
PyObject *x_attr; /* Attributes dictionary */
47+
} XxoObject;
4848

49-
staticforward typeobject Xxotype;
49+
staticforward PyTypeObject Xxo_Type;
5050

51-
#define is_xxoobject(v) ((v)->ob_type == &Xxotype)
51+
#define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
5252

53-
static xxoobject *
54-
newxxoobject(arg)
55-
object *arg;
53+
static XxoObject *
54+
newXxoObject(arg)
55+
PyObject *arg;
5656
{
57-
xxoobject *self;
58-
self = NEWOBJ(xxoobject, &Xxotype);
57+
XxoObject *self;
58+
self = PyObject_NEW(XxoObject, &Xxo_Type);
5959
if (self == NULL)
6060
return NULL;
6161
self->x_attr = NULL;
@@ -65,77 +65,77 @@ newxxoobject(arg)
6565
/* Xxo methods */
6666

6767
static void
68-
xxo_dealloc(self)
69-
xxoobject *self;
68+
Xxo_dealloc(self)
69+
XxoObject *self;
7070
{
71-
XDECREF(self->x_attr);
72-
DEL(self);
71+
Py_XDECREF(self->x_attr);
72+
PyMem_DEL(self);
7373
}
7474

75-
static object *
76-
xxo_demo(self, args)
77-
xxoobject *self;
78-
object *args;
75+
static PyObject *
76+
Xxo_demo(self, args)
77+
XxoObject *self;
78+
PyObject *args;
7979
{
80-
if (!getnoarg(args))
80+
if (!PyArg_ParseTuple(args, ""))
8181
return NULL;
82-
INCREF(None);
83-
return None;
82+
Py_INCREF(Py_None);
83+
return Py_None;
8484
}
8585

86-
static struct methodlist xxo_methods[] = {
87-
{"demo", (method)xxo_demo},
86+
static PyMethodDef Xxo_methods[] = {
87+
{"demo", (PyCFunction)Xxo_demo, 1},
8888
{NULL, NULL} /* sentinel */
8989
};
9090

91-
static object *
92-
xxo_getattr(self, name)
93-
xxoobject *self;
91+
static PyObject *
92+
Xxo_getattr(self, name)
93+
XxoObject *self;
9494
char *name;
9595
{
9696
if (self->x_attr != NULL) {
97-
object *v = dictlookup(self->x_attr, name);
97+
PyObject *v = PyDict_GetItemString(self->x_attr, name);
9898
if (v != NULL) {
99-
INCREF(v);
99+
Py_INCREF(v);
100100
return v;
101101
}
102102
}
103-
return findmethod(xxo_methods, (object *)self, name);
103+
return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
104104
}
105105

106106
static int
107-
xxo_setattr(self, name, v)
108-
xxoobject *self;
107+
Xxo_setattr(self, name, v)
108+
XxoObject *self;
109109
char *name;
110-
object *v;
110+
PyObject *v;
111111
{
112112
if (self->x_attr == NULL) {
113-
self->x_attr = newdictobject();
113+
self->x_attr = PyDict_New();
114114
if (self->x_attr == NULL)
115115
return -1;
116116
}
117117
if (v == NULL) {
118-
int rv = dictremove(self->x_attr, name);
118+
int rv = PyDict_DelItemString(self->x_attr, name);
119119
if (rv < 0)
120-
err_setstr(AttributeError,
121-
"delete non-existing xxo attribute");
120+
PyErr_SetString(PyExc_AttributeError,
121+
"delete non-existing Xxo attribute");
122122
return rv;
123123
}
124124
else
125-
return dictinsert(self->x_attr, name, v);
125+
return PyDict_SetItemString(self->x_attr, name, v);
126126
}
127127

128-
staticforward typeobject Xxotype = {
129-
OB_HEAD_INIT(&Typetype)
128+
staticforward PyTypeObject Xxo_Type = {
129+
PyObject_HEAD_INIT(&PyType_Type)
130130
0, /*ob_size*/
131-
"xxo", /*tp_name*/
132-
sizeof(xxoobject), /*tp_basicsize*/
131+
"Xxo", /*tp_name*/
132+
sizeof(XxoObject), /*tp_basicsize*/
133133
0, /*tp_itemsize*/
134134
/* methods */
135-
(destructor)xxo_dealloc, /*tp_dealloc*/
135+
(destructor)Xxo_dealloc, /*tp_dealloc*/
136136
0, /*tp_print*/
137-
(getattrfunc)xxo_getattr, /*tp_getattr*/
138-
(setattrfunc)xxo_setattr, /*tp_setattr*/
137+
(getattrfunc)Xxo_getattr, /*tp_getattr*/
138+
(setattrfunc)Xxo_setattr, /*tp_setattr*/
139139
0, /*tp_compare*/
140140
0, /*tp_repr*/
141141
0, /*tp_as_number*/
@@ -147,44 +147,44 @@ staticforward typeobject Xxotype = {
147147

148148
/* Function of two integers returning integer */
149149

150-
static object *
150+
static PyObject *
151151
xx_foo(self, args)
152-
object *self; /* Not used */
153-
object *args;
152+
PyObject *self; /* Not used */
153+
PyObject *args;
154154
{
155155
long i, j;
156156
long res;
157-
if (!getargs(args, "(ll)", &i, &j))
157+
if (!PyArg_ParseTuple(args, "ll", &i, &j))
158158
return NULL;
159159
res = i+j; /* XXX Do something here */
160-
return newintobject(res);
160+
return PyInt_FromLong(res);
161161
}
162162

163163

164-
/* Function of no arguments returning new xxo object */
164+
/* Function of no arguments returning new Xxo object */
165165

166-
static object *
166+
static PyObject *
167167
xx_new(self, args)
168-
object *self; /* Not used */
169-
object *args;
168+
PyObject *self; /* Not used */
169+
PyObject *args;
170170
{
171171
int i, j;
172-
xxoobject *rv;
172+
XxoObject *rv;
173173

174-
if (!getnoarg(args))
174+
if (!PyArg_ParseTuple(args, ""))
175175
return NULL;
176-
rv = newxxoobject(args);
176+
rv = newXxoObject(args);
177177
if ( rv == NULL )
178178
return NULL;
179-
return (object *)rv;
179+
return (PyObject *)rv;
180180
}
181181

182182

183183
/* List of functions defined in the module */
184184

185-
static struct methodlist xx_methods[] = {
186-
{"foo", xx_foo},
187-
{"new", xx_new},
185+
static PyMethodDef xx_methods[] = {
186+
{"foo", xx_foo, 1},
187+
{"new", xx_new, 1},
188188
{NULL, NULL} /* sentinel */
189189
};
190190

@@ -194,17 +194,17 @@ static struct methodlist xx_methods[] = {
194194
void
195195
initxx()
196196
{
197-
object *m, *d;
197+
PyObject *m, *d;
198198

199199
/* Create the module and add the functions */
200-
m = initmodule("xx", xx_methods);
200+
m = Py_InitModule("xx", xx_methods);
201201

202202
/* Add some symbolic constants to the module */
203-
d = getmoduledict(m);
204-
ErrorObject = newstringobject("xx.error");
205-
dictinsert(d, "error", ErrorObject);
203+
d = PyModule_GetDict(m);
204+
ErrorObject = PyString_FromString("xx.error");
205+
PyDict_SetItemString(d, "error", ErrorObject);
206206

207207
/* Check for errors */
208-
if (err_occurred())
209-
fatal("can't initialize module xx");
208+
if (PyErr_Occurred())
209+
Py_FatalError("can't initialize module xx");
210210
}

0 commit comments

Comments
 (0)