Converted to new style

This commit is contained in:
Guido van Rossum 1996-07-30 16:56:16 +00:00
parent 037b940cd7
commit 2b65444b9f
1 changed files with 70 additions and 70 deletions

View File

@ -23,7 +23,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/ ******************************************************************/
/* Use this file as a template to start implementing a module that /* Use this file as a template to start implementing a module that
also declares objects types. All occurrences of 'xxo' should be changed also declares objects types. All occurrences of 'Xxo' should be changed
to something reasonable for your objects. After that, all other to something reasonable for your objects. After that, all other
occurrences of 'xx' should be changed to something reasonable for your occurrences of 'xx' should be changed to something reasonable for your
module. If your module is named foo your sourcefile should be named 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.
/* Xxo objects */ /* Xxo objects */
#include "allobjects.h" #include "Python.h"
static object *ErrorObject; static PyObject *ErrorObject;
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
object *x_attr; /* Attributes dictionary */ PyObject *x_attr; /* Attributes dictionary */
} xxoobject; } XxoObject;
staticforward typeobject Xxotype; staticforward PyTypeObject Xxo_Type;
#define is_xxoobject(v) ((v)->ob_type == &Xxotype) #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
static xxoobject * static XxoObject *
newxxoobject(arg) newXxoObject(arg)
object *arg; PyObject *arg;
{ {
xxoobject *self; XxoObject *self;
self = NEWOBJ(xxoobject, &Xxotype); self = PyObject_NEW(XxoObject, &Xxo_Type);
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->x_attr = NULL; self->x_attr = NULL;
@ -65,77 +65,77 @@ newxxoobject(arg)
/* Xxo methods */ /* Xxo methods */
static void static void
xxo_dealloc(self) Xxo_dealloc(self)
xxoobject *self; XxoObject *self;
{ {
XDECREF(self->x_attr); Py_XDECREF(self->x_attr);
DEL(self); PyMem_DEL(self);
} }
static object * static PyObject *
xxo_demo(self, args) Xxo_demo(self, args)
xxoobject *self; XxoObject *self;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static struct methodlist xxo_methods[] = { static PyMethodDef Xxo_methods[] = {
{"demo", (method)xxo_demo}, {"demo", (PyCFunction)Xxo_demo, 1},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
xxo_getattr(self, name) Xxo_getattr(self, name)
xxoobject *self; XxoObject *self;
char *name; char *name;
{ {
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
object *v = dictlookup(self->x_attr, name); PyObject *v = PyDict_GetItemString(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
INCREF(v); Py_INCREF(v);
return v; return v;
} }
} }
return findmethod(xxo_methods, (object *)self, name); return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
} }
static int static int
xxo_setattr(self, name, v) Xxo_setattr(self, name, v)
xxoobject *self; XxoObject *self;
char *name; char *name;
object *v; PyObject *v;
{ {
if (self->x_attr == NULL) { if (self->x_attr == NULL) {
self->x_attr = newdictobject(); self->x_attr = PyDict_New();
if (self->x_attr == NULL) if (self->x_attr == NULL)
return -1; return -1;
} }
if (v == NULL) { if (v == NULL) {
int rv = dictremove(self->x_attr, name); int rv = PyDict_DelItemString(self->x_attr, name);
if (rv < 0) if (rv < 0)
err_setstr(AttributeError, PyErr_SetString(PyExc_AttributeError,
"delete non-existing xxo attribute"); "delete non-existing Xxo attribute");
return rv; return rv;
} }
else else
return dictinsert(self->x_attr, name, v); return PyDict_SetItemString(self->x_attr, name, v);
} }
staticforward typeobject Xxotype = { staticforward PyTypeObject Xxo_Type = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"xxo", /*tp_name*/ "Xxo", /*tp_name*/
sizeof(xxoobject), /*tp_basicsize*/ sizeof(XxoObject), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)xxo_dealloc, /*tp_dealloc*/ (destructor)Xxo_dealloc, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
(getattrfunc)xxo_getattr, /*tp_getattr*/ (getattrfunc)Xxo_getattr, /*tp_getattr*/
(setattrfunc)xxo_setattr, /*tp_setattr*/ (setattrfunc)Xxo_setattr, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
0, /*tp_repr*/ 0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
@ -147,44 +147,44 @@ staticforward typeobject Xxotype = {
/* Function of two integers returning integer */ /* Function of two integers returning integer */
static object * static PyObject *
xx_foo(self, args) xx_foo(self, args)
object *self; /* Not used */ PyObject *self; /* Not used */
object *args; PyObject *args;
{ {
long i, j; long i, j;
long res; long res;
if (!getargs(args, "(ll)", &i, &j)) if (!PyArg_ParseTuple(args, "ll", &i, &j))
return NULL; return NULL;
res = i+j; /* XXX Do something here */ res = i+j; /* XXX Do something here */
return newintobject(res); return PyInt_FromLong(res);
} }
/* Function of no arguments returning new xxo object */ /* Function of no arguments returning new Xxo object */
static object * static PyObject *
xx_new(self, args) xx_new(self, args)
object *self; /* Not used */ PyObject *self; /* Not used */
object *args; PyObject *args;
{ {
int i, j; int i, j;
xxoobject *rv; XxoObject *rv;
if (!getnoarg(args)) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
rv = newxxoobject(args); rv = newXxoObject(args);
if ( rv == NULL ) if ( rv == NULL )
return NULL; return NULL;
return (object *)rv; return (PyObject *)rv;
} }
/* List of functions defined in the module */ /* List of functions defined in the module */
static struct methodlist xx_methods[] = { static PyMethodDef xx_methods[] = {
{"foo", xx_foo}, {"foo", xx_foo, 1},
{"new", xx_new}, {"new", xx_new, 1},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
@ -194,17 +194,17 @@ static struct methodlist xx_methods[] = {
void void
initxx() initxx()
{ {
object *m, *d; PyObject *m, *d;
/* Create the module and add the functions */ /* Create the module and add the functions */
m = initmodule("xx", xx_methods); m = Py_InitModule("xx", xx_methods);
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = getmoduledict(m); d = PyModule_GetDict(m);
ErrorObject = newstringobject("xx.error"); ErrorObject = PyString_FromString("xx.error");
dictinsert(d, "error", ErrorObject); PyDict_SetItemString(d, "error", ErrorObject);
/* Check for errors */ /* Check for errors */
if (err_occurred()) if (PyErr_Occurred())
fatal("can't initialize module xx"); Py_FatalError("can't initialize module xx");
} }