More C++-compliance. Note especially listobject.c - to get C++ to accept the

PyTypeObject structures, I had to make prototypes for the functions, and
move the structure definition ahead of the functions. I'd dearly like a better
way to do this - to change this would make for a massive set of changes to
the codebase.

There's still some warnings - this is purely to get rid of errors first.
This commit is contained in:
Anthony Baxter 2006-04-11 06:54:30 +00:00
parent bbfe4fad36
commit 377be11ee1
8 changed files with 148 additions and 137 deletions

View File

@ -169,7 +169,7 @@ PyBuffer_New(Py_ssize_t size)
}
/* XXX: check for overflow in multiply */
/* Inline PyObject_New */
o = PyObject_MALLOC(sizeof(*b) + size);
o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size);
if ( o == NULL )
return PyErr_NoMemory();
b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type);
@ -305,7 +305,7 @@ buffer_str(PyBufferObject *self)
Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return NULL;
return PyString_FromStringAndSize(ptr, size);
return PyString_FromStringAndSize((const char *)ptr, size);
}
/* Sequence methods */

View File

@ -208,7 +208,7 @@ class_getattr(register PyClassObject *op, PyObject *name)
{
register PyObject *v;
register char *sname = PyString_AsString(name);
PyClassObject *class;
PyClassObject *klass;
descrgetfunc f;
if (sname[0] == '_' && sname[1] == '_') {
@ -234,7 +234,7 @@ class_getattr(register PyClassObject *op, PyObject *name)
return v;
}
}
v = class_lookup(op, name, &class);
v = class_lookup(op, name, &klass);
if (v == NULL) {
PyErr_Format(PyExc_AttributeError,
"class %.50s has no attribute '%.400s'",
@ -481,23 +481,23 @@ PyTypeObject PyClass_Type = {
};
int
PyClass_IsSubclass(PyObject *class, PyObject *base)
PyClass_IsSubclass(PyObject *klass, PyObject *base)
{
Py_ssize_t i, n;
PyClassObject *cp;
if (class == base)
if (klass == base)
return 1;
if (PyTuple_Check(base)) {
n = PyTuple_GET_SIZE(base);
for (i = 0; i < n; i++) {
if (PyClass_IsSubclass(class, PyTuple_GET_ITEM(base, i)))
if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i)))
return 1;
}
return 0;
}
if (class == NULL || !PyClass_Check(class))
if (klass == NULL || !PyClass_Check(klass))
return 0;
cp = (PyClassObject *)class;
cp = (PyClassObject *)klass;
n = PyTuple_Size(cp->cl_bases);
for (i = 0; i < n; i++) {
if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
@ -719,7 +719,7 @@ static PyObject *
instance_getattr2(register PyInstanceObject *inst, PyObject *name)
{
register PyObject *v;
PyClassObject *class;
PyClassObject *klass;
descrgetfunc f;
v = PyDict_GetItem(inst->in_dict, name);
@ -727,7 +727,7 @@ instance_getattr2(register PyInstanceObject *inst, PyObject *name)
Py_INCREF(v);
return v;
}
v = class_lookup(inst->in_class, name, &class);
v = class_lookup(inst->in_class, name, &klass);
if (v != NULL) {
Py_INCREF(v);
f = TP_DESCR_GET(v->ob_type);
@ -767,7 +767,7 @@ PyObject *
_PyInstance_Lookup(PyObject *pinst, PyObject *name)
{
PyObject *v;
PyClassObject *class;
PyClassObject *klass;
PyInstanceObject *inst; /* pinst cast to the right type */
assert(PyInstance_Check(pinst));
@ -777,7 +777,7 @@ _PyInstance_Lookup(PyObject *pinst, PyObject *name)
v = PyDict_GetItem(inst->in_dict, name);
if (v == NULL)
v = class_lookup(inst->in_class, name, &class);
v = class_lookup(inst->in_class, name, &klass);
return v;
}
@ -2123,7 +2123,7 @@ PyTypeObject PyInstance_Type = {
static PyMethodObject *free_list;
PyObject *
PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
{
register PyMethodObject *im;
if (!PyCallable_Check(func)) {
@ -2145,8 +2145,8 @@ PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
im->im_func = func;
Py_XINCREF(self);
im->im_self = self;
Py_XINCREF(class);
im->im_class = class;
Py_XINCREF(klass);
im->im_class = klass;
_PyObject_GC_TRACK(im);
return (PyObject *)im;
}
@ -2368,15 +2368,15 @@ instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
}
static void
getclassname(PyObject *class, char *buf, int bufsize)
getclassname(PyObject *klass, char *buf, int bufsize)
{
PyObject *name;
assert(bufsize > 1);
strcpy(buf, "?"); /* Default outcome */
if (class == NULL)
if (klass == NULL)
return;
name = PyObject_GetAttrString(class, "__name__");
name = PyObject_GetAttrString(klass, "__name__");
if (name == NULL) {
/* This function cannot return an exception */
PyErr_Clear();
@ -2392,7 +2392,7 @@ getclassname(PyObject *class, char *buf, int bufsize)
static void
getinstclassname(PyObject *inst, char *buf, int bufsize)
{
PyObject *class;
PyObject *klass;
if (inst == NULL) {
assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
@ -2400,22 +2400,22 @@ getinstclassname(PyObject *inst, char *buf, int bufsize)
return;
}
class = PyObject_GetAttrString(inst, "__class__");
if (class == NULL) {
klass = PyObject_GetAttrString(inst, "__class__");
if (klass == NULL) {
/* This function cannot return an exception */
PyErr_Clear();
class = (PyObject *)(inst->ob_type);
Py_INCREF(class);
klass = (PyObject *)(inst->ob_type);
Py_INCREF(klass);
}
getclassname(class, buf, bufsize);
Py_XDECREF(class);
getclassname(klass, buf, bufsize);
Py_XDECREF(klass);
}
static PyObject *
instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
{
PyObject *self = PyMethod_GET_SELF(func);
PyObject *class = PyMethod_GET_CLASS(func);
PyObject *klass = PyMethod_GET_CLASS(func);
PyObject *result;
func = PyMethod_GET_FUNCTION(func);
@ -2428,14 +2428,14 @@ instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
if (self == NULL)
ok = 0;
else {
ok = PyObject_IsInstance(self, class);
ok = PyObject_IsInstance(self, klass);
if (ok < 0)
return NULL;
}
if (!ok) {
char clsbuf[256];
char instbuf[256];
getclassname(class, clsbuf, sizeof(clsbuf));
getclassname(klass, clsbuf, sizeof(clsbuf));
getinstclassname(self, instbuf, sizeof(instbuf));
PyErr_Format(PyExc_TypeError,
"unbound method %s%s must be called with "

View File

@ -9,8 +9,6 @@ typedef struct {
PyObject* en_result; /* result tuple */
} enumobject;
PyTypeObject PyEnum_Type;
static PyObject *
enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{

View File

@ -313,7 +313,8 @@ PyFile_SetBufSize(PyObject *f, int bufsize)
PyMem_Free(file->f_setbuf);
file->f_setbuf = NULL;
} else {
file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
bufsize);
}
#ifdef HAVE_SETVBUF
setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
@ -1391,7 +1392,7 @@ file_readlines(PyFileObject *f, PyObject *args)
goto cleanup;
}
totalread += nread;
p = memchr(buffer+nfilled, '\n', nread);
p = (char *)memchr(buffer+nfilled, '\n', nread);
if (p == NULL) {
/* Need a larger buffer to fit this line */
nfilled += nread;
@ -1431,7 +1432,7 @@ file_readlines(PyFileObject *f, PyObject *args)
if (err != 0)
goto error;
q = p;
p = memchr(q, '\n', end-q);
p = (char *)memchr(q, '\n', end-q);
} while (p != NULL);
/* Move the remaining incomplete line to the start */
nfilled = end-q;
@ -1809,7 +1810,7 @@ readahead(PyFileObject *f, int bufsize)
else
drop_readahead(f);
}
if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
PyErr_NoMemory();
return -1;
}
@ -1852,7 +1853,7 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
if (len == 0)
return (PyStringObject *)
PyString_FromStringAndSize(NULL, skip);
bufptr = memchr(f->f_bufptr, '\n', len);
bufptr = (char *)memchr(f->f_bufptr, '\n', len);
if (bufptr != NULL) {
bufptr++; /* Count the '\n' */
len = bufptr - f->f_bufptr;

View File

@ -959,21 +959,21 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *tmp, *new;
PyObject *tmp, *newobj;
assert(PyType_IsSubtype(type, &PyFloat_Type));
tmp = float_new(&PyFloat_Type, args, kwds);
if (tmp == NULL)
return NULL;
assert(PyFloat_CheckExact(tmp));
new = type->tp_alloc(type, 0);
if (new == NULL) {
newobj = type->tp_alloc(type, 0);
if (newobj == NULL) {
Py_DECREF(tmp);
return NULL;
}
((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
Py_DECREF(tmp);
return new;
return newobj;
}
static PyObject *

View File

@ -376,7 +376,7 @@ PyObject *
PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
{
PyObject *result;
char *buffer = PyMem_MALLOC(length+1);
char *buffer = (char *)PyMem_MALLOC(length+1);
if (buffer == NULL)
return NULL;
@ -982,7 +982,7 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *tmp, *new;
PyObject *tmp, *newobj;
long ival;
assert(PyType_IsSubtype(type, &PyInt_Type));
@ -999,14 +999,14 @@ int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ival = ((PyIntObject *)tmp)->ob_ival;
}
new = type->tp_alloc(type, 0);
if (new == NULL) {
newobj = type->tp_alloc(type, 0);
if (newobj == NULL) {
Py_DECREF(tmp);
return NULL;
}
((PyIntObject *)new)->ob_ival = ival;
((PyIntObject *)newobj)->ob_ival = ival;
Py_DECREF(tmp);
return new;
return newobj;
}
static PyObject *

View File

@ -1805,28 +1805,11 @@ typedef struct {
PyObject *value;
} sortwrapperobject;
static PyTypeObject sortwrapper_type;
static PyObject *
sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
{
if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
PyErr_SetString(PyExc_TypeError,
"expected a sortwrapperobject");
return NULL;
}
return PyObject_RichCompare(a->key, b->key, op);
}
static void
sortwrapper_dealloc(sortwrapperobject *so)
{
Py_XDECREF(so->key);
Py_XDECREF(so->value);
PyObject_Del(so);
}
PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
static PyObject *
sortwrapper_richcompare(sortwrapperobject *, sortwrapperobject *, int);
static void
sortwrapper_dealloc(sortwrapperobject *);
static PyTypeObject sortwrapper_type = {
PyObject_HEAD_INIT(&PyType_Type)
@ -1858,6 +1841,26 @@ static PyTypeObject sortwrapper_type = {
(richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
};
static PyObject *
sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
{
if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
PyErr_SetString(PyExc_TypeError,
"expected a sortwrapperobject");
return NULL;
}
return PyObject_RichCompare(a->key, b->key, op);
}
static void
sortwrapper_dealloc(sortwrapperobject *so)
{
Py_XDECREF(so->key);
Py_XDECREF(so->value);
PyObject_Del(so);
}
/* Returns a new reference to a sortwrapper.
Consumes the references to the two underlying objects. */
@ -2698,7 +2701,53 @@ typedef struct {
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listiterobject;
PyTypeObject PyListIter_Type;
static PyObject *list_iter(PyObject *);
static void listiter_dealloc(listiterobject *);
static int listiter_traverse(listiterobject *, visitproc, void *);
static PyObject *listiter_next(listiterobject *);
static PyObject *listiter_len(listiterobject *);
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef listiter_methods[] = {
{"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
{NULL, NULL} /* sentinel */
};
PyTypeObject PyListIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"listiterator", /* tp_name */
sizeof(listiterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)listiter_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
0, /* tp_doc */
(traverseproc)listiter_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)listiter_next, /* tp_iternext */
listiter_methods, /* tp_methods */
0, /* tp_members */
};
static PyObject *
list_iter(PyObject *seq)
@ -2770,29 +2819,40 @@ listiter_len(listiterobject *it)
}
return PyInt_FromLong(0);
}
/*********************** List Reverse Iterator **************************/
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
typedef struct {
PyObject_HEAD
Py_ssize_t it_index;
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listreviterobject;
static PyMethodDef listiter_methods[] = {
{"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
{NULL, NULL} /* sentinel */
static PyObject *list_reversed(PyListObject *, PyObject *);
static void listreviter_dealloc(listreviterobject *);
static int listreviter_traverse(listreviterobject *, visitproc, void *);
static PyObject *listreviter_next(listreviterobject *);
static Py_ssize_t listreviter_len(listreviterobject *);
static PySequenceMethods listreviter_as_sequence = {
(lenfunc)listreviter_len, /* sq_length */
0, /* sq_concat */
};
PyTypeObject PyListIter_Type = {
PyTypeObject PyListRevIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"listiterator", /* tp_name */
sizeof(listiterobject), /* tp_basicsize */
"listreverseiterator", /* tp_name */
sizeof(listreviterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)listiter_dealloc, /* tp_dealloc */
(destructor)listreviter_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
&listreviter_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
@ -2802,26 +2862,15 @@ PyTypeObject PyListIter_Type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
0, /* tp_doc */
(traverseproc)listiter_traverse, /* tp_traverse */
(traverseproc)listreviter_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)listiter_next, /* tp_iternext */
listiter_methods, /* tp_methods */
0, /* tp_members */
(iternextfunc)listreviter_next, /* tp_iternext */
0,
};
/*********************** List Reverse Iterator **************************/
typedef struct {
PyObject_HEAD
Py_ssize_t it_index;
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listreviterobject;
PyTypeObject PyListRevIter_Type;
static PyObject *
list_reversed(PyListObject *seq, PyObject *unused)
{
@ -2884,40 +2933,3 @@ listreviter_len(listreviterobject *it)
return len;
}
static PySequenceMethods listreviter_as_sequence = {
(lenfunc)listreviter_len, /* sq_length */
0, /* sq_concat */
};
PyTypeObject PyListRevIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"listreverseiterator", /* tp_name */
sizeof(listreviterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)listreviter_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
&listreviter_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
0, /* tp_doc */
(traverseproc)listreviter_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)listreviter_next, /* tp_iternext */
0,
};

View File

@ -1476,7 +1476,7 @@ PyObject *
PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
{
PyObject *result;
char *buffer = PyMem_MALLOC(length+1);
char *buffer = (char *)PyMem_MALLOC(length+1);
if (buffer == NULL)
return NULL;
@ -3088,7 +3088,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyLongObject *tmp, *new;
PyLongObject *tmp, *newobj;
Py_ssize_t i, n;
assert(PyType_IsSubtype(type, &PyLong_Type));
@ -3099,17 +3099,17 @@ long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
n = tmp->ob_size;
if (n < 0)
n = -n;
new = (PyLongObject *)type->tp_alloc(type, n);
if (new == NULL) {
newobj = (PyLongObject *)type->tp_alloc(type, n);
if (newobj == NULL) {
Py_DECREF(tmp);
return NULL;
}
assert(PyLong_Check(new));
new->ob_size = tmp->ob_size;
assert(PyLong_Check(newobj));
newobj->ob_size = tmp->ob_size;
for (i = 0; i < n; i++)
new->ob_digit[i] = tmp->ob_digit[i];
newobj->ob_digit[i] = tmp->ob_digit[i];
Py_DECREF(tmp);
return (PyObject *)new;
return (PyObject *)newobj;
}
static PyObject *