Skip to content

Commit 0ddc283

Browse files
committed
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.
1 parent 74c273d commit 0ddc283

8 files changed

Lines changed: 147 additions & 136 deletions

File tree

Objects/bufferobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ PyBuffer_New(Py_ssize_t size)
169169
}
170170
/* XXX: check for overflow in multiply */
171171
/* Inline PyObject_New */
172-
o = PyObject_MALLOC(sizeof(*b) + size);
172+
o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size);
173173
if ( o == NULL )
174174
return PyErr_NoMemory();
175175
b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type);
@@ -305,7 +305,7 @@ buffer_str(PyBufferObject *self)
305305
Py_ssize_t size;
306306
if (!get_buf(self, &ptr, &size))
307307
return NULL;
308-
return PyString_FromStringAndSize(ptr, size);
308+
return PyString_FromStringAndSize((const char *)ptr, size);
309309
}
310310

311311
/* Sequence methods */

Objects/classobject.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class_getattr(register PyClassObject *op, PyObject *name)
208208
{
209209
register PyObject *v;
210210
register char *sname = PyString_AsString(name);
211-
PyClassObject *class;
211+
PyClassObject *klass;
212212
descrgetfunc f;
213213

214214
if (sname[0] == '_' && sname[1] == '_') {
@@ -234,7 +234,7 @@ class_getattr(register PyClassObject *op, PyObject *name)
234234
return v;
235235
}
236236
}
237-
v = class_lookup(op, name, &class);
237+
v = class_lookup(op, name, &klass);
238238
if (v == NULL) {
239239
PyErr_Format(PyExc_AttributeError,
240240
"class %.50s has no attribute '%.400s'",
@@ -481,23 +481,23 @@ PyTypeObject PyClass_Type = {
481481
};
482482

483483
int
484-
PyClass_IsSubclass(PyObject *class, PyObject *base)
484+
PyClass_IsSubclass(PyObject *klass, PyObject *base)
485485
{
486486
Py_ssize_t i, n;
487487
PyClassObject *cp;
488-
if (class == base)
488+
if (klass == base)
489489
return 1;
490490
if (PyTuple_Check(base)) {
491491
n = PyTuple_GET_SIZE(base);
492492
for (i = 0; i < n; i++) {
493-
if (PyClass_IsSubclass(class, PyTuple_GET_ITEM(base, i)))
493+
if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i)))
494494
return 1;
495495
}
496496
return 0;
497497
}
498-
if (class == NULL || !PyClass_Check(class))
498+
if (klass == NULL || !PyClass_Check(klass))
499499
return 0;
500-
cp = (PyClassObject *)class;
500+
cp = (PyClassObject *)klass;
501501
n = PyTuple_Size(cp->cl_bases);
502502
for (i = 0; i < n; i++) {
503503
if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
@@ -719,15 +719,15 @@ static PyObject *
719719
instance_getattr2(register PyInstanceObject *inst, PyObject *name)
720720
{
721721
register PyObject *v;
722-
PyClassObject *class;
722+
PyClassObject *klass;
723723
descrgetfunc f;
724724

725725
v = PyDict_GetItem(inst->in_dict, name);
726726
if (v != NULL) {
727727
Py_INCREF(v);
728728
return v;
729729
}
730-
v = class_lookup(inst->in_class, name, &class);
730+
v = class_lookup(inst->in_class, name, &klass);
731731
if (v != NULL) {
732732
Py_INCREF(v);
733733
f = TP_DESCR_GET(v->ob_type);
@@ -767,7 +767,7 @@ PyObject *
767767
_PyInstance_Lookup(PyObject *pinst, PyObject *name)
768768
{
769769
PyObject *v;
770-
PyClassObject *class;
770+
PyClassObject *klass;
771771
PyInstanceObject *inst; /* pinst cast to the right type */
772772

773773
assert(PyInstance_Check(pinst));
@@ -777,7 +777,7 @@ _PyInstance_Lookup(PyObject *pinst, PyObject *name)
777777

778778
v = PyDict_GetItem(inst->in_dict, name);
779779
if (v == NULL)
780-
v = class_lookup(inst->in_class, name, &class);
780+
v = class_lookup(inst->in_class, name, &klass);
781781
return v;
782782
}
783783

@@ -2123,7 +2123,7 @@ PyTypeObject PyInstance_Type = {
21232123
static PyMethodObject *free_list;
21242124

21252125
PyObject *
2126-
PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
2126+
PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
21272127
{
21282128
register PyMethodObject *im;
21292129
if (!PyCallable_Check(func)) {
@@ -2145,8 +2145,8 @@ PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
21452145
im->im_func = func;
21462146
Py_XINCREF(self);
21472147
im->im_self = self;
2148-
Py_XINCREF(class);
2149-
im->im_class = class;
2148+
Py_XINCREF(klass);
2149+
im->im_class = klass;
21502150
_PyObject_GC_TRACK(im);
21512151
return (PyObject *)im;
21522152
}
@@ -2368,15 +2368,15 @@ instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
23682368
}
23692369

23702370
static void
2371-
getclassname(PyObject *class, char *buf, int bufsize)
2371+
getclassname(PyObject *klass, char *buf, int bufsize)
23722372
{
23732373
PyObject *name;
23742374

23752375
assert(bufsize > 1);
23762376
strcpy(buf, "?"); /* Default outcome */
2377-
if (class == NULL)
2377+
if (klass == NULL)
23782378
return;
2379-
name = PyObject_GetAttrString(class, "__name__");
2379+
name = PyObject_GetAttrString(klass, "__name__");
23802380
if (name == NULL) {
23812381
/* This function cannot return an exception */
23822382
PyErr_Clear();
@@ -2392,30 +2392,30 @@ getclassname(PyObject *class, char *buf, int bufsize)
23922392
static void
23932393
getinstclassname(PyObject *inst, char *buf, int bufsize)
23942394
{
2395-
PyObject *class;
2395+
PyObject *klass;
23962396

23972397
if (inst == NULL) {
23982398
assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
23992399
strcpy(buf, "nothing");
24002400
return;
24012401
}
24022402

2403-
class = PyObject_GetAttrString(inst, "__class__");
2404-
if (class == NULL) {
2403+
klass = PyObject_GetAttrString(inst, "__class__");
2404+
if (klass == NULL) {
24052405
/* This function cannot return an exception */
24062406
PyErr_Clear();
2407-
class = (PyObject *)(inst->ob_type);
2408-
Py_INCREF(class);
2407+
klass = (PyObject *)(inst->ob_type);
2408+
Py_INCREF(klass);
24092409
}
2410-
getclassname(class, buf, bufsize);
2411-
Py_XDECREF(class);
2410+
getclassname(klass, buf, bufsize);
2411+
Py_XDECREF(klass);
24122412
}
24132413

24142414
static PyObject *
24152415
instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
24162416
{
24172417
PyObject *self = PyMethod_GET_SELF(func);
2418-
PyObject *class = PyMethod_GET_CLASS(func);
2418+
PyObject *klass = PyMethod_GET_CLASS(func);
24192419
PyObject *result;
24202420

24212421
func = PyMethod_GET_FUNCTION(func);
@@ -2428,14 +2428,14 @@ instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
24282428
if (self == NULL)
24292429
ok = 0;
24302430
else {
2431-
ok = PyObject_IsInstance(self, class);
2431+
ok = PyObject_IsInstance(self, klass);
24322432
if (ok < 0)
24332433
return NULL;
24342434
}
24352435
if (!ok) {
24362436
char clsbuf[256];
24372437
char instbuf[256];
2438-
getclassname(class, clsbuf, sizeof(clsbuf));
2438+
getclassname(klass, clsbuf, sizeof(clsbuf));
24392439
getinstclassname(self, instbuf, sizeof(instbuf));
24402440
PyErr_Format(PyExc_TypeError,
24412441
"unbound method %s%s must be called with "

Objects/enumobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ typedef struct {
99
PyObject* en_result; /* result tuple */
1010
} enumobject;
1111

12-
PyTypeObject PyEnum_Type;
13-
1412
static PyObject *
1513
enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1614
{

Objects/fileobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ PyFile_SetBufSize(PyObject *f, int bufsize)
313313
PyMem_Free(file->f_setbuf);
314314
file->f_setbuf = NULL;
315315
} else {
316-
file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
316+
file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
317+
bufsize);
317318
}
318319
#ifdef HAVE_SETVBUF
319320
setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
@@ -1391,7 +1392,7 @@ file_readlines(PyFileObject *f, PyObject *args)
13911392
goto cleanup;
13921393
}
13931394
totalread += nread;
1394-
p = memchr(buffer+nfilled, '\n', nread);
1395+
p = (char *)memchr(buffer+nfilled, '\n', nread);
13951396
if (p == NULL) {
13961397
/* Need a larger buffer to fit this line */
13971398
nfilled += nread;
@@ -1431,7 +1432,7 @@ file_readlines(PyFileObject *f, PyObject *args)
14311432
if (err != 0)
14321433
goto error;
14331434
q = p;
1434-
p = memchr(q, '\n', end-q);
1435+
p = (char *)memchr(q, '\n', end-q);
14351436
} while (p != NULL);
14361437
/* Move the remaining incomplete line to the start */
14371438
nfilled = end-q;
@@ -1809,7 +1810,7 @@ readahead(PyFileObject *f, int bufsize)
18091810
else
18101811
drop_readahead(f);
18111812
}
1812-
if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1813+
if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
18131814
PyErr_NoMemory();
18141815
return -1;
18151816
}
@@ -1852,7 +1853,7 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
18521853
if (len == 0)
18531854
return (PyStringObject *)
18541855
PyString_FromStringAndSize(NULL, skip);
1855-
bufptr = memchr(f->f_bufptr, '\n', len);
1856+
bufptr = (char *)memchr(f->f_bufptr, '\n', len);
18561857
if (bufptr != NULL) {
18571858
bufptr++; /* Count the '\n' */
18581859
len = bufptr - f->f_bufptr;

Objects/floatobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -959,21 +959,21 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
959959
static PyObject *
960960
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
961961
{
962-
PyObject *tmp, *new;
962+
PyObject *tmp, *newobj;
963963

964964
assert(PyType_IsSubtype(type, &PyFloat_Type));
965965
tmp = float_new(&PyFloat_Type, args, kwds);
966966
if (tmp == NULL)
967967
return NULL;
968968
assert(PyFloat_CheckExact(tmp));
969-
new = type->tp_alloc(type, 0);
970-
if (new == NULL) {
969+
newobj = type->tp_alloc(type, 0);
970+
if (newobj == NULL) {
971971
Py_DECREF(tmp);
972972
return NULL;
973973
}
974-
((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
974+
((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
975975
Py_DECREF(tmp);
976-
return new;
976+
return newobj;
977977
}
978978

979979
static PyObject *

Objects/intobject.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ PyObject *
376376
PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
377377
{
378378
PyObject *result;
379-
char *buffer = PyMem_MALLOC(length+1);
379+
char *buffer = (char *)PyMem_MALLOC(length+1);
380380

381381
if (buffer == NULL)
382382
return NULL;
@@ -982,7 +982,7 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
982982
static PyObject *
983983
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
984984
{
985-
PyObject *tmp, *new;
985+
PyObject *tmp, *newobj;
986986
long ival;
987987

988988
assert(PyType_IsSubtype(type, &PyInt_Type));
@@ -999,14 +999,14 @@ int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
999999
ival = ((PyIntObject *)tmp)->ob_ival;
10001000
}
10011001

1002-
new = type->tp_alloc(type, 0);
1003-
if (new == NULL) {
1002+
newobj = type->tp_alloc(type, 0);
1003+
if (newobj == NULL) {
10041004
Py_DECREF(tmp);
10051005
return NULL;
10061006
}
1007-
((PyIntObject *)new)->ob_ival = ival;
1007+
((PyIntObject *)newobj)->ob_ival = ival;
10081008
Py_DECREF(tmp);
1009-
return new;
1009+
return newobj;
10101010
}
10111011

10121012
static PyObject *

0 commit comments

Comments
 (0)