diff options
author | Guido van Rossum <guido@python.org> | 1997-05-02 03:12:38 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-05-02 03:12:38 (GMT) |
commit | c0b618a2ccfb0fc39e07ee96dc09da77fbcce1b1 (patch) | |
tree | 41fcbbf4aa993b2172cce9c5cb26021268c82bd8 /Objects | |
parent | f4806c2a85dccb94a183b584f99b6b5b8f40e6db (diff) | |
download | cpython-c0b618a2ccfb0fc39e07ee96dc09da77fbcce1b1.zip cpython-c0b618a2ccfb0fc39e07ee96dc09da77fbcce1b1.tar.gz cpython-c0b618a2ccfb0fc39e07ee96dc09da77fbcce1b1.tar.bz2 |
Quickly renamed the last directory.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 22 | ||||
-rw-r--r-- | Objects/classobject.c | 1062 | ||||
-rw-r--r-- | Objects/complexobject.c | 218 | ||||
-rw-r--r-- | Objects/dictobject.c | 513 | ||||
-rw-r--r-- | Objects/fileobject.c | 591 | ||||
-rw-r--r-- | Objects/floatobject.c | 231 | ||||
-rw-r--r-- | Objects/funcobject.c | 147 | ||||
-rw-r--r-- | Objects/intobject.c | 319 | ||||
-rw-r--r-- | Objects/listobject.c | 583 | ||||
-rw-r--r-- | Objects/longobject.c | 708 | ||||
-rw-r--r-- | Objects/mappingobject.c | 513 | ||||
-rw-r--r-- | Objects/methodobject.c | 156 | ||||
-rw-r--r-- | Objects/moduleobject.c | 127 | ||||
-rw-r--r-- | Objects/object.c | 289 | ||||
-rw-r--r-- | Objects/rangeobject.c | 81 | ||||
-rw-r--r-- | Objects/stringobject.c | 393 | ||||
-rw-r--r-- | Objects/tupleobject.c | 260 | ||||
-rw-r--r-- | Objects/typeobject.c | 30 |
18 files changed, 3155 insertions, 3088 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index e668b08..eebeacd 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -979,7 +979,7 @@ PyObject_CallObject(o, a) PyObject * #ifdef HAVE_STDARG_PROTOTYPES /* VARARGS 2 */ -PyObject_CallFunction(PyObject *callable, char *format, ...) +PyObject_CallFunction(PyObject *PyCallable_Check, char *format, ...) #else /* VARARGS */ PyObject_CallFunction(va_alist) va_dcl @@ -990,14 +990,14 @@ PyObject_CallFunction(va_alist) va_dcl #ifdef HAVE_STDARG_PROTOTYPES va_start(va, format); #else - PyObject *callable; + PyObject *PyCallable_Check; char *format; va_start(va); - callable = va_arg(va, PyObject *); + PyCallable_Check = va_arg(va, PyObject *); format = va_arg(va, char *); #endif - if( ! callable) + if( ! PyCallable_Check) { va_end(va); return Py_ReturnNullError(); @@ -1019,7 +1019,7 @@ PyObject_CallFunction(va_alist) va_dcl Py_TRY(PyTuple_SetItem(a,0,args) != -1); args=a; } - retval = PyObject_CallObject(callable,args); + retval = PyObject_CallObject(PyCallable_Check,args); Py_DECREF(args); return retval; } @@ -1034,7 +1034,7 @@ PyObject_CallMethod(va_alist) va_dcl #endif { va_list va; - PyObject *args, *method=0, *retval; + PyObject *args, *PyCFunction=0, *retval; #ifdef HAVE_STDARG_PROTOTYPES va_start(va, format); #else @@ -1053,15 +1053,15 @@ PyObject_CallMethod(va_alist) va_dcl return Py_ReturnNullError(); } - method=PyObject_GetAttrString(o,name); - if(! method) + PyCFunction=PyObject_GetAttrString(o,name); + if(! PyCFunction) { va_end(va); PyErr_SetString(PyExc_AttributeError,name); return 0; } - if(! (PyCallable_Check(method))) + if(! (PyCallable_Check(PyCFunction))) { va_end(va); PyErr_SetString(PyExc_TypeError,"call of non-callable attribute"); @@ -1086,9 +1086,9 @@ PyObject_CallMethod(va_alist) va_dcl args=a; } - retval = PyObject_CallObject(method,args); + retval = PyObject_CallObject(PyCFunction,args); Py_DECREF(args); - Py_DECREF(method); + Py_DECREF(PyCFunction); return retval; } diff --git a/Objects/classobject.c b/Objects/classobject.c index d78b758..72f8c11 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -31,51 +31,52 @@ PERFORMANCE OF THIS SOFTWARE. /* Class object implementation */ -#include "allobjects.h" +#include "Python.h" #include "structmember.h" /* Forward */ -static object *class_lookup PROTO((classobject *, object *, classobject **)); -static object *instance_getattr1 PROTO((instanceobject *, object *)); - -object * -newclassobject(bases, dict, name) - object *bases; /* NULL or tuple of classobjects! */ - object *dict; - object *name; /* String; NULL if unknown */ +static PyObject *class_lookup + Py_PROTO((PyClassObject *, PyObject *, PyClassObject **)); +static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *)); + +PyObject * +PyClass_New(bases, dict, name) + PyObject *bases; /* NULL or tuple of classobjects! */ + PyObject *dict; + PyObject *name; /* String; NULL if unknown */ { #ifdef SUPPORT_OBSOLETE_ACCESS int pos; - object *key, *value; + PyObject *key, *value; #endif - classobject *op, *dummy; - static object *getattrstr, *setattrstr, *delattrstr; - static object *docstr; + PyClassObject *op, *dummy; + static PyObject *getattrstr, *setattrstr, *delattrstr; + static PyObject *docstr; if (docstr == NULL) { docstr= PyString_InternFromString("__doc__"); if (docstr == NULL) return NULL; } - if (mappinglookup(dict, docstr) == NULL) { - if (mappinginsert(dict, docstr, None) < 0) + if (PyDict_GetItem(dict, docstr) == NULL) { + if (PyDict_SetItem(dict, docstr, Py_None) < 0) return NULL; } if (bases == NULL) { - bases = newtupleobject(0); + bases = PyTuple_New(0); if (bases == NULL) return NULL; } else - INCREF(bases); - op = NEWOBJ(classobject, &Classtype); + Py_INCREF(bases); + op = PyObject_NEW(PyClassObject, &PyClass_Type); if (op == NULL) { - DECREF(bases); + Py_DECREF(bases); return NULL; } op->cl_bases = bases; - INCREF(dict); + Py_INCREF(dict); op->cl_dict = dict; - XINCREF(name); + Py_XINCREF(name); op->cl_name = name; if (getattrstr == NULL) { getattrstr = PyString_InternFromString("__getattr__"); @@ -85,102 +86,103 @@ newclassobject(bases, dict, name) op->cl_getattr = class_lookup(op, getattrstr, &dummy); op->cl_setattr = class_lookup(op, setattrstr, &dummy); op->cl_delattr = class_lookup(op, delattrstr, &dummy); - XINCREF(op->cl_getattr); - XINCREF(op->cl_setattr); - XINCREF(op->cl_delattr); + Py_XINCREF(op->cl_getattr); + Py_XINCREF(op->cl_setattr); + Py_XINCREF(op->cl_delattr); #ifdef SUPPORT_OBSOLETE_ACCESS pos = 0; - while (mappinggetnext(dict, &pos, &key, &value)) { - if (is_accessobject(value)) - setaccessowner(value, (object *)op); + while (PyDict_Next(dict, &pos, &key, &value)) { + if (PyAccess_Check(value)) + PyAccess_SetOwner(value, (PyObject *)op); } #endif - return (object *) op; + return (PyObject *) op; } /* Class methods */ static void class_dealloc(op) - classobject *op; + PyClassObject *op; { - DECREF(op->cl_bases); - DECREF(op->cl_dict); - XDECREF(op->cl_name); + Py_DECREF(op->cl_bases); + Py_DECREF(op->cl_dict); + Py_XDECREF(op->cl_name); free((ANY *)op); } -static object * +static PyObject * class_lookup(cp, name, pclass) - classobject *cp; - object *name; - classobject **pclass; + PyClassObject *cp; + PyObject *name; + PyClassObject **pclass; { int i, n; - object *value = mappinglookup(cp->cl_dict, name); + PyObject *value = PyDict_GetItem(cp->cl_dict, name); if (value != NULL) { *pclass = cp; return value; } - n = gettuplesize(cp->cl_bases); + n = PyTuple_Size(cp->cl_bases); for (i = 0; i < n; i++) { - object *v = class_lookup((classobject *) - gettupleitem(cp->cl_bases, i), name, pclass); + PyObject *v = class_lookup( + (PyClassObject *) + PyTuple_GetItem(cp->cl_bases, i), name, pclass); if (v != NULL) return v; } return NULL; } -static object * +static PyObject * class_getattr(op, name) - register classobject *op; - object *name; + register PyClassObject *op; + PyObject *name; { - register object *v; - register char *sname = getstringvalue(name); - classobject *class; + register PyObject *v; + register char *sname = PyString_AsString(name); + PyClassObject *class; if (sname[0] == '_' && sname[1] == '_') { if (strcmp(sname, "__dict__") == 0) { - if (getrestricted()) { - err_setstr(RuntimeError, - "class.__dict__ not accessible in restricted mode"); + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "class.__dict__ not accessible in restricted mode"); return NULL; } - INCREF(op->cl_dict); + Py_INCREF(op->cl_dict); return op->cl_dict; } if (strcmp(sname, "__bases__") == 0) { - INCREF(op->cl_bases); + Py_INCREF(op->cl_bases); return op->cl_bases; } if (strcmp(sname, "__name__") == 0) { if (op->cl_name == NULL) - v = None; + v = Py_None; else v = op->cl_name; - INCREF(v); + Py_INCREF(v); return v; } } v = class_lookup(op, name, &class); if (v == NULL) { - err_setval(AttributeError, name); + PyErr_SetObject(PyExc_AttributeError, name); return NULL; } #ifdef SUPPORT_OBSOLETE_ACCESS - if (is_accessobject(v)) { - v = getaccessvalue(v, getowner()); + if (PyAccess_Check(v)) { + v = PyAccess_AsValue(v, PyEval_GetOwner()); if (v == NULL) return NULL; } else #endif - INCREF(v); - if (is_funcobject(v)) { - object *w = newinstancemethodobject(v, (object *)NULL, - (object *)class); - DECREF(v); + Py_INCREF(v); + if (PyFunction_Check(v)) { + PyObject *w = PyMethod_New(v, (PyObject *)NULL, + (PyObject *)class); + Py_DECREF(v); v = w; } return v; @@ -188,61 +190,62 @@ class_getattr(op, name) static int class_setattr(op, name, v) - classobject *op; - object *name; - object *v; + PyClassObject *op; + PyObject *name; + PyObject *v; { #ifdef SUPPORT_OBSOLETE_ACCESS - object *ac; + PyObject *ac; #endif - char *sname = getstringvalue(name); + char *sname = PyString_AsString(name); if (sname[0] == '_' && sname[1] == '_') { - int n = getstringsize(name); + int n = PyString_Size(name); if (sname[n-1] == '_' && sname[n-2] == '_') { - err_setstr(TypeError, "read-only special attribute"); + PyErr_SetString(PyExc_TypeError, + "read-only special attribute"); return -1; } } - if (getrestricted()) { - err_setstr(RuntimeError, + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, "classes are read-only in restricted mode"); return -1; } #ifdef SUPPORT_OBSOLETE_ACCESS - ac = mappinglookup(op->cl_dict, name); - if (ac != NULL && is_accessobject(ac)) - return setaccessvalue(ac, getowner(), v); + ac = PyDict_GetItem(op->cl_dict, name); + if (ac != NULL && PyAccess_Check(ac)) + return PyAccess_SetValue(ac, PyEval_GetOwner(), v); #endif if (v == NULL) { - int rv = mappingremove(op->cl_dict, name); + int rv = PyDict_DelItem(op->cl_dict, name); if (rv < 0) - err_setstr(AttributeError, + PyErr_SetString(PyExc_AttributeError, "delete non-existing class attribute"); return rv; } else - return mappinginsert(op->cl_dict, name, v); + return PyDict_SetItem(op->cl_dict, name, v); } -static object * +static PyObject * class_repr(op) - classobject *op; + PyClassObject *op; { char buf[140]; char *name; - if (op->cl_name == NULL || !is_stringobject(op->cl_name)) + if (op->cl_name == NULL || !PyString_Check(op->cl_name)) name = "?"; else - name = getstringvalue(op->cl_name); + name = PyString_AsString(op->cl_name); sprintf(buf, "<class %.100s at %lx>", name, (long)op); - return newstringobject(buf); + return PyString_FromString(buf); } -typeobject Classtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyClass_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "class", - sizeof(classobject), + sizeof(PyClassObject), 0, (destructor)class_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -261,20 +264,20 @@ typeobject Classtype = { }; int -issubclass(class, base) - object *class; - object *base; +PyClass_IsSubclass(class, base) + PyObject *class; + PyObject *base; { int i, n; - classobject *cp; + PyClassObject *cp; if (class == base) return 1; - if (class == NULL || !is_classobject(class)) + if (class == NULL || !PyClass_Check(class)) return 0; - cp = (classobject *)class; - n = gettuplesize(cp->cl_bases); + cp = (PyClassObject *)class; + n = PyTuple_Size(cp->cl_bases); for (i = 0; i < n; i++) { - if (issubclass(gettupleitem(cp->cl_bases, i), base)) + if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) return 1; } return 0; @@ -286,34 +289,35 @@ issubclass(class, base) #ifdef SUPPORT_OBSOLETE_ACCESS static int addaccess(class, inst) - classobject *class; - instanceobject *inst; + PyClassObject *class; + PyInstanceObject *inst; { int i, n, pos, ret; - object *key, *value, *ac; + PyObject *key, *value, *ac; - n = gettuplesize(class->cl_bases); + n = PyTuple_Size(class->cl_bases); for (i = 0; i < n; i++) { - if (addaccess((classobject *)gettupleitem(class->cl_bases, i), inst) < 0) + if (addaccess((PyClassObject *)PyTuple_GetItem( + class->cl_bases, i), inst) < 0) return -1; } pos = 0; - while (mappinggetnext(class->cl_dict, &pos, &key, &value)) { - if (!is_accessobject(value)) + while (PyDict_Next(class->cl_dict, &pos, &key, &value)) { + if (!PyAccess_Check(value)) continue; - if (hasaccessvalue(value)) + if (PyAccess_HasValue(value)) continue; - ac = dict2lookup(inst->in_dict, key); - if (ac != NULL && is_accessobject(ac)) { - err_setval(ConflictError, key); + ac = PyDict_GetItem(inst->in_dict, key); + if (ac != NULL && PyAccess_Check(ac)) { + PyErr_SetObject(PyExc_ConflictError, key); return -1; } - ac = cloneaccessobject(value); + ac = PyAccess_Clone(value); if (ac == NULL) return -1; - ret = dict2insert(inst->in_dict, key, ac); - DECREF(ac); + ret = PyDict_SetItem(inst->in_dict, key, ac); + Py_DECREF(ac); if (ret != 0) return -1; } @@ -321,125 +325,125 @@ addaccess(class, inst) } #endif -object * -newinstanceobject(class, arg, kw) - object *class; - object *arg; - object *kw; +PyObject * +PyInstance_New(class, arg, kw) + PyObject *class; + PyObject *arg; + PyObject *kw; { - register instanceobject *inst; - object *init; - static object *initstr; - if (!is_classobject(class)) { - err_badcall(); + register PyInstanceObject *inst; + PyObject *init; + static PyObject *initstr; + if (!PyClass_Check(class)) { + PyErr_BadInternalCall(); return NULL; } - inst = NEWOBJ(instanceobject, &Instancetype); + inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); if (inst == NULL) return NULL; - INCREF(class); - inst->in_class = (classobject *)class; - inst->in_dict = newdictobject(); + Py_INCREF(class); + inst->in_class = (PyClassObject *)class; + inst->in_dict = PyDict_New(); if (inst->in_dict == NULL #ifdef SUPPORT_OBSOLETE_ACCESS - || addaccess((classobject *)class, inst) != 0 + || addaccess((PyClassObject *)class, inst) != 0 #endif ) { - DECREF(inst); + Py_DECREF(inst); return NULL; } if (initstr == NULL) initstr = PyString_InternFromString("__init__"); init = instance_getattr1(inst, initstr); if (init == NULL) { - err_clear(); - if ((arg != NULL && (!is_tupleobject(arg) || - gettuplesize(arg) != 0)) - || (kw != NULL && (!is_dictobject(kw) || - getdictsize(kw) != 0))) { - err_setstr(TypeError, + PyErr_Clear(); + if ((arg != NULL && (!PyTuple_Check(arg) || + PyTuple_Size(arg) != 0)) + || (kw != NULL && (!PyDict_Check(kw) || + PyDict_Size(kw) != 0))) { + PyErr_SetString(PyExc_TypeError, "this constructor takes no arguments"); - DECREF(inst); + Py_DECREF(inst); inst = NULL; } } else { - object *res = PyEval_CallObjectWithKeywords(init, arg, kw); - DECREF(init); + PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); + Py_DECREF(init); if (res == NULL) { - DECREF(inst); + Py_DECREF(inst); inst = NULL; } else { - if (res != None) { - err_setstr(TypeError, + if (res != Py_None) { + PyErr_SetString(PyExc_TypeError, "__init__() should return None"); - DECREF(inst); + Py_DECREF(inst); inst = NULL; } - DECREF(res); + Py_DECREF(res); } } - return (object *)inst; + return (PyObject *)inst; } /* Instance methods */ static void instance_dealloc(inst) - register instanceobject *inst; + register PyInstanceObject *inst; { - object *error_type, *error_value, *error_traceback; - object *del; - static object *delstr; + PyObject *error_type, *error_value, *error_traceback; + PyObject *del; + static PyObject *delstr; /* Call the __del__ method if it exists. First temporarily revive the object and save the current exception, if any. */ #ifdef Py_TRACE_REFS /* much too complicated if Py_TRACE_REFS defined */ - extern long ref_total; - inst->ob_type = &Instancetype; - NEWREF(inst); - ref_total--; /* compensate for increment in NEWREF */ + extern long _Py_RefTotal; + inst->ob_type = &PyInstance_Type; + _Py_NewReference(inst); + _Py_RefTotal--; /* compensate for increment in NEWREF */ #ifdef COUNT_ALLOCS inst->ob_type->tp_alloc--; /* ditto */ #endif #else /* !Py_TRACE_REFS */ - INCREF(inst); + Py_INCREF(inst); #endif /* !Py_TRACE_REFS */ - err_fetch(&error_type, &error_value, &error_traceback); + PyErr_Fetch(&error_type, &error_value, &error_traceback); if (delstr == NULL) delstr = PyString_InternFromString("__del__"); if ((del = instance_getattr1(inst, delstr)) != NULL) { - object *res = call_object(del, (object *)NULL); + PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); if (res == NULL) { - object *f, *t, *v, *tb; - err_fetch(&t, &v, &tb); - f = sysget("stderr"); - err_clear(); + PyObject *f, *t, *v, *tb; + PyErr_Fetch(&t, &v, &tb); + f = PySys_GetObject("stderr"); + PyErr_Clear(); if (f != NULL) { - writestring("Exception ", f); + PyFile_WriteString("Exception ", f); if (t) { - writeobject(t, f, Py_PRINT_RAW); - if (v && v != None) { - writestring(": ", f); - writeobject(v, f, 0); + PyFile_WriteObject(t, f, Py_PRINT_RAW); + if (v && v != Py_None) { + PyFile_WriteString(": ", f); + PyFile_WriteObject(v, f, 0); } } - writestring(" in ", f); - writeobject(del, f, 0); - writestring(" ignored\n", f); - err_clear(); /* Just in case */ + PyFile_WriteString(" in ", f); + PyFile_WriteObject(del, f, 0); + PyFile_WriteString(" ignored\n", f); + PyErr_Clear(); /* Just in case */ } Py_XDECREF(t); Py_XDECREF(v); Py_XDECREF(tb); } else - DECREF(res); - DECREF(del); + Py_DECREF(res); + Py_DECREF(del); } /* Restore the saved exception and undo the temporary revival */ - err_restore(error_type, error_value, error_traceback); + PyErr_Restore(error_type, error_value, error_traceback); /* Can't use DECREF here, it would cause a recursive call */ if (--inst->ob_refcnt > 0) { #ifdef COUNT_ALLOCS @@ -451,70 +455,70 @@ instance_dealloc(inst) #ifdef COUNT_ALLOCS inst->ob_type->tp_free--; /* compensate for increment in UNREF */ #endif - UNREF(inst); + _Py_ForgetReference(inst); inst->ob_type = NULL; #endif /* Py_TRACE_REFS */ - DECREF(inst->in_class); - XDECREF(inst->in_dict); + Py_DECREF(inst->in_class); + Py_XDECREF(inst->in_dict); free((ANY *)inst); } -static object * +static PyObject * instance_getattr1(inst, name) - register instanceobject *inst; - object *name; + register PyInstanceObject *inst; + PyObject *name; { - register object *v; - register char *sname = getstringvalue(name); - classobject *class; + register PyObject *v; + register char *sname = PyString_AsString(name); + PyClassObject *class; if (sname[0] == '_' && sname[1] == '_') { if (strcmp(sname, "__dict__") == 0) { - if (getrestricted()) { - err_setstr(RuntimeError, - "instance.__dict__ not accessible in restricted mode"); + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "instance.__dict__ not accessible in restricted mode"); return NULL; } - INCREF(inst->in_dict); + Py_INCREF(inst->in_dict); return inst->in_dict; } if (strcmp(sname, "__class__") == 0) { - INCREF(inst->in_class); - return (object *)inst->in_class; + Py_INCREF(inst->in_class); + return (PyObject *)inst->in_class; } } class = NULL; - v = mappinglookup(inst->in_dict, name); + v = PyDict_GetItem(inst->in_dict, name); if (v == NULL) { v = class_lookup(inst->in_class, name, &class); if (v == NULL) { - err_setval(AttributeError, name); + PyErr_SetObject(PyExc_AttributeError, name); return NULL; } } #ifdef SUPPORT_OBSOLETE_ACCESS - if (is_accessobject(v)) { - v = getaccessvalue(v, getowner()); + if (PyAccess_Check(v)) { + v = PyAccess_AsValue(v, PyEval_GetOwner()); if (v == NULL) return NULL; } else #endif - INCREF(v); + Py_INCREF(v); if (class != NULL) { - if (is_funcobject(v)) { - object *w = newinstancemethodobject(v, (object *)inst, - (object *)class); - DECREF(v); + if (PyFunction_Check(v)) { + PyObject *w = PyMethod_New(v, (PyObject *)inst, + (PyObject *)class); + Py_DECREF(v); v = w; } - else if (is_instancemethodobject(v)) { - object *im_class = instancemethodgetclass(v); + else if (PyMethod_Check(v)) { + PyObject *im_class = PyMethod_Class(v); /* Only if classes are compatible */ - if (issubclass((object *)class, im_class)) { - object *im_func = instancemethodgetfunc(v); - object *w = newinstancemethodobject(im_func, - (object *)inst, im_class); - DECREF(v); + if (PyClass_IsSubclass((PyObject *)class, im_class)) { + PyObject *im_func = PyMethod_Function(v); + PyObject *w = PyMethod_New(im_func, + (PyObject *)inst, im_class); + Py_DECREF(v); v = w; } } @@ -522,62 +526,63 @@ instance_getattr1(inst, name) return v; } -static object * +static PyObject * instance_getattr(inst, name) - register instanceobject *inst; - object *name; + register PyInstanceObject *inst; + PyObject *name; { - register object *func, *res; + register PyObject *func, *res; res = instance_getattr1(inst, name); if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { - object *args; - err_clear(); - args = mkvalue("(OO)", inst, name); + PyObject *args; + PyErr_Clear(); + args = Py_BuildValue("(OO)", inst, name); if (args == NULL) return NULL; - res = call_object(func, args); - DECREF(args); + res = PyEval_CallObject(func, args); + Py_DECREF(args); } return res; } static int instance_setattr1(inst, name, v) - instanceobject *inst; - object *name; - object *v; + PyInstanceObject *inst; + PyObject *name; + PyObject *v; { #ifdef SUPPORT_OBSOLETE_ACCESS - object *ac; - ac = mappinglookup(inst->in_dict, name); - if (ac != NULL && is_accessobject(ac)) - return setaccessvalue(ac, getowner(), v); + PyObject *ac; + ac = PyDict_GetItem(inst->in_dict, name); + if (ac != NULL && PyAccess_Check(ac)) + return PyAccess_SetValue(ac, PyEval_GetOwner(), v); #endif if (v == NULL) { - int rv = mappingremove(inst->in_dict, name); + int rv = PyDict_DelItem(inst->in_dict, name); if (rv < 0) - err_setstr(AttributeError, + PyErr_SetString(PyExc_AttributeError, "delete non-existing instance attribute"); return rv; } else - return mappinginsert(inst->in_dict, name, v); + return PyDict_SetItem(inst->in_dict, name, v); } static int instance_setattr(inst, name, v) - instanceobject *inst; - object *name; - object *v; + PyInstanceObject *inst; + PyObject *name; + PyObject *v; { - object *func, *args, *res; - char *sname = getstringvalue(name); + PyObject *func, *args, *res; + char *sname = PyString_AsString(name); if (sname[0] == '_' && sname[1] == '_' && (strcmp(sname, "__dict__") == 0 || strcmp(sname, "__class__") == 0)) { - int n = getstringsize(name); + int n = PyString_Size(name); if (sname[n-1] == '_' && sname[n-2] == '_') { - err_setstr(TypeError, "read-only special attribute"); + PyErr_SetString(PyExc_TypeError, + "read-only special attribute"); return -1; } } @@ -588,68 +593,68 @@ instance_setattr(inst, name, v) if (func == NULL) return instance_setattr1(inst, name, v); if (v == NULL) - args = mkvalue("(OO)", inst, name); + args = Py_BuildValue("(OO)", inst, name); else - args = mkvalue("(OOO)", inst, name, v); + args = Py_BuildValue("(OOO)", inst, name, v); if (args == NULL) return -1; - res = call_object(func, args); - DECREF(args); + res = PyEval_CallObject(func, args); + Py_DECREF(args); if (res == NULL) return -1; - DECREF(res); + Py_DECREF(res); return 0; } -static object * +static PyObject * instance_repr(inst) - instanceobject *inst; + PyInstanceObject *inst; { - object *func; - object *res; - static object *reprstr; + PyObject *func; + PyObject *res; + static PyObject *reprstr; if (reprstr == NULL) reprstr = PyString_InternFromString("__repr__"); func = instance_getattr(inst, reprstr); if (func == NULL) { char buf[140]; - object *classname = inst->in_class->cl_name; + PyObject *classname = inst->in_class->cl_name; char *cname; - if (classname != NULL && is_stringobject(classname)) - cname = getstringvalue(classname); + if (classname != NULL && PyString_Check(classname)) + cname = PyString_AsString(classname); else cname = "?"; - err_clear(); + PyErr_Clear(); sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst); - return newstringobject(buf); + return PyString_FromString(buf); } - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); return res; } -static object * +static PyObject * instance_compare1(inst, other) - object *inst, *other; + PyObject *inst, *other; { - return instancebinop(inst, other, "__cmp__", "__rcmp__", + return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__", instance_compare1); } static int instance_compare(inst, other) - object *inst, *other; + PyObject *inst, *other; { - object *result; + PyObject *result; long outcome; result = instance_compare1(inst, other); - if (result == NULL || !is_intobject(result)) { - err_clear(); + if (result == NULL || !PyInt_Check(result)) { + PyErr_Clear(); return (inst < other) ? -1 : 1; } - outcome = getintvalue(result); - DECREF(result); + outcome = PyInt_AsLong(result); + Py_DECREF(result); if (outcome < 0) return -1; else if (outcome > 0) @@ -659,12 +664,12 @@ instance_compare(inst, other) static long instance_hash(inst) - instanceobject *inst; + PyInstanceObject *inst; { - object *func; - object *res; + PyObject *func; + PyObject *res; long outcome; - static object *hashstr, *cmpstr; + static PyObject *hashstr, *cmpstr; if (hashstr == NULL) hashstr = PyString_InternFromString("__hash__"); @@ -672,45 +677,46 @@ instance_hash(inst) if (func == NULL) { /* If there is no __cmp__ method, we hash on the address. If a __cmp__ method exists, there must be a __hash__. */ - err_clear(); + PyErr_Clear(); if (cmpstr == NULL) cmpstr = PyString_InternFromString("__cmp__"); func = instance_getattr(inst, cmpstr); if (func == NULL) { - err_clear(); + PyErr_Clear(); outcome = (long)inst; if (outcome == -1) outcome = -2; return outcome; } - err_setstr(TypeError, "unhashable instance"); + PyErr_SetString(PyExc_TypeError, "unhashable instance"); return -1; } - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); if (res == NULL) return -1; - if (is_intobject(res)) { - outcome = getintvalue(res); + if (PyInt_Check(res)) { + outcome = PyInt_AsLong(res); if (outcome == -1) outcome = -2; } else { - err_setstr(TypeError, "__hash__() should return an int"); + PyErr_SetString(PyExc_TypeError, + "__hash__() should return an int"); outcome = -1; } - DECREF(res); + Py_DECREF(res); return outcome; } -static object *getitemstr, *setitemstr, *delitemstr, *lenstr; +static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; static int instance_length(inst) - instanceobject *inst; + PyInstanceObject *inst; { - object *func; - object *res; + PyObject *func; + PyObject *res; int outcome; if (lenstr == NULL) @@ -718,57 +724,59 @@ instance_length(inst) func = instance_getattr(inst, lenstr); if (func == NULL) return -1; - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); if (res == NULL) return -1; - if (is_intobject(res)) { - outcome = getintvalue(res); + if (PyInt_Check(res)) { + outcome = PyInt_AsLong(res); if (outcome < 0) - err_setstr(ValueError, "__len__() should return >= 0"); + PyErr_SetString(PyExc_ValueError, + "__len__() should return >= 0"); } else { - err_setstr(TypeError, "__len__() should return an int"); + PyErr_SetString(PyExc_TypeError, + "__len__() should return an int"); outcome = -1; } - DECREF(res); + Py_DECREF(res); return outcome; } -static object * +static PyObject * instance_subscript(inst, key) - instanceobject *inst; - object *key; + PyInstanceObject *inst; + PyObject *key; { - object *func; - object *arg; - object *res; + PyObject *func; + PyObject *arg; + PyObject *res; if (getitemstr == NULL) getitemstr = PyString_InternFromString("__getitem__"); func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; - arg = mkvalue("(O)", key); + arg = Py_BuildValue("(O)", key); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return NULL; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); return res; } static int instance_ass_subscript(inst, key, value) - instanceobject*inst; - object *key; - object *value; + PyInstanceObject*inst; + PyObject *key; + PyObject *value; { - object *func; - object *arg; - object *res; + PyObject *func; + PyObject *arg; + PyObject *res; if (value == NULL) { if (delitemstr == NULL) @@ -783,82 +791,82 @@ instance_ass_subscript(inst, key, value) if (func == NULL) return -1; if (value == NULL) - arg = mkvalue("(O)", key); + arg = Py_BuildValue("(O)", key); else - arg = mkvalue("(OO)", key, value); + arg = Py_BuildValue("(OO)", key, value); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return -1; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); if (res == NULL) return -1; - DECREF(res); + Py_DECREF(res); return 0; } -static mapping_methods instance_as_mapping = { +static PyMappingMethods instance_as_mapping = { (inquiry)instance_length, /*mp_length*/ (binaryfunc)instance_subscript, /*mp_subscript*/ (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/ }; -static object * +static PyObject * instance_item(inst, i) - instanceobject *inst; + PyInstanceObject *inst; int i; { - object *func, *arg, *res; + PyObject *func, *arg, *res; if (getitemstr == NULL) getitemstr = PyString_InternFromString("__getitem__"); func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; - arg = mkvalue("(i)", i); + arg = Py_BuildValue("(i)", i); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return NULL; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); return res; } -static object * +static PyObject * instance_slice(inst, i, j) - instanceobject *inst; + PyInstanceObject *inst; int i, j; { - object *func, *arg, *res; - static object *getslicestr; + PyObject *func, *arg, *res; + static PyObject *getslicestr; if (getslicestr == NULL) getslicestr = PyString_InternFromString("__getslice__"); func = instance_getattr(inst, getslicestr); if (func == NULL) return NULL; - arg = mkvalue("(ii)", i, j); + arg = Py_BuildValue("(ii)", i, j); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return NULL; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); return res; } static int instance_ass_item(inst, i, item) - instanceobject *inst; + PyInstanceObject *inst; int i; - object *item; + PyObject *item; { - object *func, *arg, *res; + PyObject *func, *arg, *res; if (item == NULL) { if (delitemstr == NULL) @@ -873,61 +881,63 @@ instance_ass_item(inst, i, item) if (func == NULL) return -1; if (item == NULL) - arg = mkvalue("i", i); + arg = Py_BuildValue("i", i); else - arg = mkvalue("(iO)", i, item); + arg = Py_BuildValue("(iO)", i, item); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return -1; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); if (res == NULL) return -1; - DECREF(res); + Py_DECREF(res); return 0; } static int instance_ass_slice(inst, i, j, value) - instanceobject *inst; + PyInstanceObject *inst; int i, j; - object *value; + PyObject *value; { - object *func, *arg, *res; - static object *setslicestr, *delslicestr; + PyObject *func, *arg, *res; + static PyObject *setslicestr, *delslicestr; if (value == NULL) { if (delslicestr == NULL) - delslicestr = PyString_InternFromString("__delslice__"); + delslicestr = + PyString_InternFromString("__delslice__"); func = instance_getattr(inst, delslicestr); } else { if (setslicestr == NULL) - setslicestr = PyString_InternFromString("__setslice__"); + setslicestr = + PyString_InternFromString("__setslice__"); func = instance_getattr(inst, setslicestr); } if (func == NULL) return -1; if (value == NULL) - arg = mkvalue("(ii)", i, j); + arg = Py_BuildValue("(ii)", i, j); else - arg = mkvalue("(iiO)", i, j, value); + arg = Py_BuildValue("(iiO)", i, j, value); if (arg == NULL) { - DECREF(func); + Py_DECREF(func); return -1; } - res = call_object(func, arg); - DECREF(func); - DECREF(arg); + res = PyEval_CallObject(func, arg); + Py_DECREF(func); + Py_DECREF(arg); if (res == NULL) return -1; - DECREF(res); + Py_DECREF(res); return 0; } -static sequence_methods instance_as_sequence = { +static PySequenceMethods instance_as_sequence = { (inquiry)instance_length, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ @@ -937,44 +947,44 @@ static sequence_methods instance_as_sequence = { (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/ }; -static object * +static PyObject * generic_unary_op(self, methodname) - instanceobject *self; - object *methodname; + PyInstanceObject *self; + PyObject *methodname; { - object *func, *res; + PyObject *func, *res; if ((func = instance_getattr(self, methodname)) == NULL) return NULL; - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); return res; } /* Forward */ -static int halfbinop Py_PROTO((object *, object *, char *, object **, - object * (*) Py_PROTO((object *, object *)), int )); +static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **, + PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int )); /* Implement a binary operator involving at least one class instance. */ -object * -instancebinop(v, w, opname, ropname, thisfunc) - object *v; - object *w; +PyObject * +PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) + PyObject *v; + PyObject *w; char *opname; char *ropname; - object * (*thisfunc) PROTO((object *, object *)); + PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); { char buf[256]; - object *result = NULL; + PyObject *result = NULL; if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0) return result; if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0) return result; sprintf(buf, "%s nor %s defined for these operands", opname, ropname); - err_setstr(TypeError, buf); + PyErr_SetString(PyExc_TypeError, buf); return NULL; } @@ -986,152 +996,152 @@ instancebinop(v, w, opname, ropname, thisfunc) 1 if we could try another operation */ -static object *coerce_obj; +static PyObject *coerce_obj; static int halfbinop(v, w, opname, r_result, thisfunc, swapped) - object *v; - object *w; + PyObject *v; + PyObject *w; char *opname; - object **r_result; - object * (*thisfunc) PROTO((object *, object *)); + PyObject **r_result; + PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); int swapped; { - object *func; - object *args; - object *coerce; - object *coerced = NULL; - object *v1; + PyObject *func; + PyObject *args; + PyObject *PyNumber_Coerce; + PyObject *coerced = NULL; + PyObject *v1; - if (!is_instanceobject(v)) + if (!PyInstance_Check(v)) return 1; if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return -1; } - coerce = getattro(v, coerce_obj); - if (coerce == NULL) { - err_clear(); + PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj); + if (PyNumber_Coerce == NULL) { + PyErr_Clear(); } else { - args = mkvalue("(O)", w); + args = Py_BuildValue("(O)", w); if (args == NULL) { return -1; } - coerced = call_object(coerce, args); - DECREF(args); - DECREF(coerce); + coerced = PyEval_CallObject(PyNumber_Coerce, args); + Py_DECREF(args); + Py_DECREF(PyNumber_Coerce); if (coerced == NULL) { return -1; } - if (coerced == None) { - DECREF(coerced); + if (coerced == Py_None) { + Py_DECREF(coerced); return 1; } - if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) { - DECREF(coerced); - err_setstr(TypeError, + if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { + Py_DECREF(coerced); + PyErr_SetString(PyExc_TypeError, "coercion should return None or 2-tuple"); return -1; } - v1 = gettupleitem(coerced, 0); - w = gettupleitem(coerced, 1); + v1 = PyTuple_GetItem(coerced, 0); + w = PyTuple_GetItem(coerced, 1); if (v1 != v) { v = v1; - if (!is_instanceobject(v) && !is_instanceobject(w)) { + if (!PyInstance_Check(v) && !PyInstance_Check(w)) { if (swapped) *r_result = (*thisfunc)(w, v); else *r_result = (*thisfunc)(v, w); - DECREF(coerced); + Py_DECREF(coerced); return *r_result == NULL ? -1 : 0; } } - w = gettupleitem(coerced, 1); + w = PyTuple_GetItem(coerced, 1); } - func = getattr(v, opname); + func = PyObject_GetAttrString(v, opname); if (func == NULL) { - XDECREF(coerced); - if (err_occurred() != AttributeError) + Py_XDECREF(coerced); + if (PyErr_Occurred() != PyExc_AttributeError) return -1; - err_clear(); + PyErr_Clear(); return 1; } - args = mkvalue("(O)", w); + args = Py_BuildValue("(O)", w); if (args == NULL) { - DECREF(func); - XDECREF(coerced); + Py_DECREF(func); + Py_XDECREF(coerced); return -1; } - *r_result = call_object(func, args); - DECREF(args); - DECREF(func); - XDECREF(coerced); + *r_result = PyEval_CallObject(func, args); + Py_DECREF(args); + Py_DECREF(func); + Py_XDECREF(coerced); return *r_result == NULL ? -1 : 0; } static int instance_coerce(pv, pw) - object **pv; - object **pw; + PyObject **pv; + PyObject **pw; { - object *v = *pv; - object *w = *pw; - object *coerce; - object *args; - object *coerced; + PyObject *v = *pv; + PyObject *w = *pw; + PyObject *PyNumber_Coerce; + PyObject *args; + PyObject *coerced; if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return -1; } - coerce = getattro(v, coerce_obj); - if (coerce == NULL) { + PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj); + if (PyNumber_Coerce == NULL) { /* No __coerce__ method: always OK */ - err_clear(); - INCREF(v); - INCREF(w); + PyErr_Clear(); + Py_INCREF(v); + Py_INCREF(w); return 0; } /* Has __coerce__ method: call it */ - args = mkvalue("(O)", w); + args = Py_BuildValue("(O)", w); if (args == NULL) { return -1; } - coerced = call_object(coerce, args); - DECREF(args); - DECREF(coerce); + coerced = PyEval_CallObject(PyNumber_Coerce, args); + Py_DECREF(args); + Py_DECREF(PyNumber_Coerce); if (coerced == NULL) { /* __coerce__ call raised an exception */ return -1; } - if (coerced == None) { + if (coerced == Py_None) { /* __coerce__ says "I can't do it" */ - DECREF(coerced); + Py_DECREF(coerced); return 1; } - if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) { + if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { /* __coerce__ return value is malformed */ - DECREF(coerced); - err_setstr(TypeError, + Py_DECREF(coerced); + PyErr_SetString(PyExc_TypeError, "coercion should return None or 2-tuple"); return -1; } /* __coerce__ returned two new values */ - *pv = gettupleitem(coerced, 0); - *pw = gettupleitem(coerced, 1); - INCREF(*pv); - INCREF(*pw); - DECREF(coerced); + *pv = PyTuple_GetItem(coerced, 0); + *pw = PyTuple_GetItem(coerced, 1); + Py_INCREF(*pv); + Py_INCREF(*pw); + Py_DECREF(coerced); return 0; } #define UNARY(funcname, methodname) \ -static object *funcname(self) instanceobject *self; { \ - static object *o; \ +static PyObject *funcname(self) PyInstanceObject *self; { \ + static PyObject *o; \ if (o == NULL) o = PyString_InternFromString(methodname); \ return generic_unary_op(self, o); \ } @@ -1142,38 +1152,40 @@ UNARY(instance_abs, "__abs__") static int instance_nonzero(self) - instanceobject *self; + PyInstanceObject *self; { - object *func, *res; + PyObject *func, *res; long outcome; - static object *nonzerostr; + static PyObject *nonzerostr; if (nonzerostr == NULL) nonzerostr = PyString_InternFromString("__nonzero__"); if ((func = instance_getattr(self, nonzerostr)) == NULL) { - err_clear(); + PyErr_Clear(); if (lenstr == NULL) lenstr = PyString_InternFromString("__len__"); if ((func = instance_getattr(self, lenstr)) == NULL) { - err_clear(); + PyErr_Clear(); /* Fall back to the default behavior: all instances are nonzero */ return 1; } } - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); if (res == NULL) return -1; - if (!is_intobject(res)) { - DECREF(res); - err_setstr(TypeError, "__nonzero__ should return an int"); + if (!PyInt_Check(res)) { + Py_DECREF(res); + PyErr_SetString(PyExc_TypeError, + "__nonzero__ should return an int"); return -1; } - outcome = getintvalue(res); - DECREF(res); + outcome = PyInt_AsLong(res); + Py_DECREF(res); if (outcome < 0) { - err_setstr(ValueError, "__nonzero__ should return >= 0"); + PyErr_SetString(PyExc_ValueError, + "__nonzero__ should return >= 0"); return -1; } return outcome > 0; @@ -1187,35 +1199,35 @@ UNARY(instance_oct, "__oct__") UNARY(instance_hex, "__hex__") /* This version is for ternary calls only (z != None) */ -static object * +static PyObject * instance_pow(v, w, z) - object *v; - object *w; - object *z; + PyObject *v; + PyObject *w; + PyObject *z; { /* XXX Doesn't do coercions... */ - object *func; - object *args; - object *result; - static object *powstr; + PyObject *func; + PyObject *args; + PyObject *result; + static PyObject *powstr; if (powstr == NULL) powstr = PyString_InternFromString("__pow__"); - func = getattro(v, powstr); + func = PyObject_GetAttr(v, powstr); if (func == NULL) return NULL; - args = mkvalue("(OO)", w, z); + args = Py_BuildValue("(OO)", w, z); if (args == NULL) { - DECREF(func); + Py_DECREF(func); return NULL; } - result = call_object(func, args); - DECREF(func); - DECREF(args); + result = PyEval_CallObject(func, args); + Py_DECREF(func); + Py_DECREF(args); return result; } -static number_methods instance_as_number = { +static PyNumberMethods instance_as_number = { 0, /*nb_add*/ 0, /*nb_subtract*/ 0, /*nb_multiply*/ @@ -1241,11 +1253,11 @@ static number_methods instance_as_number = { (unaryfunc)instance_hex, /*nb_hex*/ }; -typeobject Instancetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyInstance_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "instance", - sizeof(instanceobject), + sizeof(PyInstanceObject), 0, (destructor)instance_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -1271,71 +1283,71 @@ typeobject Instancetype = { */ typedef struct { - OB_HEAD - object *im_func; /* The function implementing the method */ - object *im_self; /* The instance it is bound to, or NULL */ - object *im_class; /* The class that defined the method */ -} instancemethodobject; - -object * -newinstancemethodobject(func, self, class) - object *func; - object *self; - object *class; + PyObject_HEAD + PyObject *im_func; /* The function implementing the method */ + PyObject *im_self; /* The instance it is bound to, or NULL */ + PyObject *im_class; /* The class that defined the method */ +} PyMethodObject; + +PyObject * +PyMethod_New(func, self, class) + PyObject *func; + PyObject *self; + PyObject *class; { - register instancemethodobject *im; - if (!is_funcobject(func)) { - err_badcall(); + register PyMethodObject *im; + if (!PyFunction_Check(func)) { + PyErr_BadInternalCall(); return NULL; } - im = NEWOBJ(instancemethodobject, &Instancemethodtype); + im = PyObject_NEW(PyMethodObject, &PyMethod_Type); if (im == NULL) return NULL; - INCREF(func); + Py_INCREF(func); im->im_func = func; - XINCREF(self); + Py_XINCREF(self); im->im_self = self; - INCREF(class); + Py_INCREF(class); im->im_class = class; - return (object *)im; + return (PyObject *)im; } -object * -instancemethodgetfunc(im) - register object *im; +PyObject * +PyMethod_Function(im) + register PyObject *im; { - if (!is_instancemethodobject(im)) { - err_badcall(); + if (!PyMethod_Check(im)) { + PyErr_BadInternalCall(); return NULL; } - return ((instancemethodobject *)im)->im_func; + return ((PyMethodObject *)im)->im_func; } -object * -instancemethodgetself(im) - register object *im; +PyObject * +PyMethod_Self(im) + register PyObject *im; { - if (!is_instancemethodobject(im)) { - err_badcall(); + if (!PyMethod_Check(im)) { + PyErr_BadInternalCall(); return NULL; } - return ((instancemethodobject *)im)->im_self; + return ((PyMethodObject *)im)->im_self; } -object * -instancemethodgetclass(im) - register object *im; +PyObject * +PyMethod_Class(im) + register PyObject *im; { - if (!is_instancemethodobject(im)) { - err_badcall(); + if (!PyMethod_Check(im)) { + PyErr_BadInternalCall(); return NULL; } - return ((instancemethodobject *)im)->im_class; + return ((PyMethodObject *)im)->im_class; } /* Class method methods */ -#define OFF(x) offsetof(instancemethodobject, x) +#define OFF(x) offsetof(PyMethodObject, x) static struct memberlist instancemethod_memberlist[] = { {"im_func", T_OBJECT, OFF(im_func)}, @@ -1347,106 +1359,106 @@ static struct memberlist instancemethod_memberlist[] = { {NULL} /* Sentinel */ }; -static object * +static PyObject * instancemethod_getattr(im, name) - register instancemethodobject *im; - object *name; + register PyMethodObject *im; + PyObject *name; { - char *sname = getstringvalue(name); + char *sname = PyString_AsString(name); if (sname[0] == '_') { - funcobject *func = (funcobject *)(im->im_func); + PyFunctionObject *func = (PyFunctionObject *)(im->im_func); if (strcmp(sname, "__name__") == 0) { - INCREF(func->func_name); + Py_INCREF(func->func_name); return func->func_name; } if (strcmp(sname, "__doc__") == 0) { - INCREF(func->func_doc); + Py_INCREF(func->func_doc); return func->func_doc; } } - if (getrestricted()) { - err_setstr(RuntimeError, - "instance-method attributes not accessible in restricted mode"); + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "instance-method attributes not accessible in restricted mode"); return NULL; } - return getmember((char *)im, instancemethod_memberlist, sname); + return PyMember_Get((char *)im, instancemethod_memberlist, sname); } static void instancemethod_dealloc(im) - register instancemethodobject *im; + register PyMethodObject *im; { - DECREF(im->im_func); - XDECREF(im->im_self); - DECREF(im->im_class); + Py_DECREF(im->im_func); + Py_XDECREF(im->im_self); + Py_DECREF(im->im_class); free((ANY *)im); } static int instancemethod_compare(a, b) - instancemethodobject *a, *b; + PyMethodObject *a, *b; { if (a->im_self != b->im_self) return (a->im_self < b->im_self) ? -1 : 1; - return cmpobject(a->im_func, b->im_func); + return PyObject_Compare(a->im_func, b->im_func); } -static object * +static PyObject * instancemethod_repr(a) - instancemethodobject *a; + PyMethodObject *a; { char buf[240]; - instanceobject *self = (instanceobject *)(a->im_self); - funcobject *func = (funcobject *)(a->im_func); - classobject *class = (classobject *)(a->im_class); - object *fclassname, *iclassname, *funcname; + PyInstanceObject *self = (PyInstanceObject *)(a->im_self); + PyFunctionObject *func = (PyFunctionObject *)(a->im_func); + PyClassObject *class = (PyClassObject *)(a->im_class); + PyObject *fclassname, *iclassname, *funcname; char *fcname, *icname, *fname; fclassname = class->cl_name; funcname = func->func_name; - if (fclassname != NULL && is_stringobject(fclassname)) - fcname = getstringvalue(fclassname); + if (fclassname != NULL && PyString_Check(fclassname)) + fcname = PyString_AsString(fclassname); else fcname = "?"; - if (funcname != NULL && is_stringobject(funcname)) - fname = getstringvalue(funcname); + if (funcname != NULL && PyString_Check(funcname)) + fname = PyString_AsString(funcname); else fname = "?"; if (self == NULL) sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname); else { iclassname = self->in_class->cl_name; - if (iclassname != NULL && is_stringobject(iclassname)) - icname = getstringvalue(iclassname); + if (iclassname != NULL && PyString_Check(iclassname)) + icname = PyString_AsString(iclassname); else icname = "?"; sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>", fcname, fname, icname, (long)self); } - return newstringobject(buf); + return PyString_FromString(buf); } static long instancemethod_hash(a) - instancemethodobject *a; + PyMethodObject *a; { long x, y; if (a->im_self == NULL) - x = hashobject(None); + x = PyObject_Hash(Py_None); else - x = hashobject(a->im_self); + x = PyObject_Hash(a->im_self); if (x == -1) return -1; - y = hashobject(a->im_func); + y = PyObject_Hash(a->im_func); if (y == -1) return -1; return x ^ y; } -typeobject Instancemethodtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyMethod_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "instance method", - sizeof(instancemethodobject), + sizeof(PyMethodObject), 0, (destructor)instancemethod_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ diff --git a/Objects/complexobject.c b/Objects/complexobject.c index abd83e4..0ad665d 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -4,10 +4,7 @@ #ifndef WITHOUT_COMPLEX -#include "allobjects.h" -#include "modsupport.h" - -#include <errno.h> +#include "Python.h" #include "mymath.h" #ifdef i860 @@ -48,8 +45,8 @@ #endif #if !defined(__STDC__) && !defined(macintosh) -extern double fmod PROTO((double, double)); -extern double pow PROTO((double, double)); +extern double fmod Py_PROTO((double, double)); +extern double pow Py_PROTO((double, double)); #endif @@ -175,14 +172,14 @@ PyObject * PyComplex_FromCComplex(cval) Py_complex cval; { - register complexobject *op = - (complexobject *) malloc(sizeof(complexobject)); + register PyComplexObject *op = + (PyComplexObject *) malloc(sizeof(PyComplexObject)); if (op == NULL) - return err_nomem(); - op->ob_type = &Complextype; + return PyErr_NoMemory(); + op->ob_type = &PyComplex_Type; op->cval = cval; - NEWREF(op); - return (object *) op; + _Py_NewReference(op); + return (PyObject *) op; } PyObject * @@ -233,16 +230,16 @@ PyComplex_AsCComplex(op) static void complex_dealloc(op) - object *op; + PyObject *op; { - DEL(op); + PyMem_DEL(op); } static void complex_buf_repr(buf, v) char *buf; - complexobject *v; + PyComplexObject *v; { if (v->cval.real == 0.) sprintf(buf, "%.12gj", v->cval.imag); @@ -252,7 +249,7 @@ complex_buf_repr(buf, v) static int complex_print(v, fp, flags) - complexobject *v; + PyComplexObject *v; FILE *fp; int flags; /* Not used but required by interface */ { @@ -262,18 +259,18 @@ complex_print(v, fp, flags) return 0; } -static object * +static PyObject * complex_repr(v) - complexobject *v; + PyComplexObject *v; { char buf[100]; complex_buf_repr(buf, v); - return newstringobject(buf); + return PyString_FromString(buf); } static int complex_compare(v, w) - complexobject *v, *w; + PyComplexObject *v, *w; { /* Note: "greater" and "smaller" have no meaning for complex numbers, but Python requires that they be defined nevertheless. */ @@ -290,7 +287,7 @@ complex_compare(v, w) static long complex_hash(v) - complexobject *v; + PyComplexObject *v; { double intpart, fractpart; int expo; @@ -312,11 +309,11 @@ complex_hash(v) if (fractpart == 0.0 && v->cval.imag == 0.0) { if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { /* Convert to long int and use its hash... */ - object *w = dnewlongobject(v->cval.real); + PyObject *w = PyLong_FromDouble(v->cval.real); if (w == NULL) return -1; - x = hashobject(w); - DECREF(w); + x = PyObject_Hash(w); + Py_DECREF(w); return x; } x = (long)intpart; @@ -359,46 +356,46 @@ complex_hash(v) return x; } -static object * +static PyObject * complex_add(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex result; PyFPE_START_PROTECT("complex_add", return 0) result = c_sum(v->cval,w->cval); PyFPE_END_PROTECT(result) - return newcomplexobject(result); + return PyComplex_FromCComplex(result); } -static object * +static PyObject * complex_sub(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex result; PyFPE_START_PROTECT("complex_sub", return 0) result = c_diff(v->cval,w->cval); PyFPE_END_PROTECT(result) - return newcomplexobject(result); + return PyComplex_FromCComplex(result); } -static object * +static PyObject * complex_mul(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex result; PyFPE_START_PROTECT("complex_mul", return 0) result = c_prod(v->cval,w->cval); PyFPE_END_PROTECT(result) - return newcomplexobject(result); + return PyComplex_FromCComplex(result); } -static object * +static PyObject * complex_div(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex quot; PyFPE_START_PROTECT("complex_div", return 0) @@ -406,74 +403,74 @@ complex_div(v, w) quot = c_quot(v->cval,w->cval); PyFPE_END_PROTECT(quot) if (c_error == 1) { - err_setstr(ZeroDivisionError, "complex division"); + PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); return NULL; } - return newcomplexobject(quot); + return PyComplex_FromCComplex(quot); } -static object * +static PyObject * complex_remainder(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex div, mod; c_error = 0; div = c_quot(v->cval,w->cval); /* The raw divisor value. */ if (c_error == 1) { - err_setstr(ZeroDivisionError, "complex remainder"); + PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); return NULL; } div.real = floor(div.real); /* Use the floor of the real part. */ div.imag = 0.0; mod = c_diff(v->cval, c_prod(w->cval, div)); - return newcomplexobject(mod); + return PyComplex_FromCComplex(mod); } -static object * +static PyObject * complex_divmod(v, w) - complexobject *v; - complexobject *w; + PyComplexObject *v; + PyComplexObject *w; { Py_complex div, mod; PyObject *d, *m, *z; c_error = 0; div = c_quot(v->cval,w->cval); /* The raw divisor value. */ if (c_error == 1) { - err_setstr(ZeroDivisionError, "complex divmod()"); + PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); return NULL; } div.real = floor(div.real); /* Use the floor of the real part. */ div.imag = 0.0; mod = c_diff(v->cval, c_prod(w->cval, div)); - d = newcomplexobject(div); - m = newcomplexobject(mod); - z = mkvalue("(OO)", d, m); + d = PyComplex_FromCComplex(div); + m = PyComplex_FromCComplex(mod); + z = Py_BuildValue("(OO)", d, m); Py_XDECREF(d); Py_XDECREF(m); return z; } -static object * +static PyObject * complex_pow(v, w, z) - complexobject *v; - object *w; - complexobject *z; + PyComplexObject *v; + PyObject *w; + PyComplexObject *z; { Py_complex p; Py_complex exponent; long int_exponent; - if ((object *)z!=None) { - err_setstr(ValueError, "complex modulo"); + if ((PyObject *)z!=Py_None) { + PyErr_SetString(PyExc_ValueError, "complex modulo"); return NULL; } PyFPE_START_PROTECT("complex_pow", return 0) c_error = 0; - exponent = ((complexobject*)w)->cval; + exponent = ((PyComplexObject*)w)->cval; int_exponent = (long)exponent.real; if (exponent.imag == 0. && exponent.real == int_exponent) p = c_powi(v->cval,int_exponent); @@ -482,112 +479,113 @@ complex_pow(v, w, z) PyFPE_END_PROTECT(p) if (c_error == 2) { - err_setstr(ValueError, "0.0 to a negative or complex power"); + PyErr_SetString(PyExc_ValueError, + "0.0 to a negative or complex power"); return NULL; } - return newcomplexobject(p); + return PyComplex_FromCComplex(p); } -static object * +static PyObject * complex_neg(v) - complexobject *v; + PyComplexObject *v; { Py_complex neg; neg.real = -v->cval.real; neg.imag = -v->cval.imag; - return newcomplexobject(neg); + return PyComplex_FromCComplex(neg); } -static object * +static PyObject * complex_pos(v) - complexobject *v; + PyComplexObject *v; { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } -static object * +static PyObject * complex_abs(v) - complexobject *v; + PyComplexObject *v; { double result; PyFPE_START_PROTECT("complex_abs", return 0) result = hypot(v->cval.real,v->cval.imag); PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } static int complex_nonzero(v) - complexobject *v; + PyComplexObject *v; { return v->cval.real != 0.0 && v->cval.imag != 0.0; } static int complex_coerce(pv, pw) - object **pv; - object **pw; + PyObject **pv; + PyObject **pw; { Py_complex cval; cval.imag = 0.; - if (is_intobject(*pw)) { - cval.real = (double)getintvalue(*pw); - *pw = newcomplexobject(cval); - INCREF(*pv); + if (PyInt_Check(*pw)) { + cval.real = (double)PyInt_AsLong(*pw); + *pw = PyComplex_FromCComplex(cval); + Py_INCREF(*pv); return 0; } - else if (is_longobject(*pw)) { - cval.real = dgetlongvalue(*pw); - *pw = newcomplexobject(cval); - INCREF(*pv); + else if (PyLong_Check(*pw)) { + cval.real = PyLong_AsDouble(*pw); + *pw = PyComplex_FromCComplex(cval); + Py_INCREF(*pv); return 0; } - else if (is_floatobject(*pw)) { - cval.real = getfloatvalue(*pw); - *pw = newcomplexobject(cval); - INCREF(*pv); + else if (PyFloat_Check(*pw)) { + cval.real = PyFloat_AsDouble(*pw); + *pw = PyComplex_FromCComplex(cval); + Py_INCREF(*pv); return 0; } return 1; /* Can't do it */ } -static object * +static PyObject * complex_int(v) - object *v; + PyObject *v; { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "can't convert complex to int; use e.g. int(abs(z))"); return NULL; } -static object * +static PyObject * complex_long(v) - object *v; + PyObject *v; { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "can't convert complex to long; use e.g. long(abs(z))"); return NULL; } -static object * +static PyObject * complex_float(v) - object *v; + PyObject *v; { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "can't convert complex to float; use e.g. abs(z)"); return NULL; } -static object * +static PyObject * complex_conjugate(self) - object *self; + PyObject *self; { Py_complex c; - c = ((complexobject *)self)->cval; + c = ((PyComplexObject *)self)->cval; c.imag = -c.imag; - return newcomplexobject(c); + return PyComplex_FromCComplex(c); } static PyMethodDef complex_methods[] = { @@ -596,21 +594,21 @@ static PyMethodDef complex_methods[] = { }; -static object * +static PyObject * complex_getattr(self, name) - complexobject *self; + PyComplexObject *self; char *name; { if (strcmp(name, "real") == 0) - return (object *)newfloatobject(self->cval.real); + return (PyObject *)PyFloat_FromDouble(self->cval.real); else if (strcmp(name, "imag") == 0) - return (object *)newfloatobject(self->cval.imag); + return (PyObject *)PyFloat_FromDouble(self->cval.imag); else if (strcmp(name, "__members__") == 0) - return mkvalue("[ss]", "imag", "real"); - return findmethod(complex_methods, (object *)self, name); + return Py_BuildValue("[ss]", "imag", "real"); + return Py_FindMethod(complex_methods, (PyObject *)self, name); } -static number_methods complex_as_number = { +static PyNumberMethods complex_as_number = { (binaryfunc)complex_add, /*nb_add*/ (binaryfunc)complex_sub, /*nb_subtract*/ (binaryfunc)complex_mul, /*nb_multiply*/ @@ -636,11 +634,11 @@ static number_methods complex_as_number = { 0, /*nb_hex*/ }; -typeobject Complextype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyComplex_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "complex", - sizeof(complexobject), + sizeof(PyComplexObject), 0, (destructor)complex_dealloc, /*tp_dealloc*/ (printfunc)complex_print, /*tp_print*/ diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 39c2fc9..a749f0a 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -37,8 +37,7 @@ PERFORMANCE OF THIS SOFTWARE. originally derived from) a file by that name I had to change its name. For the user these objects are still called "dictionaries". */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" /* @@ -85,7 +84,7 @@ static long polys[] = { }; /* Object used as dummy key to fill deleted entries */ -static object *dummy; /* Initialized by first call to newmappingobject() */ +static PyObject *dummy; /* Initialized by first call to newmappingobject() */ /* Invariant for entries: when in use, de_value is not NULL and de_key is @@ -95,8 +94,8 @@ NULL, since otherwise other keys may be lost. */ typedef struct { long me_hash; - object *me_key; - object *me_value; + PyObject *me_key; + PyObject *me_value; #ifdef USE_CACHE_ALIGNED long aligner; #endif @@ -111,7 +110,7 @@ To avoid slowing down lookups on a near-full table, we resize the table when it is more than half filled. */ typedef struct { - OB_HEAD + PyObject_HEAD int ma_fill; int ma_used; int ma_size; @@ -119,16 +118,16 @@ typedef struct { mappingentry *ma_table; } mappingobject; -object * -newmappingobject() +PyObject * +PyDict_New() { register mappingobject *mp; if (dummy == NULL) { /* Auto-initialize dummy */ - dummy = newstringobject("<dummy key>"); + dummy = PyString_FromString("<dummy key>"); if (dummy == NULL) return NULL; } - mp = NEWOBJ(mappingobject, &Mappingtype); + mp = PyObject_NEW(mappingobject, &PyDict_Type); if (mp == NULL) return NULL; mp->ma_size = 0; @@ -136,7 +135,7 @@ newmappingobject() mp->ma_table = NULL; mp->ma_fill = 0; mp->ma_used = 0; - return (object *)mp; + return (PyObject *)mp; } /* @@ -160,11 +159,11 @@ where x is a root. The initial value is derived from sum, too. (This version is due to Reimer Behrends, some ideas are also due to Jyrki Alakuijala.) */ -static mappingentry *lookmapping PROTO((mappingobject *, object *, long)); +static mappingentry *lookmapping Py_PROTO((mappingobject *, PyObject *, long)); static mappingentry * lookmapping(mp, key, hash) mappingobject *mp; - object *key; + PyObject *key; long hash; { register int i; @@ -186,7 +185,9 @@ lookmapping(mp, key, hash) if (ep->me_key == dummy) freeslot = ep; else if (ep->me_key == key || - (ep->me_hash == hash && cmpobject(ep->me_key, key) == 0)) { + (ep->me_hash == hash && + PyObject_Compare(ep->me_key, key) == 0)) + { return ep; } /* Derive incr from sum, just to make it more arbitrary. Note that @@ -211,7 +212,7 @@ lookmapping(mp, key, hash) } else if (ep->me_key == key || (ep->me_hash == hash && - cmpobject(ep->me_key, key) == 0)) { + PyObject_Compare(ep->me_key, key) == 0)) { return ep; } /* Cycle through GF(2^n)-{0} */ @@ -226,28 +227,29 @@ Internal routine to insert a new item into the table. Used both by the internal resize routine and by the public insert routine. Eats a reference to key and one to value. */ -static void insertmapping PROTO((mappingobject *, object *, long, object *)); +static void insertmapping + Py_PROTO((mappingobject *, PyObject *, long, PyObject *)); static void insertmapping(mp, key, hash, value) register mappingobject *mp; - object *key; + PyObject *key; long hash; - object *value; + PyObject *value; { - object *old_value; + PyObject *old_value; register mappingentry *ep; ep = lookmapping(mp, key, hash); if (ep->me_value != NULL) { old_value = ep->me_value; ep->me_value = value; - DECREF(old_value); /* which **CAN** re-enter */ - DECREF(key); + Py_DECREF(old_value); /* which **CAN** re-enter */ + Py_DECREF(key); } else { if (ep->me_key == NULL) mp->ma_fill++; else - DECREF(ep->me_key); + Py_DECREF(ep->me_key); ep->me_key = key; ep->me_hash = hash; ep->me_value = value; @@ -260,7 +262,7 @@ Restructure the table by allocating a new table and reinserting all items again. When entries have been deleted, the new table may actually be smaller than the old one. */ -static int mappingresize PROTO((mappingobject *)); +static int mappingresize Py_PROTO((mappingobject *)); static int mappingresize(mp) mappingobject *mp; @@ -275,7 +277,7 @@ mappingresize(mp) for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) { if (i > sizeof(polys)/sizeof(polys[0])) { /* Ran out of polynomials */ - err_nomem(); + PyErr_NoMemory(); return -1; } if (newsize > mp->ma_used*2) { @@ -285,7 +287,7 @@ mappingresize(mp) } newtable = (mappingentry *) calloc(sizeof(mappingentry), newsize); if (newtable == NULL) { - err_nomem(); + PyErr_NoMemory(); return -1; } mp->ma_size = newsize; @@ -302,31 +304,31 @@ mappingresize(mp) } for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { if (ep->me_value == NULL) - XDECREF(ep->me_key); + Py_XDECREF(ep->me_key); } - XDEL(oldtable); + PyMem_XDEL(oldtable); return 0; } -object * -mappinglookup(op, key) - object *op; - object *key; +PyObject * +PyDict_GetItem(op, key) + PyObject *op; + PyObject *key; { long hash; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return NULL; } if (((mappingobject *)op)->ma_table == NULL) return NULL; #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } @@ -334,37 +336,37 @@ mappinglookup(op, key) } int -mappinginsert(op, key, value) - register object *op; - object *key; - object *value; +PyDict_SetItem(op, key, value) + register PyObject *op; + PyObject *key; + PyObject *value; { register mappingobject *mp; register long hash; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return -1; } mp = (mappingobject *)op; #ifdef CACHE_HASH - if (is_stringobject(key)) { + if (PyString_Check(key)) { #ifdef INTERN_STRINGS - if (((stringobject *)key)->ob_sinterned != NULL) { - key = ((stringobject *)key)->ob_sinterned; - hash = ((stringobject *)key)->ob_shash; + if (((PyStringObject *)key)->ob_sinterned != NULL) { + key = ((PyStringObject *)key)->ob_sinterned; + hash = ((PyStringObject *)key)->ob_shash; } else #endif { - hash = ((stringobject *)key)->ob_shash; + hash = ((PyStringObject *)key)->ob_shash; if (hash == -1) - hash = hashobject(key); + hash = PyObject_Hash(key); } } else #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return -1; } @@ -375,32 +377,32 @@ mappinginsert(op, key, value) return -1; } } - INCREF(value); - INCREF(key); + Py_INCREF(value); + Py_INCREF(key); insertmapping(mp, key, hash, value); return 0; } int -mappingremove(op, key) - object *op; - object *key; +PyDict_DelItem(op, key) + PyObject *op; + PyObject *key; { register mappingobject *mp; register long hash; register mappingentry *ep; - object *old_value, *old_key; + PyObject *old_value, *old_key; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return -1; } #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return -1; } @@ -410,28 +412,28 @@ mappingremove(op, key) ep = lookmapping(mp, key, hash); if (ep->me_value == NULL) { empty: - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); return -1; } old_key = ep->me_key; - INCREF(dummy); + Py_INCREF(dummy); ep->me_key = dummy; old_value = ep->me_value; ep->me_value = NULL; mp->ma_used--; - DECREF(old_value); - DECREF(old_key); + Py_DECREF(old_value); + Py_DECREF(old_key); return 0; } void -mappingclear(op) - object *op; +PyDict_Clear(op) + PyObject *op; { int i, n; register mappingentry *table; mappingobject *mp; - if (!is_mappingobject(op)) + if (!PyDict_Check(op)) return; mp = (mappingobject *)op; table = mp->ma_table; @@ -441,22 +443,22 @@ mappingclear(op) mp->ma_size = mp->ma_used = mp->ma_fill = 0; mp->ma_table = NULL; for (i = 0; i < n; i++) { - XDECREF(table[i].me_key); - XDECREF(table[i].me_value); + Py_XDECREF(table[i].me_key); + Py_XDECREF(table[i].me_value); } - DEL(table); + PyMem_DEL(table); } int -mappinggetnext(op, ppos, pkey, pvalue) - object *op; +PyDict_Next(op, ppos, pkey, pvalue) + PyObject *op; int *ppos; - object **pkey; - object **pvalue; + PyObject **pkey; + PyObject **pvalue; { int i; register mappingobject *mp; - if (!is_dictobject(op)) + if (!PyDict_Check(op)) return 0; mp = (mappingobject *)op; i = *ppos; @@ -484,12 +486,12 @@ mapping_dealloc(mp) register mappingentry *ep; for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { if (ep->me_key != NULL) - DECREF(ep->me_key); + Py_DECREF(ep->me_key); if (ep->me_value != NULL) - DECREF(ep->me_value); + Py_DECREF(ep->me_value); } - XDEL(mp->ma_table); - DEL(mp); + PyMem_XDEL(mp->ma_table); + PyMem_DEL(mp); } static int @@ -507,10 +509,10 @@ mapping_print(mp, fp, flags) if (ep->me_value != NULL) { if (any++ > 0) fprintf(fp, ", "); - if (printobject((object *)ep->me_key, fp, 0) != 0) + if (PyObject_Print((PyObject *)ep->me_key, fp, 0) != 0) return -1; fprintf(fp, ": "); - if (printobject(ep->me_value, fp, 0) != 0) + if (PyObject_Print(ep->me_value, fp, 0) != 0) return -1; } } @@ -518,31 +520,31 @@ mapping_print(mp, fp, flags) return 0; } -static object * +static PyObject * mapping_repr(mp) mappingobject *mp; { - auto object *v; - object *sepa, *colon; + auto PyObject *v; + PyObject *sepa, *colon; register int i; register int any; register mappingentry *ep; - v = newstringobject("{"); - sepa = newstringobject(", "); - colon = newstringobject(": "); + v = PyString_FromString("{"); + sepa = PyString_FromString(", "); + colon = PyString_FromString(": "); any = 0; for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) { if (ep->me_value != NULL) { if (any++) - joinstring(&v, sepa); - joinstring_decref(&v, reprobject(ep->me_key)); - joinstring(&v, colon); - joinstring_decref(&v, reprobject(ep->me_value)); + PyString_Concat(&v, sepa); + PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); + PyString_Concat(&v, colon); + PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); } } - joinstring_decref(&v, newstringobject("}")); - XDECREF(sepa); - XDECREF(colon); + PyString_ConcatAndDel(&v, PyString_FromString("}")); + Py_XDECREF(sepa); + Py_XDECREF(colon); return v; } @@ -553,123 +555,123 @@ mapping_length(mp) return mp->ma_used; } -static object * +static PyObject * mapping_subscript(mp, key) mappingobject *mp; - register object *key; + register PyObject *key; { - object *v; + PyObject *v; long hash; if (mp->ma_table == NULL) { - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); return NULL; } #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } v = lookmapping(mp, key, hash) -> me_value; if (v == NULL) - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); else - INCREF(v); + Py_INCREF(v); return v; } static int mapping_ass_sub(mp, v, w) mappingobject *mp; - object *v, *w; + PyObject *v, *w; { if (w == NULL) - return mappingremove((object *)mp, v); + return PyDict_DelItem((PyObject *)mp, v); else - return mappinginsert((object *)mp, v, w); + return PyDict_SetItem((PyObject *)mp, v, w); } -static mapping_methods mapping_as_mapping = { +static PyMappingMethods mapping_as_mapping = { (inquiry)mapping_length, /*mp_length*/ (binaryfunc)mapping_subscript, /*mp_subscript*/ (objobjargproc)mapping_ass_sub, /*mp_ass_subscript*/ }; -static object * +static PyObject * mapping_keys(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *key = mp->ma_table[i].me_key; - INCREF(key); - setlistitem(v, j, key); + PyObject *key = mp->ma_table[i].me_key; + Py_INCREF(key); + PyList_SetItem(v, j, key); j++; } } return v; } -static object * +static PyObject * mapping_values(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *value = mp->ma_table[i].me_value; - INCREF(value); - setlistitem(v, j, value); + PyObject *value = mp->ma_table[i].me_value; + Py_INCREF(value); + PyList_SetItem(v, j, value); j++; } } return v; } -static object * +static PyObject * mapping_items(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *key = mp->ma_table[i].me_key; - object *value = mp->ma_table[i].me_value; - object *item = newtupleobject(2); + PyObject *key = mp->ma_table[i].me_key; + PyObject *value = mp->ma_table[i].me_value; + PyObject *item = PyTuple_New(2); if (item == NULL) { - DECREF(v); + Py_DECREF(v); return NULL; } - INCREF(key); - settupleitem(item, 0, key); - INCREF(value); - settupleitem(item, 1, value); - setlistitem(v, j, item); + Py_INCREF(key); + PyTuple_SetItem(item, 0, key); + Py_INCREF(value); + PyTuple_SetItem(item, 1, value); + PyList_SetItem(v, j, item); j++; } } @@ -677,47 +679,47 @@ mapping_items(mp, args) } int -getmappingsize(mp) - object *mp; +PyDict_Size(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return 0; } return ((mappingobject *)mp)->ma_used; } -object * -getmappingkeys(mp) - object *mp; +PyObject * +PyDict_Keys(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_keys((mappingobject *)mp, (object *)NULL); + return mapping_keys((mappingobject *)mp, (PyObject *)NULL); } -object * -getmappingvalues(mp) - object *mp; +PyObject * +PyDict_Values(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_values((mappingobject *)mp, (object *)NULL); + return mapping_values((mappingobject *)mp, (PyObject *)NULL); } -object * -getmappingitems(mp) - object *mp; +PyObject * +PyDict_Items(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_items((mappingobject *)mp, (object *)NULL); + return mapping_items((mappingobject *)mp, (PyObject *)NULL); } #define NEWCMP @@ -728,25 +730,26 @@ getmappingitems(mp) is different or absent. The value is returned too, through the pval argument. No reference counts are incremented. */ -static object * +static PyObject * characterize(a, b, pval) mappingobject *a; mappingobject *b; - object **pval; + PyObject **pval; { - object *diff = NULL; + PyObject *diff = NULL; int i; *pval = NULL; for (i = 0; i < a->ma_size; i++) { if (a->ma_table[i].me_value != NULL) { - object *key = a->ma_table[i].me_key; - object *aval, *bval; - if (diff != NULL && cmpobject(key, diff) > 0) + PyObject *key = a->ma_table[i].me_key; + PyObject *aval, *bval; + if (diff != NULL && PyObject_Compare(key, diff) > 0) continue; aval = a->ma_table[i].me_value; - bval = mappinglookup((object *)b, key); - if (bval == NULL || cmpobject(aval, bval) != 0) { + bval = PyDict_GetItem((PyObject *)b, key); + if (bval == NULL || PyObject_Compare(aval, bval) != 0) + { diff = key; *pval = aval; } @@ -759,7 +762,7 @@ static int mapping_compare(a, b) mappingobject *a, *b; { - object *adiff, *bdiff, *aval, *bval; + PyObject *adiff, *bdiff, *aval, *bval; int res; /* Compare lengths first */ @@ -773,9 +776,9 @@ mapping_compare(a, b) return 0; /* a is a subset with the same length */ bdiff = characterize(b, a, &bval); /* bdiff == NULL would be impossible now */ - res = cmpobject(adiff, bdiff); + res = PyObject_Compare(adiff, bdiff); if (res == 0) - res = cmpobject(aval, bval); + res = PyObject_Compare(aval, bval); return res; } @@ -785,7 +788,7 @@ static int mapping_compare(a, b) mappingobject *a, *b; { - object *akeys, *bkeys; + PyObject *akeys, *bkeys; int i, n, res; if (a == b) return 0; @@ -799,51 +802,51 @@ mapping_compare(a, b) if (b->ma_used == 0) return 1; } - akeys = mapping_keys(a, (object *)NULL); - bkeys = mapping_keys(b, (object *)NULL); + akeys = mapping_keys(a, (PyObject *)NULL); + bkeys = mapping_keys(b, (PyObject *)NULL); if (akeys == NULL || bkeys == NULL) { /* Oops, out of memory -- what to do? */ /* For now, sort on address! */ - XDECREF(akeys); - XDECREF(bkeys); + Py_XDECREF(akeys); + Py_XDECREF(bkeys); if (a < b) return -1; else return 1; } - sortlist(akeys); - sortlist(bkeys); + PyList_Sort(akeys); + PyList_Sort(bkeys); n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ res = 0; for (i = 0; i < n; i++) { - object *akey, *bkey, *aval, *bval; + PyObject *akey, *bkey, *aval, *bval; long ahash, bhash; - akey = getlistitem(akeys, i); - bkey = getlistitem(bkeys, i); - res = cmpobject(akey, bkey); + akey = PyList_GetItem(akeys, i); + bkey = PyList_GetItem(bkeys, i); + res = PyObject_Compare(akey, bkey); if (res != 0) break; #ifdef CACHE_HASH - if (!is_stringobject(akey) || - (ahash = ((stringobject *) akey)->ob_shash) == -1) + if (!PyString_Check(akey) || + (ahash = ((PyStringObject *) akey)->ob_shash) == -1) #endif { - ahash = hashobject(akey); + ahash = PyObject_Hash(akey); if (ahash == -1) - err_clear(); /* Don't want errors here */ + PyErr_Clear(); /* Don't want errors here */ } #ifdef CACHE_HASH - if (!is_stringobject(bkey) || - (bhash = ((stringobject *) bkey)->ob_shash) == -1) + if (!PyString_Check(bkey) || + (bhash = ((PyStringObject *) bkey)->ob_shash) == -1) #endif { - bhash = hashobject(bkey); + bhash = PyObject_Hash(bkey); if (bhash == -1) - err_clear(); /* Don't want errors here */ + PyErr_Clear(); /* Don't want errors here */ } aval = lookmapping(a, akey, ahash) -> me_value; bval = lookmapping(b, bkey, bhash) -> me_value; - res = cmpobject(aval, bval); + res = PyObject_Compare(aval, bval); if (res != 0) break; } @@ -853,67 +856,67 @@ mapping_compare(a, b) else if (a->ma_used > b->ma_used) res = 1; } - DECREF(akeys); - DECREF(bkeys); + Py_DECREF(akeys); + Py_DECREF(bkeys); return res; } #endif /* !NEWCMP */ -static object * +static PyObject * mapping_has_key(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - object *key; + PyObject *key; long hash; register long ok; - if (!getargs(args, "O", &key)) + if (!PyArg_Parse(args, "O", &key)) return NULL; #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } ok = mp->ma_size != 0 && lookmapping(mp, key, hash)->me_value != NULL; - return newintobject(ok); + return PyInt_FromLong(ok); } -static object * +static PyObject * mapping_clear(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - mappingclear((object *)mp); - INCREF(None); - return None; + PyDict_Clear((PyObject *)mp); + Py_INCREF(Py_None); + return Py_None; } -static struct methodlist mapp_methods[] = { - {"clear", (method)mapping_clear}, - {"has_key", (method)mapping_has_key}, - {"items", (method)mapping_items}, - {"keys", (method)mapping_keys}, - {"values", (method)mapping_values}, +static PyMethodDef mapp_methods[] = { + {"clear", (PyCFunction)mapping_clear}, + {"has_key", (PyCFunction)mapping_has_key}, + {"items", (PyCFunction)mapping_items}, + {"keys", (PyCFunction)mapping_keys}, + {"values", (PyCFunction)mapping_values}, {NULL, NULL} /* sentinel */ }; -static object * +static PyObject * mapping_getattr(mp, name) mappingobject *mp; char *name; { - return findmethod(mapp_methods, (object *)mp, name); + return Py_FindMethod(mapp_methods, (PyObject *)mp, name); } -typeobject Mappingtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyDict_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "dictionary", sizeof(mappingobject), @@ -931,100 +934,100 @@ typeobject Mappingtype = { /* For backward compatibility with old dictionary interface */ -static object *last_name_object; +static PyObject *last_name_object; static char *last_name_char; /* NULL or == getstringvalue(last_name_object) */ -object * -getattro(v, name) - object *v; - object *name; +PyObject * +PyObject_GetAttr(v, name) + PyObject *v; + PyObject *name; { if (v->ob_type->tp_getattro != NULL) return (*v->ob_type->tp_getattro)(v, name); if (name != last_name_object) { - XDECREF(last_name_object); - INCREF(name); + Py_XDECREF(last_name_object); + Py_INCREF(name); last_name_object = name; - last_name_char = getstringvalue(name); + last_name_char = PyString_AsString(name); } - return getattr(v, last_name_char); + return PyObject_GetAttrString(v, last_name_char); } int -setattro(v, name, value) - object *v; - object *name; - object *value; +PyObject_SetAttr(v, name, value) + PyObject *v; + PyObject *name; + PyObject *value; { int err; - INCREF(name); + Py_INCREF(name); PyString_InternInPlace(&name); if (v->ob_type->tp_setattro != NULL) err = (*v->ob_type->tp_setattro)(v, name, value); else { if (name != last_name_object) { - XDECREF(last_name_object); - INCREF(name); + Py_XDECREF(last_name_object); + Py_INCREF(name); last_name_object = name; - last_name_char = getstringvalue(name); + last_name_char = PyString_AsString(name); } - err = setattr(v, last_name_char, value); + err = PyObject_SetAttrString(v, last_name_char, value); } - DECREF(name); + Py_DECREF(name); return err; } -object * -dictlookup(v, key) - object *v; +PyObject * +PyDict_GetItemString(v, key) + PyObject *v; char *key; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return NULL; } PyString_InternInPlace(&last_name_object); - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappinglookup(v, last_name_object); + return PyDict_GetItem(v, last_name_object); } int -dictinsert(v, key, item) - object *v; +PyDict_SetItemString(v, key, item) + PyObject *v; char *key; - object *item; + PyObject *item; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return -1; } PyString_InternInPlace(&last_name_object); - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappinginsert(v, last_name_object, item); + return PyDict_SetItem(v, last_name_object, item); } int -dictremove(v, key) - object *v; +PyDict_DelItemString(v, key) + PyObject *v; char *key; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return -1; } - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappingremove(v, last_name_object); + return PyDict_DelItem(v, last_name_object); } diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 2ba63bd..cee41a10 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -31,10 +31,8 @@ PERFORMANCE OF THIS SOFTWARE. /* File object implementation */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" #include "structmember.h" -#include "ceval.h" #ifdef HAVE_UNISTD_H #include <unistd.h> @@ -48,69 +46,69 @@ PERFORMANCE OF THIS SOFTWARE. #define NO_FOPEN_ERRNO #endif -#define BUF(v) GETSTRINGVALUE((stringobject *)v) +#define BUF(v) PyString_AS_STRING((PyStringObject *)v) #include <errno.h> typedef struct { - OB_HEAD + PyObject_HEAD FILE *f_fp; - object *f_name; - object *f_mode; - int (*f_close) PROTO((FILE *)); + PyObject *f_name; + PyObject *f_mode; + int (*f_close) Py_PROTO((FILE *)); int f_softspace; /* Flag used by 'print' command */ -} fileobject; +} PyFileObject; FILE * -getfilefile(f) - object *f; +PyFile_AsFile(f) + PyObject *f; { - if (f == NULL || !is_fileobject(f)) + if (f == NULL || !PyFile_Check(f)) return NULL; else - return ((fileobject *)f)->f_fp; + return ((PyFileObject *)f)->f_fp; } -object * -getfilename(f) - object *f; +PyObject * +PyFile_Name(f) + PyObject *f; { - if (f == NULL || !is_fileobject(f)) + if (f == NULL || !PyFile_Check(f)) return NULL; else - return ((fileobject *)f)->f_name; + return ((PyFileObject *)f)->f_name; } -object * -newopenfileobject(fp, name, mode, close) +PyObject * +PyFile_FromFile(fp, name, mode, close) FILE *fp; char *name; char *mode; - int (*close) FPROTO((FILE *)); + int (*close) Py_FPROTO((FILE *)); { - fileobject *f = NEWOBJ(fileobject, &Filetype); + PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type); if (f == NULL) return NULL; f->f_fp = NULL; - f->f_name = newstringobject(name); - f->f_mode = newstringobject(mode); + f->f_name = PyString_FromString(name); + f->f_mode = PyString_FromString(mode); f->f_close = close; f->f_softspace = 0; if (f->f_name == NULL || f->f_mode == NULL) { - DECREF(f); + Py_DECREF(f); return NULL; } f->f_fp = fp; - return (object *) f; + return (PyObject *) f; } -object * -newfileobject(name, mode) +PyObject * +PyFile_FromString(name, mode) char *name, *mode; { - extern int fclose PROTO((FILE *)); - fileobject *f; - f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose); + extern int fclose Py_PROTO((FILE *)); + PyFileObject *f; + f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose); if (f == NULL) return NULL; #ifdef HAVE_FOPENRF @@ -121,28 +119,28 @@ newfileobject(name, mode) else #endif { - BGN_SAVE + Py_BEGIN_ALLOW_THREADS f->f_fp = fopen(name, mode); - END_SAVE + Py_END_ALLOW_THREADS } if (f->f_fp == NULL) { #ifdef NO_FOPEN_ERRNO if ( errno == 0 ) { - err_setstr(IOError, "Cannot open file"); - DECREF(f); + PyErr_SetString(PyExc_IOError, "Cannot open file"); + Py_DECREF(f); return NULL; } #endif - err_errno(IOError); - DECREF(f); + PyErr_SetFromErrno(PyExc_IOError); + Py_DECREF(f); return NULL; } - return (object *)f; + return (PyObject *)f; } void -setfilebufsize(f, bufsize) - object *f; +PyFile_SetBufSize(f, bufsize) + PyObject *f; int bufsize; { if (bufsize >= 0) { @@ -159,15 +157,16 @@ setfilebufsize(f, bufsize) default: type = _IOFBF; } - setvbuf(((fileobject *)f)->f_fp, (char *)NULL, type, bufsize); + setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, + type, bufsize); #endif /* HAVE_SETVBUF */ } } -static object * +static PyObject * err_closed() { - err_setstr(ValueError, "I/O operation on closed file"); + PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); return NULL; } @@ -175,62 +174,62 @@ err_closed() static void file_dealloc(f) - fileobject *f; + PyFileObject *f; { if (f->f_fp != NULL && f->f_close != NULL) { - BGN_SAVE + Py_BEGIN_ALLOW_THREADS (*f->f_close)(f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS } if (f->f_name != NULL) - DECREF(f->f_name); + Py_DECREF(f->f_name); if (f->f_mode != NULL) - DECREF(f->f_mode); + Py_DECREF(f->f_mode); free((char *)f); } -static object * +static PyObject * file_repr(f) - fileobject *f; + PyFileObject *f; { char buf[300]; sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>", f->f_fp == NULL ? "closed" : "open", - getstringvalue(f->f_name), - getstringvalue(f->f_mode), + PyString_AsString(f->f_name), + PyString_AsString(f->f_mode), (long)f); - return newstringobject(buf); + return PyString_FromString(buf); } -static object * +static PyObject * file_close(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { int sts = 0; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; if (f->f_fp != NULL) { if (f->f_close != NULL) { - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; sts = (*f->f_close)(f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS } f->f_fp = NULL; } if (sts == EOF) - return err_errno(IOError); + return PyErr_SetFromErrno(PyExc_IOError); if (sts != 0) - return newintobject((long)sts); - INCREF(None); - return None; + return PyInt_FromLong((long)sts); + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * file_seek(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { long offset; int whence; @@ -239,165 +238,165 @@ file_seek(f, args) if (f->f_fp == NULL) return err_closed(); whence = 0; - if (!getargs(args, "l", &offset)) { - err_clear(); - if (!getargs(args, "(li)", &offset, &whence)) + if (!PyArg_Parse(args, "l", &offset)) { + PyErr_Clear(); + if (!PyArg_Parse(args, "(li)", &offset, &whence)) return NULL; } - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; ret = fseek(f->f_fp, offset, whence); - END_SAVE + Py_END_ALLOW_THREADS if (ret != 0) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #ifdef HAVE_FTRUNCATE -static object * +static PyObject * file_truncate(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { long newsize; int ret; if (f->f_fp == NULL) return err_closed(); - if (!getargs(args, "l", &newsize)) { - err_clear(); - if (!getnoarg(args)) + if (!PyArg_Parse(args, "l", &newsize)) { + PyErr_Clear(); + if (!PyArg_NoArgs(args)) return NULL; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; newsize = ftell(f->f_fp); /* default to current position*/ - END_SAVE + Py_END_ALLOW_THREADS if (newsize == -1L) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } } - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; ret = fflush(f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS if (ret == 0) { - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; ret = ftruncate(fileno(f->f_fp), newsize); - END_SAVE + Py_END_ALLOW_THREADS } if (ret != 0) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #endif /* HAVE_FTRUNCATE */ -static object * +static PyObject * file_tell(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { long offset; if (f->f_fp == NULL) return err_closed(); - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; offset = ftell(f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS if (offset == -1L) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } - return newintobject(offset); + return PyInt_FromLong(offset); } -static object * +static PyObject * file_fileno(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { if (f->f_fp == NULL) return err_closed(); - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - return newintobject((long) fileno(f->f_fp)); + return PyInt_FromLong((long) fileno(f->f_fp)); } -static object * +static PyObject * file_flush(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { int res; if (f->f_fp == NULL) return err_closed(); - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; res = fflush(f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS if (res != 0) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * file_isatty(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { long res; if (f->f_fp == NULL) return err_closed(); - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS res = isatty((int)fileno(f->f_fp)); - END_SAVE - return newintobject(res); + Py_END_ALLOW_THREADS + return PyInt_FromLong(res); } -static object * +static PyObject * file_read(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { int n, n1, n2, n3; - object *v; + PyObject *v; if (f->f_fp == NULL) return err_closed(); if (args == NULL) n = -1; else { - if (!getargs(args, "i", &n)) + if (!PyArg_Parse(args, "i", &n)) return NULL; } n2 = n >= 0 ? n : BUFSIZ; - v = newsizedstringobject((char *)NULL, n2); + v = PyString_FromStringAndSize((char *)NULL, n2); if (v == NULL) return NULL; n1 = 0; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS for (;;) { n3 = fread(BUF(v)+n1, 1, n2-n1, f->f_fp); /* XXX Error check? */ @@ -408,15 +407,15 @@ file_read(f, args) break; if (n < 0) { n2 = n1 + BUFSIZ; - RET_SAVE - if (resizestring(&v, n2) < 0) + Py_BLOCK_THREADS + if (_PyString_Resize(&v, n2) < 0) return NULL; - RES_SAVE + Py_UNBLOCK_THREADS } } - END_SAVE + Py_END_ALLOW_THREADS if (n1 != n2) - resizestring(&v, n1); + _PyString_Resize(&v, n1); return v; } @@ -427,38 +426,38 @@ file_read(f, args) < 0: strip trailing '\n', raise EOFError if EOF reached immediately */ -static object * +static PyObject * getline(f, n) - fileobject *f; + PyFileObject *f; int n; { register FILE *fp; register int c; register char *buf, *end; int n1, n2; - object *v; + PyObject *v; fp = f->f_fp; n2 = n > 0 ? n : 100; - v = newsizedstringobject((char *)NULL, n2); + v = PyString_FromStringAndSize((char *)NULL, n2); if (v == NULL) return NULL; buf = BUF(v); end = buf + n2; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS for (;;) { if ((c = getc(fp)) == EOF) { clearerr(fp); - if (sigcheck()) { - RET_SAVE - DECREF(v); + if (PyErr_CheckSignals()) { + Py_BLOCK_THREADS + Py_DECREF(v); return NULL; } if (n < 0 && buf == BUF(v)) { - RET_SAVE - DECREF(v); - err_setstr(EOFError, + Py_BLOCK_THREADS + Py_DECREF(v); + PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); return NULL; } @@ -474,90 +473,91 @@ getline(f, n) break; n1 = n2; n2 += 1000; - RET_SAVE - if (resizestring(&v, n2) < 0) + Py_BLOCK_THREADS + if (_PyString_Resize(&v, n2) < 0) return NULL; - RES_SAVE + Py_UNBLOCK_THREADS buf = BUF(v) + n1; end = BUF(v) + n2; } } - END_SAVE + Py_END_ALLOW_THREADS n1 = buf - BUF(v); if (n1 != n2) - resizestring(&v, n1); + _PyString_Resize(&v, n1); return v; } /* External C interface */ -object * -filegetline(f, n) - object *f; +PyObject * +PyFile_GetLine(f, n) + PyObject *f; int n; { if (f == NULL) { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } - if (!is_fileobject(f)) { - object *reader; - object *args; - object *result; - reader = getattr(f, "readline"); + if (!PyFile_Check(f)) { + PyObject *reader; + PyObject *args; + PyObject *result; + reader = PyObject_GetAttrString(f, "readline"); if (reader == NULL) return NULL; if (n <= 0) - args = mkvalue("()"); + args = Py_BuildValue("()"); else - args = mkvalue("(i)", n); + args = Py_BuildValue("(i)", n); if (args == NULL) { - DECREF(reader); + Py_DECREF(reader); return NULL; } - result = call_object(reader, args); - DECREF(reader); - DECREF(args); - if (result != NULL && !is_stringobject(result)) { - DECREF(result); + result = PyEval_CallObject(reader, args); + Py_DECREF(reader); + Py_DECREF(args); + if (result != NULL && !PyString_Check(result)) { + Py_DECREF(result); result = NULL; - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "object.readline() returned non-string"); } if (n < 0 && result != NULL) { - char *s = getstringvalue(result); - int len = getstringsize(result); + char *s = PyString_AsString(result); + int len = PyString_Size(result); if (len == 0) { - DECREF(result); + Py_DECREF(result); result = NULL; - err_setstr(EOFError, + PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) - resizestring(&result, len-1); + _PyString_Resize(&result, len-1); else { - object *v; - v = newsizedstringobject(s, len-1); - DECREF(result); + PyObject *v; + v = PyString_FromStringAndSize(s, + len-1); + Py_DECREF(result); result = v; } } } return result; } - if (((fileobject*)f)->f_fp == NULL) + if (((PyFileObject*)f)->f_fp == NULL) return err_closed(); - return getline((fileobject *)f, n); + return getline((PyFileObject *)f, n); } /* Python method */ -static object * +static PyObject * file_readline(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { int n; @@ -566,10 +566,10 @@ file_readline(f, args) if (args == NULL) n = 0; /* Unlimited */ else { - if (!getintarg(args, &n)) + if (!PyArg_Parse(args, "i", &n)) return NULL; if (n == 0) - return newstringobject(""); + return PyString_FromString(""); if (n < 0) n = 0; } @@ -577,121 +577,121 @@ file_readline(f, args) return getline(f, n); } -static object * +static PyObject * file_readlines(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { - object *list; - object *line; + PyObject *list; + PyObject *line; if (f->f_fp == NULL) return err_closed(); - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - if ((list = newlistobject(0)) == NULL) + if ((list = PyList_New(0)) == NULL) return NULL; for (;;) { line = getline(f, 0); - if (line != NULL && getstringsize(line) == 0) { - DECREF(line); + if (line != NULL && PyString_Size(line) == 0) { + Py_DECREF(line); break; } - if (line == NULL || addlistitem(list, line) != 0) { - DECREF(list); - XDECREF(line); + if (line == NULL || PyList_Append(list, line) != 0) { + Py_DECREF(list); + Py_XDECREF(line); return NULL; } - DECREF(line); + Py_DECREF(line); } return list; } -static object * +static PyObject * file_write(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { char *s; int n, n2; if (f->f_fp == NULL) return err_closed(); - if (!getargs(args, "s#", &s, &n)) + if (!PyArg_Parse(args, "s#", &s, &n)) return NULL; f->f_softspace = 0; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; n2 = fwrite(s, 1, n, f->f_fp); - END_SAVE + Py_END_ALLOW_THREADS if (n2 != n) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * file_writelines(f, args) - fileobject *f; - object *args; + PyFileObject *f; + PyObject *args; { int i, n; if (f->f_fp == NULL) return err_closed(); - if (args == NULL || !is_listobject(args)) { - err_setstr(TypeError, + if (args == NULL || !PyList_Check(args)) { + PyErr_SetString(PyExc_TypeError, "writelines() requires list of strings"); return NULL; } - n = getlistsize(args); + n = PyList_Size(args); f->f_softspace = 0; - BGN_SAVE + Py_BEGIN_ALLOW_THREADS errno = 0; for (i = 0; i < n; i++) { - object *line = getlistitem(args, i); + PyObject *line = PyList_GetItem(args, i); int len; int nwritten; - if (!is_stringobject(line)) { - RET_SAVE - err_setstr(TypeError, + if (!PyString_Check(line)) { + Py_BLOCK_THREADS + PyErr_SetString(PyExc_TypeError, "writelines() requires list of strings"); return NULL; } - len = getstringsize(line); - nwritten = fwrite(getstringvalue(line), 1, len, f->f_fp); + len = PyString_Size(line); + nwritten = fwrite(PyString_AsString(line), 1, len, f->f_fp); if (nwritten != len) { - RET_SAVE - err_errno(IOError); + Py_BLOCK_THREADS + PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } } - END_SAVE - INCREF(None); - return None; + Py_END_ALLOW_THREADS + Py_INCREF(Py_None); + return Py_None; } -static struct methodlist file_methods[] = { - {"close", (method)file_close, 0}, - {"flush", (method)file_flush, 0}, - {"fileno", (method)file_fileno, 0}, - {"isatty", (method)file_isatty, 0}, - {"read", (method)file_read, 0}, - {"readline", (method)file_readline, 0}, - {"readlines", (method)file_readlines, 0}, - {"seek", (method)file_seek, 0}, +static PyMethodDef file_methods[] = { + {"close", (PyCFunction)file_close, 0}, + {"flush", (PyCFunction)file_flush, 0}, + {"fileno", (PyCFunction)file_fileno, 0}, + {"isatty", (PyCFunction)file_isatty, 0}, + {"read", (PyCFunction)file_read, 0}, + {"readline", (PyCFunction)file_readline, 0}, + {"readlines", (PyCFunction)file_readlines, 0}, + {"seek", (PyCFunction)file_seek, 0}, #ifdef HAVE_FTRUNCATE - {"truncate", (method)file_truncate, 0}, + {"truncate", (PyCFunction)file_truncate, 0}, #endif - {"tell", (method)file_tell, 0}, - {"write", (method)file_write, 0}, - {"writelines", (method)file_writelines, 0}, + {"tell", (PyCFunction)file_tell, 0}, + {"write", (PyCFunction)file_write, 0}, + {"writelines", (PyCFunction)file_writelines, 0}, {NULL, NULL} /* sentinel */ }; -#define OFF(x) offsetof(fileobject, x) +#define OFF(x) offsetof(PyFileObject, x) static struct memberlist file_memberlist[] = { {"softspace", T_INT, OFF(f_softspace)}, @@ -702,40 +702,41 @@ static struct memberlist file_memberlist[] = { {NULL} /* Sentinel */ }; -static object * +static PyObject * file_getattr(f, name) - fileobject *f; + PyFileObject *f; char *name; { - object *res; + PyObject *res; - res = findmethod(file_methods, (object *)f, name); + res = Py_FindMethod(file_methods, (PyObject *)f, name); if (res != NULL) return res; - err_clear(); + PyErr_Clear(); if (strcmp(name, "closed") == 0) - return newintobject((long)(f->f_fp == 0)); - return getmember((char *)f, file_memberlist, name); + return PyInt_FromLong((long)(f->f_fp == 0)); + return PyMember_Get((char *)f, file_memberlist, name); } static int file_setattr(f, name, v) - fileobject *f; + PyFileObject *f; char *name; - object *v; + PyObject *v; { if (v == NULL) { - err_setstr(AttributeError, "can't delete file attributes"); + PyErr_SetString(PyExc_AttributeError, + "can't delete file attributes"); return -1; } - return setmember((char *)f, file_memberlist, name, v); + return PyMember_Set((char *)f, file_memberlist, name, v); } -typeobject Filetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyFile_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "file", - sizeof(fileobject), + sizeof(PyFileObject), 0, (destructor)file_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -748,35 +749,35 @@ typeobject Filetype = { /* Interface for the 'soft space' between print items. */ int -softspace(f, newflag) - object *f; +PyFile_SoftSpace(f, newflag) + PyObject *f; int newflag; { int oldflag = 0; if (f == NULL) { /* Do nothing */ } - else if (is_fileobject(f)) { - oldflag = ((fileobject *)f)->f_softspace; - ((fileobject *)f)->f_softspace = newflag; + else if (PyFile_Check(f)) { + oldflag = ((PyFileObject *)f)->f_softspace; + ((PyFileObject *)f)->f_softspace = newflag; } else { - object *v; - v = getattr(f, "softspace"); + PyObject *v; + v = PyObject_GetAttrString(f, "softspace"); if (v == NULL) - err_clear(); + PyErr_Clear(); else { - if (is_intobject(v)) - oldflag = getintvalue(v); - DECREF(v); + if (PyInt_Check(v)) + oldflag = PyInt_AsLong(v); + Py_DECREF(v); } - v = newintobject((long)newflag); + v = PyInt_FromLong((long)newflag); if (v == NULL) - err_clear(); + PyErr_Clear(); else { - if (setattr(f, "softspace", v) != 0) - err_clear(); - DECREF(v); + if (PyObject_SetAttrString(f, "softspace", v) != 0) + PyErr_Clear(); + Py_DECREF(v); } } return oldflag; @@ -785,73 +786,73 @@ softspace(f, newflag) /* Interfaces to write objects/strings to file-like objects */ int -writeobject(v, f, flags) - object *v; - object *f; +PyFile_WriteObject(v, f, flags) + PyObject *v; + PyObject *f; int flags; { - object *writer, *value, *args, *result; + PyObject *writer, *value, *args, *result; if (f == NULL) { - err_setstr(TypeError, "writeobject with NULL file"); + PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); return -1; } - else if (is_fileobject(f)) { - FILE *fp = getfilefile(f); + else if (PyFile_Check(f)) { + FILE *fp = PyFile_AsFile(f); if (fp == NULL) { err_closed(); return -1; } - return printobject(v, fp, flags); + return PyObject_Print(v, fp, flags); } - writer = getattr(f, "write"); + writer = PyObject_GetAttrString(f, "write"); if (writer == NULL) return -1; - if (flags & PRINT_RAW) - value = strobject(v); + if (flags & Py_PRINT_RAW) + value = PyObject_Str(v); else - value = reprobject(v); + value = PyObject_Repr(v); if (value == NULL) { - DECREF(writer); + Py_DECREF(writer); return -1; } - args = mkvalue("(O)", value); + args = Py_BuildValue("(O)", value); if (value == NULL) { - DECREF(value); - DECREF(writer); + Py_DECREF(value); + Py_DECREF(writer); return -1; } - result = call_object(writer, args); - DECREF(args); - DECREF(value); - DECREF(writer); + result = PyEval_CallObject(writer, args); + Py_DECREF(args); + Py_DECREF(value); + Py_DECREF(writer); if (result == NULL) return -1; - DECREF(result); + Py_DECREF(result); return 0; } void -writestring(s, f) +PyFile_WriteString(s, f) char *s; - object *f; + PyObject *f; { if (f == NULL) { /* Do nothing */ } - else if (is_fileobject(f)) { - FILE *fp = getfilefile(f); + else if (PyFile_Check(f)) { + FILE *fp = PyFile_AsFile(f); if (fp != NULL) fputs(s, fp); } - else if (!err_occurred()) { - object *v = newstringobject(s); + else if (!PyErr_Occurred()) { + PyObject *v = PyString_FromString(s); if (v == NULL) { - err_clear(); + PyErr_Clear(); } else { - if (writeobject(v, f, PRINT_RAW) != 0) - err_clear(); - DECREF(v); + if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0) + PyErr_Clear(); + Py_DECREF(v); } } } diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 72b68a9..d66bf85 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -34,10 +34,8 @@ PERFORMANCE OF THIS SOFTWARE. /* XXX There should be overflow checks here, but it's hard to check for any kind of float exception without losing portability. */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" -#include <errno.h> #include <ctype.h> #include "mymath.h" @@ -81,62 +79,64 @@ PERFORMANCE OF THIS SOFTWARE. #endif #if !defined(__STDC__) && !defined(macintosh) -extern double fmod PROTO((double, double)); -extern double pow PROTO((double, double)); +extern double fmod Py_PROTO((double, double)); +extern double pow Py_PROTO((double, double)); #endif -object * +PyObject * #ifdef __SC__ -newfloatobject(double fval) +PyFloat_FromDouble(double fval) #else -newfloatobject(fval) +PyFloat_FromDouble(fval) double fval; #endif { /* For efficiency, this code is copied from newobject() */ - register floatobject *op = (floatobject *) malloc(sizeof(floatobject)); + register PyFloatObject *op = + (PyFloatObject *) malloc(sizeof(PyFloatObject)); if (op == NULL) - return err_nomem(); - op->ob_type = &Floattype; + return PyErr_NoMemory(); + op->ob_type = &PyFloat_Type; op->ob_fval = fval; - NEWREF(op); - return (object *) op; + _Py_NewReference(op); + return (PyObject *) op; } static void float_dealloc(op) - object *op; + PyObject *op; { - DEL(op); + PyMem_DEL(op); } double -getfloatvalue(op) - object *op; +PyFloat_AsDouble(op) + PyObject *op; { - number_methods *nb; - floatobject *fo; + PyNumberMethods *nb; + PyFloatObject *fo; double val; - if (op && is_floatobject(op)) - return GETFLOATVALUE((floatobject*) op); + if (op && PyFloat_Check(op)) + return PyFloat_AS_DOUBLE((PyFloatObject*) op); if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) { - err_badarg(); + PyErr_BadArgument(); return -1; } - fo = (floatobject*) (*nb->nb_float) (op); + fo = (PyFloatObject*) (*nb->nb_float) (op); if (fo == NULL) return -1; - if (!is_floatobject(fo)) { - err_setstr(TypeError, "nb_float should return float object"); + if (!PyFloat_Check(fo)) { + PyErr_SetString(PyExc_TypeError, + "nb_float should return float object"); return -1; } - val = GETFLOATVALUE(fo); - DECREF(fo); + val = PyFloat_AS_DOUBLE(fo); + Py_DECREF(fo); return val; } @@ -144,9 +144,9 @@ getfloatvalue(op) /* Methods */ void -float_buf_repr(buf, v) +PyFloat_AsString(buf, v) char *buf; - floatobject *v; + PyFloatObject *v; { register char *cp; /* Subroutine for float_repr and float_print. @@ -174,28 +174,28 @@ float_buf_repr(buf, v) /* ARGSUSED */ static int float_print(v, fp, flags) - floatobject *v; + PyFloatObject *v; FILE *fp; int flags; /* Not used but required by interface */ { char buf[100]; - float_buf_repr(buf, v); + PyFloat_AsString(buf, v); fputs(buf, fp); return 0; } -static object * +static PyObject * float_repr(v) - floatobject *v; + PyFloatObject *v; { char buf[100]; - float_buf_repr(buf, v); - return newstringobject(buf); + PyFloat_AsString(buf, v); + return PyString_FromString(buf); } static int float_compare(v, w) - floatobject *v, *w; + PyFloatObject *v, *w; { double i = v->ob_fval; double j = w->ob_fval; @@ -204,7 +204,7 @@ float_compare(v, w) static long float_hash(v) - floatobject *v; + PyFloatObject *v; { double intpart, fractpart; int expo; @@ -226,11 +226,11 @@ float_hash(v) if (fractpart == 0.0) { if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { /* Convert to long int and use its hash... */ - object *w = dnewlongobject(v->ob_fval); + PyObject *w = PyLong_FromDouble(v->ob_fval); if (w == NULL) return -1; - x = hashobject(w); - DECREF(w); + x = PyObject_Hash(w); + Py_DECREF(w); return x; } x = (long)intpart; @@ -252,69 +252,69 @@ float_hash(v) return x; } -static object * +static PyObject * float_add(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double result; PyFPE_START_PROTECT("add", return 0) result = v->ob_fval + w->ob_fval; PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } -static object * +static PyObject * float_sub(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double result; PyFPE_START_PROTECT("subtract", return 0) result = v->ob_fval - w->ob_fval; PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } -static object * +static PyObject * float_mul(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double result; PyFPE_START_PROTECT("multiply", return 0) result = v->ob_fval * w->ob_fval; PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } -static object * +static PyObject * float_div(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double result; if (w->ob_fval == 0) { - err_setstr(ZeroDivisionError, "float division"); + PyErr_SetString(PyExc_ZeroDivisionError, "float division"); return NULL; } PyFPE_START_PROTECT("divide", return 0) result = v->ob_fval / w->ob_fval; PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } -static object * +static PyObject * float_rem(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double vx, wx; double /* div, */ mod; wx = w->ob_fval; if (wx == 0.0) { - err_setstr(ZeroDivisionError, "float modulo"); + PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); return NULL; } PyFPE_START_PROTECT("modulo", return 0) @@ -326,19 +326,19 @@ float_rem(v, w) /* div -= 1.0; */ } PyFPE_END_PROTECT(mod) - return newfloatobject(mod); + return PyFloat_FromDouble(mod); } -static object * +static PyObject * float_divmod(v, w) - floatobject *v; - floatobject *w; + PyFloatObject *v; + PyFloatObject *w; { double vx, wx; double div, mod; wx = w->ob_fval; if (wx == 0.0) { - err_setstr(ZeroDivisionError, "float divmod()"); + PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); return NULL; } PyFPE_START_PROTECT("divmod", return 0) @@ -350,7 +350,7 @@ float_divmod(v, w) div -= 1.0; } PyFPE_END_PROTECT(div) - return mkvalue("(dd)", div, mod); + return Py_BuildValue("(dd)", div, mod); } static double powu(x, n) @@ -369,11 +369,11 @@ static double powu(x, n) return r; } -static object * +static PyObject * float_pow(v, w, z) - floatobject *v; - object *w; - floatobject *z; + PyFloatObject *v; + PyObject *w; + PyFloatObject *z; { double iv, iw, ix; long intw; @@ -383,19 +383,19 @@ float_pow(v, w, z) * [AMK] */ iv = v->ob_fval; - iw = ((floatobject *)w)->ob_fval; + iw = ((PyFloatObject *)w)->ob_fval; intw = (long)iw; if (iw == intw && -10000 < intw && intw < 10000) { /* Sort out special cases here instead of relying on pow() */ if (intw == 0) { /* x**0 is 1, even 0**0 */ PyFPE_START_PROTECT("pow", return 0) - if ((object *)z!=None) { + if ((PyObject *)z!=Py_None) { ix=fmod(1.0, z->ob_fval); if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval; } else ix=1.0; PyFPE_END_PROTECT(ix) - return newfloatobject(ix); + return PyFloat_FromDouble(ix); } errno = 0; PyFPE_START_PROTECT("pow", return 0) @@ -409,14 +409,14 @@ float_pow(v, w, z) /* Sort out special cases here instead of relying on pow() */ if (iv == 0.0) { if (iw < 0.0) { - err_setstr(ValueError, + PyErr_SetString(PyExc_ValueError, "0.0 to a negative power"); return NULL; } - return newfloatobject(0.0); + return PyFloat_FromDouble(0.0); } if (iv < 0.0) { - err_setstr(ValueError, + PyErr_SetString(PyExc_ValueError, "negative number to a float power"); return NULL; } @@ -428,10 +428,10 @@ float_pow(v, w, z) CHECK(ix); if (errno != 0) { /* XXX could it be another type of error? */ - err_errno(OverflowError); + PyErr_SetFromErrno(PyExc_OverflowError); return NULL; } - if ((object *)z!=None) { + if ((PyObject *)z!=Py_None) { PyFPE_START_PROTECT("pow", return 0) ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */ if ( ix!=0 && @@ -440,27 +440,27 @@ float_pow(v, w, z) } PyFPE_END_PROTECT(ix) } - return newfloatobject(ix); + return PyFloat_FromDouble(ix); } -static object * +static PyObject * float_neg(v) - floatobject *v; + PyFloatObject *v; { - return newfloatobject(-v->ob_fval); + return PyFloat_FromDouble(-v->ob_fval); } -static object * +static PyObject * float_pos(v) - floatobject *v; + PyFloatObject *v; { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } -static object * +static PyObject * float_abs(v) - floatobject *v; + PyFloatObject *v; { if (v->ob_fval < 0) return float_neg(v); @@ -470,61 +470,62 @@ float_abs(v) static int float_nonzero(v) - floatobject *v; + PyFloatObject *v; { return v->ob_fval != 0.0; } static int float_coerce(pv, pw) - object **pv; - object **pw; + PyObject **pv; + PyObject **pw; { - if (is_intobject(*pw)) { - long x = getintvalue(*pw); - *pw = newfloatobject((double)x); - INCREF(*pv); + if (PyInt_Check(*pw)) { + long x = PyInt_AsLong(*pw); + *pw = PyFloat_FromDouble((double)x); + Py_INCREF(*pv); return 0; } - else if (is_longobject(*pw)) { - *pw = newfloatobject(dgetlongvalue(*pw)); - INCREF(*pv); + else if (PyLong_Check(*pw)) { + *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); + Py_INCREF(*pv); return 0; } return 1; /* Can't do it */ } -static object * +static PyObject * float_int(v) - object *v; + PyObject *v; { - double x = getfloatvalue(v); + double x = PyFloat_AsDouble(v); if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN : (x = floor(x)) > (double)LONG_MAX) { - err_setstr(OverflowError, "float too large to convert"); + PyErr_SetString(PyExc_OverflowError, + "float too large to convert"); return NULL; } - return newintobject((long)x); + return PyInt_FromLong((long)x); } -static object * +static PyObject * float_long(v) - object *v; + PyObject *v; { - double x = getfloatvalue(v); - return dnewlongobject(x); + double x = PyFloat_AsDouble(v); + return PyLong_FromDouble(x); } -static object * +static PyObject * float_float(v) - object *v; + PyObject *v; { - INCREF(v); + Py_INCREF(v); return v; } -static number_methods float_as_number = { +static PyNumberMethods float_as_number = { (binaryfunc)float_add, /*nb_add*/ (binaryfunc)float_sub, /*nb_subtract*/ (binaryfunc)float_mul, /*nb_multiply*/ @@ -550,11 +551,11 @@ static number_methods float_as_number = { 0, /*nb_hex*/ }; -typeobject Floattype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyFloat_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "float", - sizeof(floatobject), + sizeof(PyFloatObject), 0, (destructor)float_dealloc, /*tp_dealloc*/ (printfunc)float_print, /*tp_print*/ diff --git a/Objects/funcobject.c b/Objects/funcobject.c index c67d6d3..53c926f 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -31,98 +31,99 @@ PERFORMANCE OF THIS SOFTWARE. /* Function object implementation */ -#include "allobjects.h" +#include "Python.h" #include "compile.h" #include "structmember.h" -object * -newfuncobject(code, globals) - object *code; - object *globals; +PyObject * +PyFunction_New(code, globals) + PyObject *code; + PyObject *globals; { - funcobject *op = NEWOBJ(funcobject, &Functype); + PyFunctionObject *op = PyObject_NEW(PyFunctionObject, + &PyFunction_Type); if (op != NULL) { - object *doc; - object *consts; - INCREF(code); + PyObject *doc; + PyObject *consts; + Py_INCREF(code); op->func_code = code; - INCREF(globals); + Py_INCREF(globals); op->func_globals = globals; - op->func_name = ((codeobject *)code)->co_name; - INCREF(op->func_name); + op->func_name = ((PyCodeObject *)code)->co_name; + Py_INCREF(op->func_name); op->func_defaults = NULL; /* No default arguments */ - consts = ((codeobject *)code)->co_consts; - if (gettuplesize(consts) >= 1) { - doc = gettupleitem(consts, 0); - if (!is_stringobject(doc)) - doc = None; + consts = ((PyCodeObject *)code)->co_consts; + if (PyTuple_Size(consts) >= 1) { + doc = PyTuple_GetItem(consts, 0); + if (!PyString_Check(doc)) + doc = Py_None; } else - doc = None; - INCREF(doc); + doc = Py_None; + Py_INCREF(doc); op->func_doc = doc; } - return (object *)op; + return (PyObject *)op; } -object * -getfunccode(op) - object *op; +PyObject * +PyFunction_GetCode(op) + PyObject *op; { - if (!is_funcobject(op)) { - err_badcall(); + if (!PyFunction_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((funcobject *) op) -> func_code; + return ((PyFunctionObject *) op) -> func_code; } -object * -getfuncglobals(op) - object *op; +PyObject * +PyFunction_GetGlobals(op) + PyObject *op; { - if (!is_funcobject(op)) { - err_badcall(); + if (!PyFunction_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((funcobject *) op) -> func_globals; + return ((PyFunctionObject *) op) -> func_globals; } -object * +PyObject * PyFunction_GetDefaults(op) - object *op; + PyObject *op; { - if (!is_funcobject(op)) { - err_badcall(); + if (!PyFunction_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((funcobject *) op) -> func_defaults; + return ((PyFunctionObject *) op) -> func_defaults; } int PyFunction_SetDefaults(op, defaults) - object *op; - object *defaults; + PyObject *op; + PyObject *defaults; { - if (!is_funcobject(op)) { - err_badcall(); + if (!PyFunction_Check(op)) { + PyErr_BadInternalCall(); return -1; } - if (defaults == None) + if (defaults == Py_None) defaults = NULL; - else if (is_tupleobject(defaults)) - XINCREF(defaults); + else if (PyTuple_Check(defaults)) + Py_XINCREF(defaults); else { - err_setstr(SystemError, "non-tuple default args"); + PyErr_SetString(PyExc_SystemError, "non-tuple default args"); return -1; } - XDECREF(((funcobject *) op) -> func_defaults); - ((funcobject *) op) -> func_defaults = defaults; + Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); + ((PyFunctionObject *) op) -> func_defaults = defaults; return 0; } /* Methods */ -#define OFF(x) offsetof(funcobject, x) +#define OFF(x) offsetof(PyFunctionObject, x) static struct memberlist func_memberlist[] = { {"func_code", T_OBJECT, OFF(func_code), READONLY}, @@ -135,75 +136,75 @@ static struct memberlist func_memberlist[] = { {NULL} /* Sentinel */ }; -static object * +static PyObject * func_getattr(op, name) - funcobject *op; + PyFunctionObject *op; char *name; { - if (name[0] != '_' && getrestricted()) { - err_setstr(RuntimeError, + if (name[0] != '_' && PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, "function attributes not accessible in restricted mode"); return NULL; } - return getmember((char *)op, func_memberlist, name); + return PyMember_Get((char *)op, func_memberlist, name); } static void func_dealloc(op) - funcobject *op; + PyFunctionObject *op; { - DECREF(op->func_code); - DECREF(op->func_globals); - DECREF(op->func_name); - XDECREF(op->func_defaults); - XDECREF(op->func_doc); - DEL(op); + Py_DECREF(op->func_code); + Py_DECREF(op->func_globals); + Py_DECREF(op->func_name); + Py_XDECREF(op->func_defaults); + Py_XDECREF(op->func_doc); + PyMem_DEL(op); } -static object* +static PyObject* func_repr(op) - funcobject *op; + PyFunctionObject *op; { char buf[140]; - if (op->func_name == None) + if (op->func_name == Py_None) sprintf(buf, "<anonymous function at %lx>", (long)op); else sprintf(buf, "<function %.100s at %lx>", - getstringvalue(op->func_name), + PyString_AsString(op->func_name), (long)op); - return newstringobject(buf); + return PyString_FromString(buf); } static int func_compare(f, g) - funcobject *f, *g; + PyFunctionObject *f, *g; { int c; if (f->func_globals != g->func_globals) return (f->func_globals < g->func_globals) ? -1 : 1; - c = cmpobject(f->func_defaults, g->func_defaults); + c = PyObject_Compare(f->func_defaults, g->func_defaults); if (c != 0) return c; - return cmpobject(f->func_code, g->func_code); + return PyObject_Compare(f->func_code, g->func_code); } static long func_hash(f) - funcobject *f; + PyFunctionObject *f; { long h; - h = hashobject(f->func_code); + h = PyObject_Hash(f->func_code); if (h == -1) return h; h = h ^ (long)f->func_globals; if (h == -1) h = -2; return h; } -typeobject Functype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyFunction_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "function", - sizeof(funcobject), + sizeof(PyFunctionObject), 0, (destructor)func_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ diff --git a/Objects/intobject.c b/Objects/intobject.c index 1f1298f..4db2a69 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. /* Integer object implementation */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" #ifdef HAVE_LIMITS_H #include <limits.h> @@ -55,28 +54,28 @@ PERFORMANCE OF THIS SOFTWARE. #endif long -getmaxint() +PyInt_GetMax() { return LONG_MAX; /* To initialize sys.maxint */ } /* Standard Booleans */ -intobject FalseObject = { - OB_HEAD_INIT(&Inttype) +PyIntObject _Py_ZeroStruct = { + PyObject_HEAD_INIT(&PyInt_Type) 0 }; -intobject TrueObject = { - OB_HEAD_INIT(&Inttype) +PyIntObject _Py_TrueStruct = { + PyObject_HEAD_INIT(&PyInt_Type) 1 }; -static object * +static PyObject * err_ovf(msg) char *msg; { - err_setstr(OverflowError, msg); + PyErr_SetString(PyExc_OverflowError, msg); return NULL; } @@ -91,23 +90,23 @@ err_ovf(msg) */ #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ -#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject)) +#define N_INTOBJECTS (BLOCK_SIZE / sizeof(PyIntObject)) -static intobject * +static PyIntObject * fill_free_list() { - intobject *p, *q; - p = NEW(intobject, N_INTOBJECTS); + PyIntObject *p, *q; + p = PyMem_NEW(PyIntObject, N_INTOBJECTS); if (p == NULL) - return (intobject *)err_nomem(); + return (PyIntObject *)PyErr_NoMemory(); q = p + N_INTOBJECTS; while (--q > p) - *(intobject **)q = q-1; - *(intobject **)q = NULL; + *(PyIntObject **)q = q-1; + *(PyIntObject **)q = NULL; return p + N_INTOBJECTS - 1; } -static intobject *free_list = NULL; +static PyIntObject *free_list = NULL; #ifndef NSMALLPOSINTS #define NSMALLPOSINTS 100 #endif @@ -120,28 +119,28 @@ static intobject *free_list = NULL; The integers that are saved are those in the range -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). */ -static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; +static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; #endif #ifdef COUNT_ALLOCS int quick_int_allocs, quick_neg_int_allocs; #endif -object * -newintobject(ival) +PyObject * +PyInt_FromLong(ival) long ival; { - register intobject *v; + register PyIntObject *v; #if NSMALLNEGINTS + NSMALLPOSINTS > 0 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { - INCREF(v); + Py_INCREF(v); #ifdef COUNT_ALLOCS if (ival >= 0) quick_int_allocs++; else quick_neg_int_allocs++; #endif - return (object *) v; + return (PyObject *) v; } #endif if (free_list == NULL) { @@ -149,55 +148,56 @@ newintobject(ival) return NULL; } v = free_list; - free_list = *(intobject **)free_list; - v->ob_type = &Inttype; + free_list = *(PyIntObject **)free_list; + v->ob_type = &PyInt_Type; v->ob_ival = ival; - NEWREF(v); + _Py_NewReference(v); #if NSMALLNEGINTS + NSMALLPOSINTS > 0 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { /* save this one for a following allocation */ - INCREF(v); + Py_INCREF(v); small_ints[ival + NSMALLNEGINTS] = v; } #endif - return (object *) v; + return (PyObject *) v; } static void int_dealloc(v) - intobject *v; + PyIntObject *v; { - *(intobject **)v = free_list; + *(PyIntObject **)v = free_list; free_list = v; } long -getintvalue(op) - register object *op; +PyInt_AsLong(op) + register PyObject *op; { - number_methods *nb; - intobject *io; + PyNumberMethods *nb; + PyIntObject *io; long val; - if (op && is_intobject(op)) - return GETINTVALUE((intobject*) op); + if (op && PyInt_Check(op)) + return PyInt_AS_LONG((PyIntObject*) op); if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || nb->nb_int == NULL) { - err_badarg(); + PyErr_BadArgument(); return -1; } - io = (intobject*) (*nb->nb_int) (op); + io = (PyIntObject*) (*nb->nb_int) (op); if (io == NULL) return -1; - if (!is_intobject(io)) { - err_setstr(TypeError, "nb_int should return int object"); + if (!PyInt_Check(io)) { + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); return -1; } - val = GETINTVALUE(io); - DECREF(io); + val = PyInt_AS_LONG(io); + Py_DECREF(io); return val; } @@ -207,7 +207,7 @@ getintvalue(op) /* ARGSUSED */ static int int_print(v, fp, flags) - intobject *v; + PyIntObject *v; FILE *fp; int flags; /* Not used but required by interface */ { @@ -215,18 +215,18 @@ int_print(v, fp, flags) return 0; } -static object * +static PyObject * int_repr(v) - intobject *v; + PyIntObject *v; { char buf[20]; sprintf(buf, "%ld", v->ob_ival); - return newstringobject(buf); + return PyString_FromString(buf); } static int int_compare(v, w) - intobject *v, *w; + PyIntObject *v, *w; { register long i = v->ob_ival; register long j = w->ob_ival; @@ -235,7 +235,7 @@ int_compare(v, w) static long int_hash(v) - intobject *v; + PyIntObject *v; { /* XXX If this is changed, you also need to change the way Python's long, float and complex types are hashed. */ @@ -245,10 +245,10 @@ int_hash(v) return x; } -static object * +static PyObject * int_add(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b, x; a = v->ob_ival; @@ -256,13 +256,13 @@ int_add(v, w) x = a + b; if ((x^a) < 0 && (x^b) < 0) return err_ovf("integer addition"); - return newintobject(x); + return PyInt_FromLong(x); } -static object * +static PyObject * int_sub(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b, x; a = v->ob_ival; @@ -270,7 +270,7 @@ int_sub(v, w) x = a - b; if ((x^a) < 0 && (x^~b) < 0) return err_ovf("integer subtraction"); - return newintobject(x); + return PyInt_FromLong(x); } /* @@ -302,10 +302,10 @@ guess the above is the preferred solution. */ -static object * +static PyObject * int_mul(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { long a, b, ah, bh, x, y; int s = 1; @@ -321,7 +321,7 @@ int_mul(v, w) x = a*b; if (x < 0) goto bad; - return newintobject(x); + return PyInt_FromLong(x); } /* Arrange that a >= b >= 0 */ @@ -367,7 +367,7 @@ int_mul(v, w) x = a*b; if (x < 0) goto bad; - return newintobject(x*s); + return PyInt_FromLong(x*s); } if (a < b) { @@ -397,7 +397,7 @@ int_mul(v, w) if (x < 0) goto bad; ok: - return newintobject(x * s); + return PyInt_FromLong(x * s); bad: return err_ovf("integer multiplication"); @@ -405,7 +405,7 @@ int_mul(v, w) static int i_divmod(x, y, p_xdivy, p_xmody) - register intobject *x, *y; + register PyIntObject *x, *y; long *p_xdivy, *p_xmody; { long xi = x->ob_ival; @@ -413,7 +413,8 @@ i_divmod(x, y, p_xdivy, p_xmody) long xdivy, xmody; if (yi == 0) { - err_setstr(ZeroDivisionError, "integer division or modulo"); + PyErr_SetString(PyExc_ZeroDivisionError, + "integer division or modulo"); return -1; } if (yi < 0) { @@ -438,57 +439,59 @@ i_divmod(x, y, p_xdivy, p_xmody) return 0; } -static object * +static PyObject * int_div(x, y) - intobject *x; - intobject *y; + PyIntObject *x; + PyIntObject *y; { long d, m; if (i_divmod(x, y, &d, &m) < 0) return NULL; - return newintobject(d); + return PyInt_FromLong(d); } -static object * +static PyObject * int_mod(x, y) - intobject *x; - intobject *y; + PyIntObject *x; + PyIntObject *y; { long d, m; if (i_divmod(x, y, &d, &m) < 0) return NULL; - return newintobject(m); + return PyInt_FromLong(m); } -static object * +static PyObject * int_divmod(x, y) - intobject *x; - intobject *y; + PyIntObject *x; + PyIntObject *y; { long d, m; if (i_divmod(x, y, &d, &m) < 0) return NULL; - return mkvalue("(ll)", d, m); + return Py_BuildValue("(ll)", d, m); } -static object * +static PyObject * int_pow(v, w, z) - intobject *v; - intobject *w; - intobject *z; + PyIntObject *v; + PyIntObject *w; + PyIntObject *z; { #if 1 register long iv, iw, iz=0, ix, temp, prev; iv = v->ob_ival; iw = w->ob_ival; if (iw < 0) { - err_setstr(ValueError, "integer to the negative power"); + PyErr_SetString(PyExc_ValueError, + "integer to the negative power"); return NULL; } - if ((object *)z != None) { + if ((PyObject *)z != Py_None) { iz = z->ob_ival; if (iz == 0) { - err_setstr(ValueError, "pow(x, y, z) with z==0"); + PyErr_SetString(PyExc_ValueError, + "pow(x, y, z) with z==0"); return NULL; } } @@ -524,31 +527,35 @@ int_pow(v, w, z) } } if (iz) { - object *t1, *t2; + PyObject *t1, *t2; long int div, mod; - t1=newintobject(ix); - t2=newintobject(iz); + t1=PyInt_FromLong(ix); + t2=PyInt_FromLong(iz); if (t1==NULL || t2==NULL || - i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) { - XDECREF(t1); - XDECREF(t2); + i_divmod((PyIntObject *)t1, + (PyIntObject *)t2, &div, &mod)<0) + { + Py_XDECREF(t1); + Py_XDECREF(t2); return(NULL); } - DECREF(t1); - DECREF(t2); + Py_DECREF(t1); + Py_DECREF(t2); ix=mod; } - return newintobject(ix); + return PyInt_FromLong(ix); #else register long iv, iw, ix; iv = v->ob_ival; iw = w->ob_ival; if (iw < 0) { - err_setstr(ValueError, "integer to the negative power"); + PyErr_SetString(PyExc_ValueError, + "integer to the negative power"); return NULL; } - if ((object *)z != None) { - err_setstr(TypeError, "pow(int, int, int) not yet supported"); + if ((PyObject *)z != Py_None) { + PyErr_SetString(PyExc_TypeError, + "pow(int, int, int) not yet supported"); return NULL; } ix = 1; @@ -560,33 +567,33 @@ int_pow(v, w, z) if (ix / iv != prev) return err_ovf("integer pow()"); } - return newintobject(ix); + return PyInt_FromLong(ix); #endif } -static object * +static PyObject * int_neg(v) - intobject *v; + PyIntObject *v; { register long a, x; a = v->ob_ival; x = -a; if (a < 0 && x < 0) return err_ovf("integer negation"); - return newintobject(x); + return PyInt_FromLong(x); } -static object * +static PyObject * int_pos(v) - intobject *v; + PyIntObject *v; { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } -static object * +static PyObject * int_abs(v) - intobject *v; + PyIntObject *v; { if (v->ob_ival >= 0) return int_pos(v); @@ -596,56 +603,56 @@ int_abs(v) static int int_nonzero(v) - intobject *v; + PyIntObject *v; { return v->ob_ival != 0; } -static object * +static PyObject * int_invert(v) - intobject *v; + PyIntObject *v; { - return newintobject(~v->ob_ival); + return PyInt_FromLong(~v->ob_ival); } -static object * +static PyObject * int_lshift(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b; a = v->ob_ival; b = w->ob_ival; if (b < 0) { - err_setstr(ValueError, "negative shift count"); + PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } if (a == 0 || b == 0) { - INCREF(v); - return (object *) v; + Py_INCREF(v); + return (PyObject *) v; } if (b >= LONG_BIT) { - return newintobject(0L); + return PyInt_FromLong(0L); } a = (unsigned long)a << b; - return newintobject(a); + return PyInt_FromLong(a); } -static object * +static PyObject * int_rshift(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b; a = v->ob_ival; b = w->ob_ival; if (b < 0) { - err_setstr(ValueError, "negative shift count"); + PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } if (a == 0 || b == 0) { - INCREF(v); - return (object *) v; + Py_INCREF(v); + return (PyObject *) v; } if (b >= LONG_BIT) { if (a < 0) @@ -659,67 +666,67 @@ int_rshift(v, w) else a = (unsigned long)a >> b; } - return newintobject(a); + return PyInt_FromLong(a); } -static object * +static PyObject * int_and(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b; a = v->ob_ival; b = w->ob_ival; - return newintobject(a & b); + return PyInt_FromLong(a & b); } -static object * +static PyObject * int_xor(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b; a = v->ob_ival; b = w->ob_ival; - return newintobject(a ^ b); + return PyInt_FromLong(a ^ b); } -static object * +static PyObject * int_or(v, w) - intobject *v; - intobject *w; + PyIntObject *v; + PyIntObject *w; { register long a, b; a = v->ob_ival; b = w->ob_ival; - return newintobject(a | b); + return PyInt_FromLong(a | b); } -static object * +static PyObject * int_int(v) - intobject *v; + PyIntObject *v; { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } -static object * +static PyObject * int_long(v) - intobject *v; + PyIntObject *v; { - return newlongobject((v -> ob_ival)); + return PyLong_FromLong((v -> ob_ival)); } -static object * +static PyObject * int_float(v) - intobject *v; + PyIntObject *v; { - return newfloatobject((double)(v -> ob_ival)); + return PyFloat_FromDouble((double)(v -> ob_ival)); } -static object * +static PyObject * int_oct(v) - intobject *v; + PyIntObject *v; { char buf[100]; long x = v -> ob_ival; @@ -727,20 +734,20 @@ int_oct(v) strcpy(buf, "0"); else sprintf(buf, "0%lo", x); - return newstringobject(buf); + return PyString_FromString(buf); } -static object * +static PyObject * int_hex(v) - intobject *v; + PyIntObject *v; { char buf[100]; long x = v -> ob_ival; sprintf(buf, "0x%lx", x); - return newstringobject(buf); + return PyString_FromString(buf); } -static number_methods int_as_number = { +static PyNumberMethods int_as_number = { (binaryfunc)int_add, /*nb_add*/ (binaryfunc)int_sub, /*nb_subtract*/ (binaryfunc)int_mul, /*nb_multiply*/ @@ -766,11 +773,11 @@ static number_methods int_as_number = { (unaryfunc)int_hex, /*nb_hex*/ }; -typeobject Inttype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyInt_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "int", - sizeof(intobject), + sizeof(PyIntObject), 0, (destructor)int_dealloc, /*tp_dealloc*/ (printfunc)int_print, /*tp_print*/ diff --git a/Objects/listobject.c b/Objects/listobject.c index e2f6fd8..16d63b8 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -31,16 +31,16 @@ PERFORMANCE OF THIS SOFTWARE. /* List object implementation */ -#include "allobjects.h" -#include "modsupport.h" -#include "ceval.h" +#include "Python.h" + #ifdef STDC_HEADERS #include <stddef.h> #else #include <sys/types.h> /* For size_t */ #endif -#define ROUNDUP(n, block) ((((n)+(block)-1)/(block))*(block)) +#define ROUNDUP(n, PyTryBlock) \ + ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock)) static int roundup(n) @@ -52,119 +52,121 @@ roundup(n) return ROUNDUP(n, 100); } -#define NRESIZE(var, type, nitems) RESIZE(var, type, roundup(nitems)) +#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundup(nitems)) -object * -newlistobject(size) +PyObject * +PyList_New(size) int size; { int i; - listobject *op; + PyListObject *op; size_t nbytes; if (size < 0) { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } - nbytes = size * sizeof(object *); + nbytes = size * sizeof(PyObject *); /* Check for overflow */ - if (nbytes / sizeof(object *) != (size_t)size) { - return err_nomem(); + if (nbytes / sizeof(PyObject *) != (size_t)size) { + return PyErr_NoMemory(); } - op = (listobject *) malloc(sizeof(listobject)); + op = (PyListObject *) malloc(sizeof(PyListObject)); if (op == NULL) { - return err_nomem(); + return PyErr_NoMemory(); } if (size <= 0) { op->ob_item = NULL; } else { - op->ob_item = (object **) malloc(nbytes); + op->ob_item = (PyObject **) malloc(nbytes); if (op->ob_item == NULL) { free((ANY *)op); - return err_nomem(); + return PyErr_NoMemory(); } } - op->ob_type = &Listtype; + op->ob_type = &PyList_Type; op->ob_size = size; for (i = 0; i < size; i++) op->ob_item[i] = NULL; - NEWREF(op); - return (object *) op; + _Py_NewReference(op); + return (PyObject *) op; } int -getlistsize(op) - object *op; +PyList_Size(op) + PyObject *op; { - if (!is_listobject(op)) { - err_badcall(); + if (!PyList_Check(op)) { + PyErr_BadInternalCall(); return -1; } else - return ((listobject *)op) -> ob_size; + return ((PyListObject *)op) -> ob_size; } -static object *indexerr; +static PyObject *indexerr; -object * -getlistitem(op, i) - object *op; +PyObject * +PyList_GetItem(op, i) + PyObject *op; int i; { - if (!is_listobject(op)) { - err_badcall(); + if (!PyList_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - if (i < 0 || i >= ((listobject *)op) -> ob_size) { + if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { if (indexerr == NULL) - indexerr = newstringobject("list index out of range"); - err_setval(IndexError, indexerr); + indexerr = PyString_FromString( + "list index out of range"); + PyErr_SetObject(PyExc_IndexError, indexerr); return NULL; } - return ((listobject *)op) -> ob_item[i]; + return ((PyListObject *)op) -> ob_item[i]; } int -setlistitem(op, i, newitem) - register object *op; +PyList_SetItem(op, i, newitem) + register PyObject *op; register int i; - register object *newitem; + register PyObject *newitem; { - register object *olditem; - register object **p; - if (!is_listobject(op)) { - XDECREF(newitem); - err_badcall(); + register PyObject *olditem; + register PyObject **p; + if (!PyList_Check(op)) { + Py_XDECREF(newitem); + PyErr_BadInternalCall(); return -1; } - if (i < 0 || i >= ((listobject *)op) -> ob_size) { - XDECREF(newitem); - err_setstr(IndexError, "list assignment index out of range"); + if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { + Py_XDECREF(newitem); + PyErr_SetString(PyExc_IndexError, + "list assignment index out of range"); return -1; } - p = ((listobject *)op) -> ob_item + i; + p = ((PyListObject *)op) -> ob_item + i; olditem = *p; *p = newitem; - XDECREF(olditem); + Py_XDECREF(olditem); return 0; } static int ins1(self, where, v) - listobject *self; + PyListObject *self; int where; - object *v; + PyObject *v; { int i; - object **items; + PyObject **items; if (v == NULL) { - err_badcall(); + PyErr_BadInternalCall(); return -1; } items = self->ob_item; - NRESIZE(items, object *, self->ob_size+1); + NRESIZE(items, PyObject *, self->ob_size+1); if (items == NULL) { - err_nomem(); + PyErr_NoMemory(); return -1; } if (where < 0) @@ -173,7 +175,7 @@ ins1(self, where, v) where = self->ob_size; for (i = self->ob_size; --i >= where; ) items[i+1] = items[i]; - INCREF(v); + Py_INCREF(v); items[where] = v; self->ob_item = items; self->ob_size++; @@ -181,41 +183,41 @@ ins1(self, where, v) } int -inslistitem(op, where, newitem) - object *op; +PyList_Insert(op, where, newitem) + PyObject *op; int where; - object *newitem; + PyObject *newitem; { - if (!is_listobject(op)) { - err_badcall(); + if (!PyList_Check(op)) { + PyErr_BadInternalCall(); return -1; } - return ins1((listobject *)op, where, newitem); + return ins1((PyListObject *)op, where, newitem); } int -addlistitem(op, newitem) - object *op; - object *newitem; +PyList_Append(op, newitem) + PyObject *op; + PyObject *newitem; { - if (!is_listobject(op)) { - err_badcall(); + if (!PyList_Check(op)) { + PyErr_BadInternalCall(); return -1; } - return ins1((listobject *)op, - (int) ((listobject *)op)->ob_size, newitem); + return ins1((PyListObject *)op, + (int) ((PyListObject *)op)->ob_size, newitem); } /* Methods */ static void list_dealloc(op) - listobject *op; + PyListObject *op; { int i; if (op->ob_item != NULL) { for (i = 0; i < op->ob_size; i++) { - XDECREF(op->ob_item[i]); + Py_XDECREF(op->ob_item[i]); } free((ANY *)op->ob_item); } @@ -224,7 +226,7 @@ list_dealloc(op) static int list_print(op, fp, flags) - listobject *op; + PyListObject *op; FILE *fp; int flags; { @@ -233,39 +235,39 @@ list_print(op, fp, flags) for (i = 0; i < op->ob_size; i++) { if (i > 0) fprintf(fp, ", "); - if (printobject(op->ob_item[i], fp, 0) != 0) + if (PyObject_Print(op->ob_item[i], fp, 0) != 0) return -1; } fprintf(fp, "]"); return 0; } -static object * +static PyObject * list_repr(v) - listobject *v; + PyListObject *v; { - object *s, *comma; + PyObject *s, *comma; int i; - s = newstringobject("["); - comma = newstringobject(", "); + s = PyString_FromString("["); + comma = PyString_FromString(", "); for (i = 0; i < v->ob_size && s != NULL; i++) { if (i > 0) - joinstring(&s, comma); - joinstring_decref(&s, reprobject(v->ob_item[i])); + PyString_Concat(&s, comma); + PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); } - XDECREF(comma); - joinstring_decref(&s, newstringobject("]")); + Py_XDECREF(comma); + PyString_ConcatAndDel(&s, PyString_FromString("]")); return s; } static int list_compare(v, w) - listobject *v, *w; + PyListObject *v, *w; { int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; int i; for (i = 0; i < len; i++) { - int cmp = cmpobject(v->ob_item[i], w->ob_item[i]); + int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]); if (cmp != 0) return cmp; } @@ -274,32 +276,33 @@ list_compare(v, w) static int list_length(a) - listobject *a; + PyListObject *a; { return a->ob_size; } -static object * +static PyObject * list_item(a, i) - listobject *a; + PyListObject *a; int i; { if (i < 0 || i >= a->ob_size) { if (indexerr == NULL) - indexerr = newstringobject("list index out of range"); - err_setval(IndexError, indexerr); + indexerr = PyString_FromString( + "list index out of range"); + PyErr_SetObject(PyExc_IndexError, indexerr); return NULL; } - INCREF(a->ob_item[i]); + Py_INCREF(a->ob_item[i]); return a->ob_item[i]; } -static object * +static PyObject * list_slice(a, ilow, ihigh) - listobject *a; + PyListObject *a; int ilow, ihigh; { - listobject *np; + PyListObject *np; int i; if (ilow < 0) ilow = 0; @@ -311,92 +314,92 @@ list_slice(a, ilow, ihigh) ihigh = ilow; else if (ihigh > a->ob_size) ihigh = a->ob_size; - np = (listobject *) newlistobject(ihigh - ilow); + np = (PyListObject *) PyList_New(ihigh - ilow); if (np == NULL) return NULL; for (i = ilow; i < ihigh; i++) { - object *v = a->ob_item[i]; - INCREF(v); + PyObject *v = a->ob_item[i]; + Py_INCREF(v); np->ob_item[i - ilow] = v; } - return (object *)np; + return (PyObject *)np; } -object * -getlistslice(a, ilow, ihigh) - object *a; +PyObject * +PyList_GetSlice(a, ilow, ihigh) + PyObject *a; int ilow, ihigh; { - if (!is_listobject(a)) { - err_badcall(); + if (!PyList_Check(a)) { + PyErr_BadInternalCall(); return NULL; } - return list_slice((listobject *)a, ilow, ihigh); + return list_slice((PyListObject *)a, ilow, ihigh); } -static object * +static PyObject * list_concat(a, bb) - listobject *a; - object *bb; + PyListObject *a; + PyObject *bb; { int size; int i; - listobject *np; - if (!is_listobject(bb)) { - err_badarg(); + PyListObject *np; + if (!PyList_Check(bb)) { + PyErr_BadArgument(); return NULL; } -#define b ((listobject *)bb) +#define b ((PyListObject *)bb) size = a->ob_size + b->ob_size; - np = (listobject *) newlistobject(size); + np = (PyListObject *) PyList_New(size); if (np == NULL) { return NULL; } for (i = 0; i < a->ob_size; i++) { - object *v = a->ob_item[i]; - INCREF(v); + PyObject *v = a->ob_item[i]; + Py_INCREF(v); np->ob_item[i] = v; } for (i = 0; i < b->ob_size; i++) { - object *v = b->ob_item[i]; - INCREF(v); + PyObject *v = b->ob_item[i]; + Py_INCREF(v); np->ob_item[i + a->ob_size] = v; } - return (object *)np; + return (PyObject *)np; #undef b } -static object * +static PyObject * list_repeat(a, n) - listobject *a; + PyListObject *a; int n; { int i, j; int size; - listobject *np; - object **p; + PyListObject *np; + PyObject **p; if (n < 0) n = 0; size = a->ob_size * n; - np = (listobject *) newlistobject(size); + np = (PyListObject *) PyList_New(size); if (np == NULL) return NULL; p = np->ob_item; for (i = 0; i < n; i++) { for (j = 0; j < a->ob_size; j++) { *p = a->ob_item[j]; - INCREF(*p); + Py_INCREF(*p); p++; } } - return (object *) np; + return (PyObject *) np; } static int list_ass_slice(a, ilow, ihigh, v) - listobject *a; + PyListObject *a; int ilow, ihigh; - object *v; + PyObject *v; { /* Because [X]DECREF can recursively invoke list operations on this list, we must postpone all [X]DECREF activity until @@ -404,27 +407,27 @@ list_ass_slice(a, ilow, ihigh, v) we must allocate an additional array, 'recycle', into which we temporarily copy the items that are deleted from the list. :-( */ - object **recycle, **p; - object **item; + PyObject **recycle, **p; + PyObject **item; int n; /* Size of replacement list */ int d; /* Change in size */ int k; /* Loop index */ -#define b ((listobject *)v) +#define b ((PyListObject *)v) if (v == NULL) n = 0; - else if (is_listobject(v)) { + else if (PyList_Check(v)) { n = b->ob_size; if (a == b) { /* Special case "a[i:j] = a" -- copy b first */ int ret; v = list_slice(b, 0, n); ret = list_ass_slice(a, ilow, ihigh, v); - DECREF(v); + Py_DECREF(v); return ret; } } else { - err_badarg(); + PyErr_BadArgument(); return -1; } if (ilow < 0) @@ -440,7 +443,7 @@ list_ass_slice(a, ilow, ihigh, v) item = a->ob_item; d = n - (ihigh-ilow); if (ihigh > ilow) - p = recycle = NEW(object *, (ihigh-ilow)); + p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); else p = recycle = NULL; if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ @@ -450,15 +453,15 @@ list_ass_slice(a, ilow, ihigh, v) for (/*k = ihigh*/; k < a->ob_size; k++) item[k+d] = item[k]; a->ob_size += d; - NRESIZE(item, object *, a->ob_size); /* Can't fail */ + NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ a->ob_item = item; } } else { /* Insert d items; recycle ihigh-ilow items */ - NRESIZE(item, object *, a->ob_size + d); + NRESIZE(item, PyObject *, a->ob_size + d); if (item == NULL) { - XDEL(recycle); - err_nomem(); + PyMem_XDEL(recycle); + PyErr_NoMemory(); return -1; } for (k = a->ob_size; --k >= ihigh; ) @@ -469,83 +472,84 @@ list_ass_slice(a, ilow, ihigh, v) a->ob_size += d; } for (k = 0; k < n; k++, ilow++) { - object *w = b->ob_item[k]; - XINCREF(w); + PyObject *w = b->ob_item[k]; + Py_XINCREF(w); item[ilow] = w; } if (recycle) { while (--p >= recycle) - XDECREF(*p); - DEL(recycle); + Py_XDECREF(*p); + PyMem_DEL(recycle); } return 0; #undef b } int -setlistslice(a, ilow, ihigh, v) - object *a; +PyList_SetSlice(a, ilow, ihigh, v) + PyObject *a; int ilow, ihigh; - object *v; + PyObject *v; { - if (!is_listobject(a)) { - err_badcall(); + if (!PyList_Check(a)) { + PyErr_BadInternalCall(); return -1; } - return list_ass_slice((listobject *)a, ilow, ihigh, v); + return list_ass_slice((PyListObject *)a, ilow, ihigh, v); } static int list_ass_item(a, i, v) - listobject *a; + PyListObject *a; int i; - object *v; + PyObject *v; { - object *old_value; + PyObject *old_value; if (i < 0 || i >= a->ob_size) { - err_setstr(IndexError, "list assignment index out of range"); + PyErr_SetString(PyExc_IndexError, + "list assignment index out of range"); return -1; } if (v == NULL) return list_ass_slice(a, i, i+1, v); - INCREF(v); + Py_INCREF(v); old_value = a->ob_item[i]; a->ob_item[i] = v; - DECREF(old_value); + Py_DECREF(old_value); return 0; } -static object * +static PyObject * ins(self, where, v) - listobject *self; + PyListObject *self; int where; - object *v; + PyObject *v; { if (ins1(self, where, v) != 0) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * listinsert(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { int i; - object *v; - if (!getargs(args, "(iO)", &i, &v)) + PyObject *v; + if (!PyArg_Parse(args, "(iO)", &i, &v)) return NULL; return ins(self, i, v); } -static object * +static PyObject * listappend(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { - object *v; - if (!getargs(args, "O", &v)) + PyObject *v; + if (!PyArg_Parse(args, "O", &v)) return NULL; return ins(self, (int) self->ob_size, v); } @@ -569,30 +573,31 @@ listappend(self, args) static int docompare(x, y, compare) - object *x; - object *y; - object *compare; + PyObject *x; + PyObject *y; + PyObject *compare; { - object *args, *res; + PyObject *args, *res; int i; if (compare == NULL) - return cmpobject(x, y); + return PyObject_Compare(x, y); - args = mkvalue("(OO)", x, y); + args = Py_BuildValue("(OO)", x, y); if (args == NULL) return CMPERROR; - res = call_object(compare, args); - DECREF(args); + res = PyEval_CallObject(compare, args); + Py_DECREF(args); if (res == NULL) return CMPERROR; - if (!is_intobject(res)) { - DECREF(res); - err_setstr(TypeError, "comparison function should return int"); + if (!PyInt_Check(res)) { + Py_DECREF(res); + PyErr_SetString(PyExc_TypeError, + "comparison function should return int"); return CMPERROR; } - i = getintvalue(res); - DECREF(res); + i = PyInt_AsLong(res); + Py_DECREF(res); if (i < 0) return -1; if (i > 0) @@ -604,17 +609,17 @@ docompare(x, y, compare) static int insertionsort(array, size, compare) - object **array; /* Start of array to sort */ + PyObject **array; /* Start of array to sort */ int size; /* Number of elements to sort */ - object *compare;/* Comparison function object, or NULL for default */ + PyObject *compare;/* Comparison function object, or NULL for default */ { - register object **a = array; - register object **end = array+size; - register object **p; + register PyObject **a = array; + register PyObject **end = array+size; + register PyObject **p; for (p = a+1; p < end; p++) { - register object *key = *p; - register object **q = p; + register PyObject *key = *p; + register PyObject **q = p; while (--q >= a) { register int k = docompare(*q, key, compare); if (k == CMPERROR) @@ -651,15 +656,15 @@ insertionsort(array, size, compare) static int quicksort(array, size, compare) - object **array; /* Start of array to sort */ + PyObject **array; /* Start of array to sort */ int size; /* Number of elements to sort */ - object *compare;/* Comparison function object, or NULL for default */ + PyObject *compare;/* Comparison function object, or NULL for default */ { - register object *tmp, *pivot; - register object **lo, **hi, **l, **r; + register PyObject *tmp, *pivot; + register PyObject **lo, **hi, **l, **r; int top, k, n, n2; - object **lostack[STACKSIZE]; - object **histack[STACKSIZE]; + PyObject **lostack[STACKSIZE]; + PyObject **histack[STACKSIZE]; /* Start out with the whole array on the work stack */ lostack[0] = array; @@ -770,99 +775,100 @@ quicksort(array, size, compare) return 0; } -static object * +static PyObject * listsort(self, compare) - listobject *self; - object *compare; + PyListObject *self; + PyObject *compare; { /* XXX Don't you *dare* changing the list's length in compare()! */ if (quicksort(self->ob_item, self->ob_size, compare) < 0) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #else /* !NEWSORT */ -static object *comparefunc; +static PyObject *comparefunc; static int cmp(v, w) const ANY *v, *w; { - object *t, *res; + PyObject *t, *res; long i; - if (err_occurred()) + if (PyErr_Occurred()) return 0; if (comparefunc == NULL) - return cmpobject(* (object **) v, * (object **) w); + return PyObject_Compare(* (PyObject **) v, * (PyObject **) w); /* Call the user-supplied comparison function */ - t = mkvalue("(OO)", * (object **) v, * (object **) w); + t = Py_BuildValue("(OO)", * (PyObject **) v, * (PyObject **) w); if (t == NULL) return 0; - res = call_object(comparefunc, t); - DECREF(t); + res = PyEval_CallObject(comparefunc, t); + Py_DECREF(t); if (res == NULL) return 0; - if (!is_intobject(res)) { - err_setstr(TypeError, "comparison function should return int"); + if (!PyInt_Check(res)) { + PyErr_SetString(PyExc_TypeError, + "comparison function should return int"); i = 0; } else { - i = getintvalue(res); + i = PyInt_AsLong(res); if (i < 0) i = -1; else if (i > 0) i = 1; } - DECREF(res); + Py_DECREF(res); return (int) i; } -static object * +static PyObject * listsort(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { - object *save_comparefunc; + PyObject *save_comparefunc; if (self->ob_size <= 1) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } save_comparefunc = comparefunc; comparefunc = args; if (comparefunc != NULL) { /* Test the comparison function for obvious errors */ (void) cmp((ANY *)&self->ob_item[0], (ANY *)&self->ob_item[1]); - if (err_occurred()) { + if (PyErr_Occurred()) { comparefunc = save_comparefunc; return NULL; } } qsort((char *)self->ob_item, - (int) self->ob_size, sizeof(object *), cmp); + (int) self->ob_size, sizeof(PyObject *), cmp); comparefunc = save_comparefunc; - if (err_occurred()) + if (PyErr_Occurred()) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #endif -static object * +static PyObject * listreverse(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { - register object **p, **q; - register object *tmp; + register PyObject **p, **q; + register PyObject *tmp; if (args != NULL) { - err_badarg(); + PyErr_BadArgument(); return NULL; } @@ -875,148 +881,149 @@ listreverse(self, args) } } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } int -reverselist(v) - object *v; +PyList_Reverse(v) + PyObject *v; { - if (v == NULL || !is_listobject(v)) { - err_badcall(); + if (v == NULL || !PyList_Check(v)) { + PyErr_BadInternalCall(); return -1; } - v = listreverse((listobject *)v, (object *)NULL); + v = listreverse((PyListObject *)v, (PyObject *)NULL); if (v == NULL) return -1; - DECREF(v); + Py_DECREF(v); return 0; } int -sortlist(v) - object *v; +PyList_Sort(v) + PyObject *v; { - if (v == NULL || !is_listobject(v)) { - err_badcall(); + if (v == NULL || !PyList_Check(v)) { + PyErr_BadInternalCall(); return -1; } - v = listsort((listobject *)v, (object *)NULL); + v = listsort((PyListObject *)v, (PyObject *)NULL); if (v == NULL) return -1; - DECREF(v); + Py_DECREF(v); return 0; } -object * -listtuple(v) - object *v; +PyObject * +PyList_AsTuple(v) + PyObject *v; { - object *w; - object **p; + PyObject *w; + PyObject **p; int n; - if (v == NULL || !is_listobject(v)) { - err_badcall(); + if (v == NULL || !PyList_Check(v)) { + PyErr_BadInternalCall(); return NULL; } - n = ((listobject *)v)->ob_size; - w = newtupleobject(n); + n = ((PyListObject *)v)->ob_size; + w = PyTuple_New(n); if (w == NULL) return NULL; - p = ((tupleobject *)w)->ob_item; + p = ((PyTupleObject *)w)->ob_item; memcpy((ANY *)p, - (ANY *)((listobject *)v)->ob_item, - n*sizeof(object *)); + (ANY *)((PyListObject *)v)->ob_item, + n*sizeof(PyObject *)); while (--n >= 0) { - INCREF(*p); + Py_INCREF(*p); p++; } return w; } -static object * +static PyObject * listindex(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { int i; if (args == NULL) { - err_badarg(); + PyErr_BadArgument(); return NULL; } for (i = 0; i < self->ob_size; i++) { - if (cmpobject(self->ob_item[i], args) == 0) - return newintobject((long)i); + if (PyObject_Compare(self->ob_item[i], args) == 0) + return PyInt_FromLong((long)i); } - err_setstr(ValueError, "list.index(x): x not in list"); + PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); return NULL; } -static object * +static PyObject * listcount(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { int count = 0; int i; if (args == NULL) { - err_badarg(); + PyErr_BadArgument(); return NULL; } for (i = 0; i < self->ob_size; i++) { - if (cmpobject(self->ob_item[i], args) == 0) + if (PyObject_Compare(self->ob_item[i], args) == 0) count++; } - return newintobject((long)count); + return PyInt_FromLong((long)count); } -static object * +static PyObject * listremove(self, args) - listobject *self; - object *args; + PyListObject *self; + PyObject *args; { int i; if (args == NULL) { - err_badarg(); + PyErr_BadArgument(); return NULL; } for (i = 0; i < self->ob_size; i++) { - if (cmpobject(self->ob_item[i], args) == 0) { - if (list_ass_slice(self, i, i+1, (object *)NULL) != 0) + if (PyObject_Compare(self->ob_item[i], args) == 0) { + if (list_ass_slice(self, i, i+1, + (PyObject *)NULL) != 0) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } } - err_setstr(ValueError, "list.remove(x): x not in list"); + PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); return NULL; } -static struct methodlist list_methods[] = { - {"append", (method)listappend}, - {"count", (method)listcount}, - {"index", (method)listindex}, - {"insert", (method)listinsert}, - {"sort", (method)listsort, 0}, - {"remove", (method)listremove}, - {"reverse", (method)listreverse}, +static PyMethodDef list_methods[] = { + {"append", (PyCFunction)listappend}, + {"count", (PyCFunction)listcount}, + {"index", (PyCFunction)listindex}, + {"insert", (PyCFunction)listinsert}, + {"sort", (PyCFunction)listsort, 0}, + {"remove", (PyCFunction)listremove}, + {"reverse", (PyCFunction)listreverse}, {NULL, NULL} /* sentinel */ }; -static object * +static PyObject * list_getattr(f, name) - listobject *f; + PyListObject *f; char *name; { - return findmethod(list_methods, (object *)f, name); + return Py_FindMethod(list_methods, (PyObject *)f, name); } -static sequence_methods list_as_sequence = { +static PySequenceMethods list_as_sequence = { (inquiry)list_length, /*sq_length*/ (binaryfunc)list_concat, /*sq_concat*/ (intargfunc)list_repeat, /*sq_repeat*/ @@ -1026,11 +1033,11 @@ static sequence_methods list_as_sequence = { (intintobjargproc)list_ass_slice, /*sq_ass_slice*/ }; -typeobject Listtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyList_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "list", - sizeof(listobject), + sizeof(PyListObject), 0, (destructor)list_dealloc, /*tp_dealloc*/ (printfunc)list_print, /*tp_print*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index dc84583..d95e86c 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -33,36 +33,37 @@ PERFORMANCE OF THIS SOFTWARE. /* XXX The functional organization of this file is terrible */ -#include "allobjects.h" +#include "Python.h" #include "longintrepr.h" #include "mymath.h" + #include <assert.h> #include <ctype.h> #define ABS(x) ((x) < 0 ? -(x) : (x)) /* Forward */ -static longobject *long_normalize PROTO((longobject *)); -static longobject *mul1 PROTO((longobject *, wdigit)); -static longobject *muladd1 PROTO((longobject *, wdigit, wdigit)); -static longobject *divrem1 PROTO((longobject *, wdigit, digit *)); -static object *long_format PROTO((object *aa, int base)); +static PyLongObject *long_normalize Py_PROTO((PyLongObject *)); +static PyLongObject *mul1 Py_PROTO((PyLongObject *, wdigit)); +static PyLongObject *muladd1 Py_PROTO((PyLongObject *, wdigit, wdigit)); +static PyLongObject *divrem1 Py_PROTO((PyLongObject *, wdigit, digit *)); +static PyObject *long_format Py_PROTO((PyObject *aa, int base)); static int ticker; /* XXX Could be shared with ceval? */ -#define SIGCHECK(block) \ +#define SIGCHECK(PyTryBlock) \ if (--ticker < 0) { \ ticker = 100; \ - if (sigcheck()) { block; } \ + if (PyErr_CheckSignals()) { PyTryBlock; } \ } /* Normalize (remove leading zeros from) a long int object. Doesn't attempt to free the storage--in most cases, due to the nature of the algorithms used, this could save at most be one word anyway. */ -static longobject * +static PyLongObject * long_normalize(v) - register longobject *v; + register PyLongObject *v; { int j = ABS(v->ob_size); register int i = j; @@ -77,22 +78,22 @@ long_normalize(v) /* Allocate a new long int object with size digits. Return NULL and set exception if we run out of memory. */ -longobject * -alloclongobject(size) +PyLongObject * +_PyLong_New(size) int size; { - return NEWVAROBJ(longobject, &Longtype, size); + return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); } /* Create a new long int object from a C long int */ -object * -newlongobject(ival) +PyObject * +PyLong_FromLong(ival) long ival; { /* Assume a C long fits in at most 5 'digits' */ /* Works on both 32- and 64-bit machines */ - longobject *v = alloclongobject(5); + PyLongObject *v = _PyLong_New(5); if (v != NULL) { unsigned long t = ival; int i; @@ -106,18 +107,18 @@ newlongobject(ival) } v = long_normalize(v); } - return (object *)v; + return (PyObject *)v; } /* Create a new long int object from a C unsigned long int */ -object * +PyObject * PyLong_FromUnsignedLong(ival) unsigned long ival; { /* Assume a C long fits in at most 5 'digits' */ /* Works on both 32- and 64-bit machines */ - longobject *v = alloclongobject(5); + PyLongObject *v = _PyLong_New(5); if (v != NULL) { unsigned long t = ival; int i; @@ -127,20 +128,20 @@ PyLong_FromUnsignedLong(ival) } v = long_normalize(v); } - return (object *)v; + return (PyObject *)v; } /* Create a new long int object from a C double */ -object * +PyObject * #ifdef MPW -dnewlongobject(double dval) +PyLong_FromDouble(double dval) #else -dnewlongobject(dval) +PyLong_FromDouble(dval) double dval; #endif /* MPW */ { - longobject *v; + PyLongObject *v; double frac; int i, ndig, expo, neg; neg = 0; @@ -150,9 +151,9 @@ dnewlongobject(dval) } frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ if (expo <= 0) - return newlongobject(0L); + return PyLong_FromLong(0L); ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ - v = alloclongobject(ndig); + v = _PyLong_New(ndig); if (v == NULL) return NULL; frac = ldexp(frac, (expo-1) % SHIFT + 1); @@ -164,25 +165,25 @@ dnewlongobject(dval) } if (neg) v->ob_size = -(v->ob_size); - return (object *)v; + return (PyObject *)v; } /* Get a C long int from a long int object. Returns -1 and sets an error condition if overflow occurs. */ long -getlongvalue(vv) - object *vv; +PyLong_AsLong(vv) + PyObject *vv; { - register longobject *v; + register PyLongObject *v; long x, prev; int i, sign; - if (vv == NULL || !is_longobject(vv)) { - err_badcall(); + if (vv == NULL || !PyLong_Check(vv)) { + PyErr_BadInternalCall(); return -1; } - v = (longobject *)vv; + v = (PyLongObject *)vv; i = v->ob_size; sign = 1; x = 0; @@ -194,7 +195,7 @@ getlongvalue(vv) prev = x; x = (x << SHIFT) + v->ob_digit[i]; if ((x >> SHIFT) != prev) { - err_setstr(OverflowError, + PyErr_SetString(PyExc_OverflowError, "long int too long to convert"); return -1; } @@ -207,21 +208,21 @@ getlongvalue(vv) unsigned long PyLong_AsUnsignedLong(vv) - object *vv; + PyObject *vv; { - register longobject *v; + register PyLongObject *v; unsigned long x, prev; int i; - if (vv == NULL || !is_longobject(vv)) { - err_badcall(); + if (vv == NULL || !PyLong_Check(vv)) { + PyErr_BadInternalCall(); return (unsigned long) -1; } - v = (longobject *)vv; + v = (PyLongObject *)vv; i = v->ob_size; x = 0; if (i < 0) { - err_setstr(OverflowError, + PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned long"); return (unsigned long) -1; } @@ -229,7 +230,7 @@ PyLong_AsUnsignedLong(vv) prev = x; x = (x << SHIFT) + v->ob_digit[i]; if ((x >> SHIFT) != prev) { - err_setstr(OverflowError, + PyErr_SetString(PyExc_OverflowError, "long int too long to convert"); return (unsigned long) -1; } @@ -240,19 +241,19 @@ PyLong_AsUnsignedLong(vv) /* Get a C double from a long int object. */ double -dgetlongvalue(vv) - object *vv; +PyLong_AsDouble(vv) + PyObject *vv; { - register longobject *v; + register PyLongObject *v; double x; double multiplier = (double) (1L << SHIFT); int i, sign; - if (vv == NULL || !is_longobject(vv)) { - err_badcall(); + if (vv == NULL || !PyLong_Check(vv)) { + PyErr_BadInternalCall(); return -1; } - v = (longobject *)vv; + v = (PyLongObject *)vv; i = v->ob_size; sign = 1; x = 0.0; @@ -268,9 +269,9 @@ dgetlongvalue(vv) /* Multiply by a single digit, ignoring the sign. */ -static longobject * +static PyLongObject * mul1(a, n) - longobject *a; + PyLongObject *a; wdigit n; { return muladd1(a, n, (digit)0); @@ -278,14 +279,14 @@ mul1(a, n) /* Multiply by a single digit and add a single digit, ignoring the sign. */ -static longobject * +static PyLongObject * muladd1(a, n, extra) - longobject *a; + PyLongObject *a; wdigit n; wdigit extra; { int size_a = ABS(a->ob_size); - longobject *z = alloclongobject(size_a+1); + PyLongObject *z = _PyLong_New(size_a+1); twodigits carry = extra; int i; @@ -304,19 +305,19 @@ muladd1(a, n, extra) (as function result) and the remainder (through *prem). The sign of a is ignored; n should not be zero. */ -static longobject * +static PyLongObject * divrem1(a, n, prem) - longobject *a; + PyLongObject *a; wdigit n; digit *prem; { int size = ABS(a->ob_size); - longobject *z; + PyLongObject *z; int i; twodigits rem = 0; assert(n > 0 && n <= MASK); - z = alloclongobject(size); + z = _PyLong_New(size); if (z == NULL) return NULL; for (i = size; --i >= 0; ) { @@ -333,21 +334,21 @@ divrem1(a, n, prem) If base is 8 or 16, add the proper prefix '0' or '0x'. External linkage: used in bltinmodule.c by hex() and oct(). */ -static object * +static PyObject * long_format(aa, base) - object *aa; + PyObject *aa; int base; { - register longobject *a = (longobject *)aa; - stringobject *str; + register PyLongObject *a = (PyLongObject *)aa; + PyStringObject *str; int i; int size_a = ABS(a->ob_size); char *p; int bits; char sign = '\0'; - if (a == NULL || !is_longobject(a)) { - err_badcall(); + if (a == NULL || !PyLong_Check(a)) { + PyErr_BadInternalCall(); return NULL; } assert(base >= 2 && base <= 36); @@ -360,39 +361,39 @@ long_format(aa, base) i >>= 1; } i = 6 + (size_a*SHIFT + bits-1) / bits; - str = (stringobject *) newsizedstringobject((char *)0, i); + str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); if (str == NULL) return NULL; - p = GETSTRINGVALUE(str) + i; + p = PyString_AS_STRING(str) + i; *p = '\0'; *--p = 'L'; if (a->ob_size < 0) sign = '-'; - INCREF(a); + Py_INCREF(a); do { digit rem; - longobject *temp = divrem1(a, (digit)base, &rem); + PyLongObject *temp = divrem1(a, (digit)base, &rem); if (temp == NULL) { - DECREF(a); - DECREF(str); + Py_DECREF(a); + Py_DECREF(str); return NULL; } if (rem < 10) rem += '0'; else rem += 'A'-10; - assert(p > GETSTRINGVALUE(str)); + assert(p > PyString_AS_STRING(str)); *--p = (char) rem; - DECREF(a); + Py_DECREF(a); a = temp; SIGCHECK({ - DECREF(a); - DECREF(str); + Py_DECREF(a); + Py_DECREF(str); return NULL; }) } while (ABS(a->ob_size) != 0); - DECREF(a); + Py_DECREF(a); if (base == 8) { if (size_a != 0) *--p = '0'; @@ -409,15 +410,16 @@ long_format(aa, base) } if (sign) *--p = sign; - if (p != GETSTRINGVALUE(str)) { - char *q = GETSTRINGVALUE(str); + if (p != PyString_AS_STRING(str)) { + char *q = PyString_AS_STRING(str); assert(p > q); do { } while ((*q++ = *p++) != '\0'); q--; - resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str))); + _PyString_Resize((PyObject **)&str, + (int) (q - PyString_AS_STRING(str))); } - return (object *)str; + return (PyObject *)str; } #if 0 @@ -425,26 +427,27 @@ long_format(aa, base) Base zero implies a default depending on the number. External linkage: used in compile.c and stropmodule.c. */ -object * +PyObject * long_scan(str, base) char *str; int base; { - return long_escan(str, (char **)NULL, base); + return PyLong_FromString(str, (char **)NULL, base); } #endif -object * -long_escan(str, pend, base) +PyObject * +PyLong_FromString(str, pend, base) char *str; char **pend; int base; { int sign = 1; - longobject *z; + PyLongObject *z; if ((base != 0 && base < 2) || base > 36) { - err_setstr(ValueError, "invalid base for long literal"); + PyErr_SetString(PyExc_ValueError, + "invalid base for long literal"); return NULL; } while (*str != '\0' && isspace(Py_CHARMASK(*str))) @@ -467,10 +470,10 @@ long_escan(str, pend, base) } if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) str += 2; - z = alloclongobject(0); + z = _PyLong_New(0); for ( ; z != NULL; ++str) { int k = -1; - longobject *temp; + PyLongObject *temp; if (*str <= '9') k = *str - '0'; @@ -481,43 +484,44 @@ long_escan(str, pend, base) if (k < 0 || k >= base) break; temp = muladd1(z, (digit)base, (digit)k); - DECREF(z); + Py_DECREF(z); z = temp; } if (sign < 0 && z != NULL && z->ob_size != 0) z->ob_size = -(z->ob_size); if (pend) *pend = str; - return (object *) z; + return (PyObject *) z; } -static longobject *x_divrem PROTO((longobject *, longobject *, longobject **)); -static object *long_pos PROTO((longobject *)); -static long_divrem PROTO((longobject *, longobject *, - longobject **, longobject **)); +static PyLongObject *x_divrem + Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject **)); +static PyObject *long_pos Py_PROTO((PyLongObject *)); +static long_divrem Py_PROTO((PyLongObject *, PyLongObject *, + PyLongObject **, PyLongObject **)); /* Long division with remainder, top-level routine */ static int long_divrem(a, b, pdiv, prem) - longobject *a, *b; - longobject **pdiv; - longobject **prem; + PyLongObject *a, *b; + PyLongObject **pdiv; + PyLongObject **prem; { int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); - longobject *z; + PyLongObject *z; if (size_b == 0) { - err_setstr(ZeroDivisionError, "long division or modulo"); + PyErr_SetString(PyExc_ZeroDivisionError, "long division or modulo"); return -1; } if (size_a < size_b || (size_a == size_b && a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { /* |a| < |b|. */ - *pdiv = alloclongobject(0); - INCREF(a); - *prem = (longobject *) a; + *pdiv = _PyLong_New(0); + Py_INCREF(a); + *prem = (PyLongObject *) a; return 0; } if (size_b == 1) { @@ -525,7 +529,7 @@ long_divrem(a, b, pdiv, prem) z = divrem1(a, b->ob_digit[0], &rem); if (z == NULL) return -1; - *prem = (longobject *) newlongobject((long)rem); + *prem = (PyLongObject *) PyLong_FromLong((long)rem); } else { z = x_divrem(a, b, prem); @@ -546,21 +550,21 @@ long_divrem(a, b, pdiv, prem) /* Unsigned long division with remainder -- the algorithm */ -static longobject * +static PyLongObject * x_divrem(v1, w1, prem) - longobject *v1, *w1; - longobject **prem; + PyLongObject *v1, *w1; + PyLongObject **prem; { int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); - longobject *v = mul1(v1, d); - longobject *w = mul1(w1, d); - longobject *a; + PyLongObject *v = mul1(v1, d); + PyLongObject *w = mul1(w1, d); + PyLongObject *a; int j, k; if (v == NULL || w == NULL) { - XDECREF(v); - XDECREF(w); + Py_XDECREF(v); + Py_XDECREF(w); return NULL; } @@ -569,7 +573,7 @@ x_divrem(v1, w1, prem) assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ size_v = ABS(v->ob_size); - a = alloclongobject(size_v - size_w + 1); + a = _PyLong_New(size_v - size_w + 1); for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; @@ -578,7 +582,7 @@ x_divrem(v1, w1, prem) int i; SIGCHECK({ - DECREF(a); + Py_DECREF(a); a = NULL; break; }) @@ -600,7 +604,8 @@ x_divrem(v1, w1, prem) for (i = 0; i < size_w && i+k < size_v; ++i) { twodigits z = w->ob_digit[i] * q; digit zz = (digit) (z >> SHIFT); - carry += v->ob_digit[i+k] - z + ((twodigits)zz << SHIFT); + carry += v->ob_digit[i+k] - z + + ((twodigits)zz << SHIFT); v->ob_digit[i+k] = carry & MASK; carry = (carry >> SHIFT) - zz; } @@ -631,58 +636,59 @@ x_divrem(v1, w1, prem) *prem = divrem1(v, d, &d); /* d receives the (unused) remainder */ if (*prem == NULL) { - DECREF(a); + Py_DECREF(a); a = NULL; } } - DECREF(v); - DECREF(w); + Py_DECREF(v); + Py_DECREF(w); return a; } /* Methods */ /* Forward */ -static void long_dealloc PROTO((object *)); -static object *long_repr PROTO((object *)); -static int long_compare PROTO((longobject *, longobject *)); -static long long_hash PROTO((longobject *)); - -static object *long_add PROTO((longobject *, longobject *)); -static object *long_sub PROTO((longobject *, longobject *)); -static object *long_mul PROTO((longobject *, longobject *)); -static object *long_div PROTO((longobject *, longobject *)); -static object *long_mod PROTO((longobject *, longobject *)); -static object *long_divmod PROTO((longobject *, longobject *)); -static object *long_pow PROTO((longobject *, longobject *, longobject *)); -static object *long_neg PROTO((longobject *)); -static object *long_pos PROTO((longobject *)); -static object *long_abs PROTO((longobject *)); -static int long_nonzero PROTO((longobject *)); -static object *long_invert PROTO((longobject *)); -static object *long_lshift PROTO((longobject *, longobject *)); -static object *long_rshift PROTO((longobject *, longobject *)); -static object *long_and PROTO((longobject *, longobject *)); -static object *long_xor PROTO((longobject *, longobject *)); -static object *long_or PROTO((longobject *, longobject *)); +static void long_dealloc Py_PROTO((PyObject *)); +static PyObject *long_repr Py_PROTO((PyObject *)); +static int long_compare Py_PROTO((PyLongObject *, PyLongObject *)); +static long long_hash Py_PROTO((PyLongObject *)); + +static PyObject *long_add Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_sub Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_mul Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_div Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_mod Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_divmod Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_pow + Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject *)); +static PyObject *long_neg Py_PROTO((PyLongObject *)); +static PyObject *long_pos Py_PROTO((PyLongObject *)); +static PyObject *long_abs Py_PROTO((PyLongObject *)); +static int long_nonzero Py_PROTO((PyLongObject *)); +static PyObject *long_invert Py_PROTO((PyLongObject *)); +static PyObject *long_lshift Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_rshift Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_and Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_xor Py_PROTO((PyLongObject *, PyLongObject *)); +static PyObject *long_or Py_PROTO((PyLongObject *, PyLongObject *)); static void long_dealloc(v) - object *v; + PyObject *v; { - DEL(v); + PyMem_DEL(v); } -static object * +static PyObject * long_repr(v) - object *v; + PyObject *v; { return long_format(v, 10); } static int long_compare(a, b) - longobject *a, *b; + PyLongObject *a, *b; { int sign; @@ -709,7 +715,7 @@ long_compare(a, b) static long long_hash(v) - longobject *v; + PyLongObject *v; { long x; int i, sign; @@ -738,22 +744,24 @@ long_hash(v) /* Add the absolute values of two long integers. */ -static longobject *x_add PROTO((longobject *, longobject *)); -static longobject * +static PyLongObject *x_add Py_PROTO((PyLongObject *, PyLongObject *)); +static PyLongObject * x_add(a, b) - longobject *a, *b; + PyLongObject *a, *b; { int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); - longobject *z; + PyLongObject *z; int i; digit carry = 0; /* Ensure a is the larger of the two: */ if (size_a < size_b) { - { longobject *temp = a; a = b; b = temp; } - { int size_temp = size_a; size_a = size_b; size_b = size_temp; } + { PyLongObject *temp = a; a = b; b = temp; } + { int size_temp = size_a; + size_a = size_b; + size_b = size_temp; } } - z = alloclongobject(size_a+1); + z = _PyLong_New(size_a+1); if (z == NULL) return NULL; for (i = 0; i < size_b; ++i) { @@ -774,13 +782,13 @@ x_add(a, b) /* Subtract the absolute values of two integers. */ -static longobject *x_sub PROTO((longobject *, longobject *)); -static longobject * +static PyLongObject *x_sub Py_PROTO((PyLongObject *, PyLongObject *)); +static PyLongObject * x_sub(a, b) - longobject *a, *b; + PyLongObject *a, *b; { int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); - longobject *z; + PyLongObject *z; int i; int sign = 1; digit borrow = 0; @@ -788,8 +796,10 @@ x_sub(a, b) /* Ensure a is the larger of the two: */ if (size_a < size_b) { sign = -1; - { longobject *temp = a; a = b; b = temp; } - { int size_temp = size_a; size_a = size_b; size_b = size_temp; } + { PyLongObject *temp = a; a = b; b = temp; } + { int size_temp = size_a; + size_a = size_b; + size_b = size_temp; } } else if (size_a == size_b) { /* Find highest digit where a and b differ: */ @@ -797,14 +807,14 @@ x_sub(a, b) while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) ; if (i < 0) - return alloclongobject(0); + return _PyLong_New(0); if (a->ob_digit[i] < b->ob_digit[i]) { sign = -1; - { longobject *temp = a; a = b; b = temp; } + { PyLongObject *temp = a; a = b; b = temp; } } size_a = size_b = i+1; } - z = alloclongobject(size_a); + z = _PyLong_New(size_a); if (z == NULL) return NULL; for (i = 0; i < size_b; ++i) { @@ -826,12 +836,12 @@ x_sub(a, b) return long_normalize(z); } -static object * +static PyObject * long_add(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { - longobject *z; + PyLongObject *z; if (a->ob_size < 0) { if (b->ob_size < 0) { @@ -848,15 +858,15 @@ long_add(a, b) else z = x_add(a, b); } - return (object *)z; + return (PyObject *)z; } -static object * +static PyObject * long_sub(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { - longobject *z; + PyLongObject *z; if (a->ob_size < 0) { if (b->ob_size < 0) @@ -872,22 +882,22 @@ long_sub(a, b) else z = x_sub(a, b); } - return (object *)z; + return (PyObject *)z; } -static object * +static PyObject * long_mul(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { int size_a; int size_b; - longobject *z; + PyLongObject *z; int i; size_a = ABS(a->ob_size); size_b = ABS(b->ob_size); - z = alloclongobject(size_a + size_b); + z = _PyLong_New(size_a + size_b); if (z == NULL) return NULL; for (i = 0; i < z->ob_size; ++i) @@ -898,7 +908,7 @@ long_mul(a, b) int j; SIGCHECK({ - DECREF(z); + Py_DECREF(z); return NULL; }) for (j = 0; j < size_b; ++j) { @@ -917,7 +927,7 @@ long_mul(a, b) z->ob_size = -(z->ob_size); if (b->ob_size < 0) z->ob_size = -(z->ob_size); - return (object *) long_normalize(z); + return (PyObject *) long_normalize(z); } /* The / and % operators are now defined in terms of divmod(). @@ -935,40 +945,40 @@ long_mul(a, b) have different signs. We then subtract one from the 'div' part of the outcome to keep the invariant intact. */ -static int l_divmod PROTO((longobject *, longobject *, - longobject **, longobject **)); +static int l_divmod Py_PROTO((PyLongObject *, PyLongObject *, + PyLongObject **, PyLongObject **)); static int l_divmod(v, w, pdiv, pmod) - longobject *v; - longobject *w; - longobject **pdiv; - longobject **pmod; + PyLongObject *v; + PyLongObject *w; + PyLongObject **pdiv; + PyLongObject **pmod; { - longobject *div, *mod; + PyLongObject *div, *mod; if (long_divrem(v, w, &div, &mod) < 0) return -1; if ((mod->ob_size < 0 && w->ob_size > 0) || (mod->ob_size > 0 && w->ob_size < 0)) { - longobject *temp; - longobject *one; - temp = (longobject *) long_add(mod, w); - DECREF(mod); + PyLongObject *temp; + PyLongObject *one; + temp = (PyLongObject *) long_add(mod, w); + Py_DECREF(mod); mod = temp; if (mod == NULL) { - DECREF(div); + Py_DECREF(div); return -1; } - one = (longobject *) newlongobject(1L); + one = (PyLongObject *) PyLong_FromLong(1L); if (one == NULL || - (temp = (longobject *) long_sub(div, one)) == NULL) { - DECREF(mod); - DECREF(div); - XDECREF(one); + (temp = (PyLongObject *) long_sub(div, one)) == NULL) { + Py_DECREF(mod); + Py_DECREF(div); + Py_XDECREF(one); return -1; } - DECREF(one); - DECREF(div); + Py_DECREF(one); + Py_DECREF(div); div = temp; } *pdiv = div; @@ -976,81 +986,82 @@ l_divmod(v, w, pdiv, pmod) return 0; } -static object * +static PyObject * long_div(v, w) - longobject *v; - longobject *w; + PyLongObject *v; + PyLongObject *w; { - longobject *div, *mod; + PyLongObject *div, *mod; if (l_divmod(v, w, &div, &mod) < 0) return NULL; - DECREF(mod); - return (object *)div; + Py_DECREF(mod); + return (PyObject *)div; } -static object * +static PyObject * long_mod(v, w) - longobject *v; - longobject *w; + PyLongObject *v; + PyLongObject *w; { - longobject *div, *mod; + PyLongObject *div, *mod; if (l_divmod(v, w, &div, &mod) < 0) return NULL; - DECREF(div); - return (object *)mod; + Py_DECREF(div); + return (PyObject *)mod; } -static object * +static PyObject * long_divmod(v, w) - longobject *v; - longobject *w; + PyLongObject *v; + PyLongObject *w; { - object *z; - longobject *div, *mod; + PyObject *z; + PyLongObject *div, *mod; if (l_divmod(v, w, &div, &mod) < 0) return NULL; - z = newtupleobject(2); + z = PyTuple_New(2); if (z != NULL) { - settupleitem(z, 0, (object *) div); - settupleitem(z, 1, (object *) mod); + PyTuple_SetItem(z, 0, (PyObject *) div); + PyTuple_SetItem(z, 1, (PyObject *) mod); } else { - DECREF(div); - DECREF(mod); + Py_DECREF(div); + Py_DECREF(mod); } return z; } -static object * +static PyObject * long_pow(a, b, c) - longobject *a; - longobject *b; - longobject *c; + PyLongObject *a; + PyLongObject *b; + PyLongObject *c; { - longobject *z, *div, *mod; + PyLongObject *z, *div, *mod; int size_b, i; size_b = b->ob_size; if (size_b < 0) { - err_setstr(ValueError, "long integer to the negative power"); + PyErr_SetString(PyExc_ValueError, + "long integer to the negative power"); return NULL; } - z = (longobject *)newlongobject(1L); - INCREF(a); + z = (PyLongObject *)PyLong_FromLong(1L); + Py_INCREF(a); for (i = 0; i < size_b; ++i) { digit bi = b->ob_digit[i]; int j; for (j = 0; j < SHIFT; ++j) { - longobject *temp; + PyLongObject *temp; if (bi & 1) { - temp = (longobject *)long_mul(z, a); - DECREF(z); - if ((object*)c!=None && temp!=NULL) { + temp = (PyLongObject *)long_mul(z, a); + Py_DECREF(z); + if ((PyObject*)c!=Py_None && temp!=NULL) { l_divmod(temp, c, &div, &mod); - XDECREF(div); - DECREF(temp); + Py_XDECREF(div); + Py_DECREF(temp); temp = mod; } z = temp; @@ -1060,17 +1071,17 @@ long_pow(a, b, c) bi >>= 1; if (bi == 0 && i+1 == size_b) break; - temp = (longobject *)long_mul(a, a); - DECREF(a); - if ((object*)c!=None && temp!=NULL) { + temp = (PyLongObject *)long_mul(a, a); + Py_DECREF(a); + if ((PyObject*)c!=Py_None && temp!=NULL) { l_divmod(temp, c, &div, &mod); - XDECREF(div); - DECREF(temp); + Py_XDECREF(div); + Py_DECREF(temp); temp = mod; } a = temp; if (a == NULL) { - DECREF(z); + Py_DECREF(z); z = NULL; break; } @@ -1078,124 +1089,124 @@ long_pow(a, b, c) if (a == NULL || z == NULL) break; } - XDECREF(a); - if ((object*)c!=None && z!=NULL) { + Py_XDECREF(a); + if ((PyObject*)c!=Py_None && z!=NULL) { l_divmod(z, c, &div, &mod); - XDECREF(div); - DECREF(z); + Py_XDECREF(div); + Py_DECREF(z); z=mod; } - return (object *)z; + return (PyObject *)z; } -static object * +static PyObject * long_invert(v) - longobject *v; + PyLongObject *v; { /* Implement ~x as -(x+1) */ - longobject *x; - longobject *w; - w = (longobject *)newlongobject(1L); + PyLongObject *x; + PyLongObject *w; + w = (PyLongObject *)PyLong_FromLong(1L); if (w == NULL) return NULL; - x = (longobject *) long_add(v, w); - DECREF(w); + x = (PyLongObject *) long_add(v, w); + Py_DECREF(w); if (x == NULL) return NULL; if (x->ob_size != 0) x->ob_size = -(x->ob_size); - return (object *)x; + return (PyObject *)x; } -static object * +static PyObject * long_pos(v) - longobject *v; + PyLongObject *v; { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } -static object * +static PyObject * long_neg(v) - longobject *v; + PyLongObject *v; { - longobject *z; + PyLongObject *z; int i, n; n = ABS(v->ob_size); if (n == 0) { /* -0 == 0 */ - INCREF(v); - return (object *) v; + Py_INCREF(v); + return (PyObject *) v; } - z = alloclongobject(ABS(n)); + z = _PyLong_New(ABS(n)); if (z == NULL) return NULL; for (i = 0; i < n; i++) z->ob_digit[i] = v->ob_digit[i]; z->ob_size = -(v->ob_size); - return (object *)z; + return (PyObject *)z; } -static object * +static PyObject * long_abs(v) - longobject *v; + PyLongObject *v; { if (v->ob_size < 0) return long_neg(v); else { - INCREF(v); - return (object *)v; + Py_INCREF(v); + return (PyObject *)v; } } static int long_nonzero(v) - longobject *v; + PyLongObject *v; { return ABS(v->ob_size) != 0; } -static object * +static PyObject * long_rshift(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { - longobject *z; + PyLongObject *z; long shiftby; int newsize, wordshift, loshift, hishift, i, j; digit lomask, himask; if (a->ob_size < 0) { /* Right shifting negative numbers is harder */ - longobject *a1, *a2, *a3; - a1 = (longobject *) long_invert(a); + PyLongObject *a1, *a2, *a3; + a1 = (PyLongObject *) long_invert(a); if (a1 == NULL) return NULL; - a2 = (longobject *) long_rshift(a1, b); - DECREF(a1); + a2 = (PyLongObject *) long_rshift(a1, b); + Py_DECREF(a1); if (a2 == NULL) return NULL; - a3 = (longobject *) long_invert(a2); - DECREF(a2); - return (object *) a3; + a3 = (PyLongObject *) long_invert(a2); + Py_DECREF(a2); + return (PyObject *) a3; } - shiftby = getlongvalue((object *)b); - if (shiftby == -1L && err_occurred()) + shiftby = PyLong_AsLong((PyObject *)b); + if (shiftby == -1L && PyErr_Occurred()) return NULL; if (shiftby < 0) { - err_setstr(ValueError, "negative shift count"); + PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } wordshift = shiftby / SHIFT; newsize = ABS(a->ob_size) - wordshift; if (newsize <= 0) { - z = alloclongobject(0); - return (object *)z; + z = _PyLong_New(0); + return (PyObject *)z; } loshift = shiftby % SHIFT; hishift = SHIFT - loshift; lomask = ((digit)1 << hishift) - 1; himask = MASK ^ lomask; - z = alloclongobject(newsize); + z = _PyLong_New(newsize); if (z == NULL) return NULL; if (a->ob_size < 0) @@ -1206,29 +1217,30 @@ long_rshift(a, b) z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; } - return (object *) long_normalize(z); + return (PyObject *) long_normalize(z); } -static object * +static PyObject * long_lshift(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { /* This version due to Tim Peters */ - longobject *z; + PyLongObject *z; long shiftby; int oldsize, newsize, wordshift, remshift, i, j; twodigits accum; - shiftby = getlongvalue((object *)b); - if (shiftby == -1L && err_occurred()) + shiftby = PyLong_AsLong((PyObject *)b); + if (shiftby == -1L && PyErr_Occurred()) return NULL; if (shiftby < 0) { - err_setstr(ValueError, "negative shift count"); + PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } if ((long)(int)shiftby != shiftby) { - err_setstr(ValueError, "outrageous left shift count"); + PyErr_SetString(PyExc_ValueError, + "outrageous left shift count"); return NULL; } /* wordshift, remshift = divmod(shiftby, SHIFT) */ @@ -1239,7 +1251,7 @@ long_lshift(a, b) newsize = oldsize + wordshift; if (remshift) ++newsize; - z = alloclongobject(newsize); + z = _PyLong_New(newsize); if (z == NULL) return NULL; if (a->ob_size < 0) @@ -1256,7 +1268,7 @@ long_lshift(a, b) z->ob_digit[newsize-1] = (digit)accum; else assert(!accum); - return (object *) long_normalize(z); + return (PyObject *) long_normalize(z); } @@ -1265,46 +1277,46 @@ long_lshift(a, b) #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) -static object *long_bitwise PROTO((longobject *, int, longobject *)); -static object * +static PyObject *long_bitwise Py_PROTO((PyLongObject *, int, PyLongObject *)); +static PyObject * long_bitwise(a, op, b) - longobject *a; + PyLongObject *a; int op; /* '&', '|', '^' */ - longobject *b; + PyLongObject *b; { digit maska, maskb; /* 0 or MASK */ int negz; int size_a, size_b, size_z; - longobject *z; + PyLongObject *z; int i; digit diga, digb; - object *v; + PyObject *v; if (a->ob_size < 0) { - a = (longobject *) long_invert(a); + a = (PyLongObject *) long_invert(a); maska = MASK; } else { - INCREF(a); + Py_INCREF(a); maska = 0; } if (b->ob_size < 0) { - b = (longobject *) long_invert(b); + b = (PyLongObject *) long_invert(b); maskb = MASK; } else { - INCREF(b); + Py_INCREF(b); maskb = 0; } size_a = a->ob_size; size_b = b->ob_size; size_z = MAX(size_a, size_b); - z = alloclongobject(size_z); + z = _PyLong_New(size_z); if (a == NULL || b == NULL || z == NULL) { - XDECREF(a); - XDECREF(b); - XDECREF(z); + Py_XDECREF(a); + Py_XDECREF(b); + Py_XDECREF(z); return NULL; } @@ -1344,93 +1356,93 @@ long_bitwise(a, op, b) } } - DECREF(a); - DECREF(b); + Py_DECREF(a); + Py_DECREF(b); z = long_normalize(z); if (negz == 0) - return (object *) z; + return (PyObject *) z; v = long_invert(z); - DECREF(z); + Py_DECREF(z); return v; } -static object * +static PyObject * long_and(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { return long_bitwise(a, '&', b); } -static object * +static PyObject * long_xor(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { return long_bitwise(a, '^', b); } -static object * +static PyObject * long_or(a, b) - longobject *a; - longobject *b; + PyLongObject *a; + PyLongObject *b; { return long_bitwise(a, '|', b); } static int long_coerce(pv, pw) - object **pv; - object **pw; + PyObject **pv; + PyObject **pw; { - if (is_intobject(*pw)) { - *pw = newlongobject(getintvalue(*pw)); - INCREF(*pv); + if (PyInt_Check(*pw)) { + *pw = PyLong_FromLong(PyInt_AsLong(*pw)); + Py_INCREF(*pv); return 0; } return 1; /* Can't do it */ } -static object * +static PyObject * long_int(v) - object *v; + PyObject *v; { long x; - x = getlongvalue(v); - if (err_occurred()) + x = PyLong_AsLong(v); + if (PyErr_Occurred()) return NULL; - return newintobject(x); + return PyInt_FromLong(x); } -static object * +static PyObject * long_long(v) - object *v; + PyObject *v; { - INCREF(v); + Py_INCREF(v); return v; } -static object * +static PyObject * long_float(v) - object *v; + PyObject *v; { double result; PyFPE_START_PROTECT("long_float", return 0) - result = dgetlongvalue(v); + result = PyLong_AsDouble(v); PyFPE_END_PROTECT(result) - return newfloatobject(result); + return PyFloat_FromDouble(result); } -static object * +static PyObject * long_oct(v) - object *v; + PyObject *v; { return long_format(v, 8); } -static object * +static PyObject * long_hex(v) - object *v; + PyObject *v; { return long_format(v, 16); } @@ -1441,7 +1453,7 @@ long_hex(v) #define TF (ternaryfunc) #define IF (inquiry) -static number_methods long_as_number = { +static PyNumberMethods long_as_number = { BF long_add, /*nb_add*/ BF long_sub, /*nb_subtract*/ BF long_mul, /*nb_multiply*/ @@ -1459,7 +1471,7 @@ static number_methods long_as_number = { BF long_and, /*nb_and*/ BF long_xor, /*nb_xor*/ BF long_or, /*nb_or*/ - (int (*) FPROTO((object **, object **))) + (int (*) Py_FPROTO((PyObject **, PyObject **))) (coercion)long_coerce, /*nb_coerce*/ UF long_int, /*nb_int*/ UF long_long, /*nb_long*/ @@ -1468,22 +1480,22 @@ static number_methods long_as_number = { UF long_hex, /*nb_hex*/ }; -typeobject Longtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyLong_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "long int", - sizeof(longobject) - sizeof(digit), + sizeof(PyLongObject) - sizeof(digit), sizeof(digit), (destructor)long_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ - (int (*) FPROTO((object *, object *))) + (int (*) Py_FPROTO((PyObject *, PyObject *))) (cmpfunc)long_compare, /*tp_compare*/ (reprfunc)long_repr, /*tp_repr*/ &long_as_number,/*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ - (long (*) FPROTO((object *))) + (long (*) Py_FPROTO((PyObject *))) (hashfunc)long_hash, /*tp_hash*/ }; diff --git a/Objects/mappingobject.c b/Objects/mappingobject.c index 39c2fc9..a749f0a 100644 --- a/Objects/mappingobject.c +++ b/Objects/mappingobject.c @@ -37,8 +37,7 @@ PERFORMANCE OF THIS SOFTWARE. originally derived from) a file by that name I had to change its name. For the user these objects are still called "dictionaries". */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" /* @@ -85,7 +84,7 @@ static long polys[] = { }; /* Object used as dummy key to fill deleted entries */ -static object *dummy; /* Initialized by first call to newmappingobject() */ +static PyObject *dummy; /* Initialized by first call to newmappingobject() */ /* Invariant for entries: when in use, de_value is not NULL and de_key is @@ -95,8 +94,8 @@ NULL, since otherwise other keys may be lost. */ typedef struct { long me_hash; - object *me_key; - object *me_value; + PyObject *me_key; + PyObject *me_value; #ifdef USE_CACHE_ALIGNED long aligner; #endif @@ -111,7 +110,7 @@ To avoid slowing down lookups on a near-full table, we resize the table when it is more than half filled. */ typedef struct { - OB_HEAD + PyObject_HEAD int ma_fill; int ma_used; int ma_size; @@ -119,16 +118,16 @@ typedef struct { mappingentry *ma_table; } mappingobject; -object * -newmappingobject() +PyObject * +PyDict_New() { register mappingobject *mp; if (dummy == NULL) { /* Auto-initialize dummy */ - dummy = newstringobject("<dummy key>"); + dummy = PyString_FromString("<dummy key>"); if (dummy == NULL) return NULL; } - mp = NEWOBJ(mappingobject, &Mappingtype); + mp = PyObject_NEW(mappingobject, &PyDict_Type); if (mp == NULL) return NULL; mp->ma_size = 0; @@ -136,7 +135,7 @@ newmappingobject() mp->ma_table = NULL; mp->ma_fill = 0; mp->ma_used = 0; - return (object *)mp; + return (PyObject *)mp; } /* @@ -160,11 +159,11 @@ where x is a root. The initial value is derived from sum, too. (This version is due to Reimer Behrends, some ideas are also due to Jyrki Alakuijala.) */ -static mappingentry *lookmapping PROTO((mappingobject *, object *, long)); +static mappingentry *lookmapping Py_PROTO((mappingobject *, PyObject *, long)); static mappingentry * lookmapping(mp, key, hash) mappingobject *mp; - object *key; + PyObject *key; long hash; { register int i; @@ -186,7 +185,9 @@ lookmapping(mp, key, hash) if (ep->me_key == dummy) freeslot = ep; else if (ep->me_key == key || - (ep->me_hash == hash && cmpobject(ep->me_key, key) == 0)) { + (ep->me_hash == hash && + PyObject_Compare(ep->me_key, key) == 0)) + { return ep; } /* Derive incr from sum, just to make it more arbitrary. Note that @@ -211,7 +212,7 @@ lookmapping(mp, key, hash) } else if (ep->me_key == key || (ep->me_hash == hash && - cmpobject(ep->me_key, key) == 0)) { + PyObject_Compare(ep->me_key, key) == 0)) { return ep; } /* Cycle through GF(2^n)-{0} */ @@ -226,28 +227,29 @@ Internal routine to insert a new item into the table. Used both by the internal resize routine and by the public insert routine. Eats a reference to key and one to value. */ -static void insertmapping PROTO((mappingobject *, object *, long, object *)); +static void insertmapping + Py_PROTO((mappingobject *, PyObject *, long, PyObject *)); static void insertmapping(mp, key, hash, value) register mappingobject *mp; - object *key; + PyObject *key; long hash; - object *value; + PyObject *value; { - object *old_value; + PyObject *old_value; register mappingentry *ep; ep = lookmapping(mp, key, hash); if (ep->me_value != NULL) { old_value = ep->me_value; ep->me_value = value; - DECREF(old_value); /* which **CAN** re-enter */ - DECREF(key); + Py_DECREF(old_value); /* which **CAN** re-enter */ + Py_DECREF(key); } else { if (ep->me_key == NULL) mp->ma_fill++; else - DECREF(ep->me_key); + Py_DECREF(ep->me_key); ep->me_key = key; ep->me_hash = hash; ep->me_value = value; @@ -260,7 +262,7 @@ Restructure the table by allocating a new table and reinserting all items again. When entries have been deleted, the new table may actually be smaller than the old one. */ -static int mappingresize PROTO((mappingobject *)); +static int mappingresize Py_PROTO((mappingobject *)); static int mappingresize(mp) mappingobject *mp; @@ -275,7 +277,7 @@ mappingresize(mp) for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) { if (i > sizeof(polys)/sizeof(polys[0])) { /* Ran out of polynomials */ - err_nomem(); + PyErr_NoMemory(); return -1; } if (newsize > mp->ma_used*2) { @@ -285,7 +287,7 @@ mappingresize(mp) } newtable = (mappingentry *) calloc(sizeof(mappingentry), newsize); if (newtable == NULL) { - err_nomem(); + PyErr_NoMemory(); return -1; } mp->ma_size = newsize; @@ -302,31 +304,31 @@ mappingresize(mp) } for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { if (ep->me_value == NULL) - XDECREF(ep->me_key); + Py_XDECREF(ep->me_key); } - XDEL(oldtable); + PyMem_XDEL(oldtable); return 0; } -object * -mappinglookup(op, key) - object *op; - object *key; +PyObject * +PyDict_GetItem(op, key) + PyObject *op; + PyObject *key; { long hash; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return NULL; } if (((mappingobject *)op)->ma_table == NULL) return NULL; #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } @@ -334,37 +336,37 @@ mappinglookup(op, key) } int -mappinginsert(op, key, value) - register object *op; - object *key; - object *value; +PyDict_SetItem(op, key, value) + register PyObject *op; + PyObject *key; + PyObject *value; { register mappingobject *mp; register long hash; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return -1; } mp = (mappingobject *)op; #ifdef CACHE_HASH - if (is_stringobject(key)) { + if (PyString_Check(key)) { #ifdef INTERN_STRINGS - if (((stringobject *)key)->ob_sinterned != NULL) { - key = ((stringobject *)key)->ob_sinterned; - hash = ((stringobject *)key)->ob_shash; + if (((PyStringObject *)key)->ob_sinterned != NULL) { + key = ((PyStringObject *)key)->ob_sinterned; + hash = ((PyStringObject *)key)->ob_shash; } else #endif { - hash = ((stringobject *)key)->ob_shash; + hash = ((PyStringObject *)key)->ob_shash; if (hash == -1) - hash = hashobject(key); + hash = PyObject_Hash(key); } } else #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return -1; } @@ -375,32 +377,32 @@ mappinginsert(op, key, value) return -1; } } - INCREF(value); - INCREF(key); + Py_INCREF(value); + Py_INCREF(key); insertmapping(mp, key, hash, value); return 0; } int -mappingremove(op, key) - object *op; - object *key; +PyDict_DelItem(op, key) + PyObject *op; + PyObject *key; { register mappingobject *mp; register long hash; register mappingentry *ep; - object *old_value, *old_key; + PyObject *old_value, *old_key; - if (!is_mappingobject(op)) { - err_badcall(); + if (!PyDict_Check(op)) { + PyErr_BadInternalCall(); return -1; } #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return -1; } @@ -410,28 +412,28 @@ mappingremove(op, key) ep = lookmapping(mp, key, hash); if (ep->me_value == NULL) { empty: - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); return -1; } old_key = ep->me_key; - INCREF(dummy); + Py_INCREF(dummy); ep->me_key = dummy; old_value = ep->me_value; ep->me_value = NULL; mp->ma_used--; - DECREF(old_value); - DECREF(old_key); + Py_DECREF(old_value); + Py_DECREF(old_key); return 0; } void -mappingclear(op) - object *op; +PyDict_Clear(op) + PyObject *op; { int i, n; register mappingentry *table; mappingobject *mp; - if (!is_mappingobject(op)) + if (!PyDict_Check(op)) return; mp = (mappingobject *)op; table = mp->ma_table; @@ -441,22 +443,22 @@ mappingclear(op) mp->ma_size = mp->ma_used = mp->ma_fill = 0; mp->ma_table = NULL; for (i = 0; i < n; i++) { - XDECREF(table[i].me_key); - XDECREF(table[i].me_value); + Py_XDECREF(table[i].me_key); + Py_XDECREF(table[i].me_value); } - DEL(table); + PyMem_DEL(table); } int -mappinggetnext(op, ppos, pkey, pvalue) - object *op; +PyDict_Next(op, ppos, pkey, pvalue) + PyObject *op; int *ppos; - object **pkey; - object **pvalue; + PyObject **pkey; + PyObject **pvalue; { int i; register mappingobject *mp; - if (!is_dictobject(op)) + if (!PyDict_Check(op)) return 0; mp = (mappingobject *)op; i = *ppos; @@ -484,12 +486,12 @@ mapping_dealloc(mp) register mappingentry *ep; for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { if (ep->me_key != NULL) - DECREF(ep->me_key); + Py_DECREF(ep->me_key); if (ep->me_value != NULL) - DECREF(ep->me_value); + Py_DECREF(ep->me_value); } - XDEL(mp->ma_table); - DEL(mp); + PyMem_XDEL(mp->ma_table); + PyMem_DEL(mp); } static int @@ -507,10 +509,10 @@ mapping_print(mp, fp, flags) if (ep->me_value != NULL) { if (any++ > 0) fprintf(fp, ", "); - if (printobject((object *)ep->me_key, fp, 0) != 0) + if (PyObject_Print((PyObject *)ep->me_key, fp, 0) != 0) return -1; fprintf(fp, ": "); - if (printobject(ep->me_value, fp, 0) != 0) + if (PyObject_Print(ep->me_value, fp, 0) != 0) return -1; } } @@ -518,31 +520,31 @@ mapping_print(mp, fp, flags) return 0; } -static object * +static PyObject * mapping_repr(mp) mappingobject *mp; { - auto object *v; - object *sepa, *colon; + auto PyObject *v; + PyObject *sepa, *colon; register int i; register int any; register mappingentry *ep; - v = newstringobject("{"); - sepa = newstringobject(", "); - colon = newstringobject(": "); + v = PyString_FromString("{"); + sepa = PyString_FromString(", "); + colon = PyString_FromString(": "); any = 0; for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) { if (ep->me_value != NULL) { if (any++) - joinstring(&v, sepa); - joinstring_decref(&v, reprobject(ep->me_key)); - joinstring(&v, colon); - joinstring_decref(&v, reprobject(ep->me_value)); + PyString_Concat(&v, sepa); + PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); + PyString_Concat(&v, colon); + PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); } } - joinstring_decref(&v, newstringobject("}")); - XDECREF(sepa); - XDECREF(colon); + PyString_ConcatAndDel(&v, PyString_FromString("}")); + Py_XDECREF(sepa); + Py_XDECREF(colon); return v; } @@ -553,123 +555,123 @@ mapping_length(mp) return mp->ma_used; } -static object * +static PyObject * mapping_subscript(mp, key) mappingobject *mp; - register object *key; + register PyObject *key; { - object *v; + PyObject *v; long hash; if (mp->ma_table == NULL) { - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); return NULL; } #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } v = lookmapping(mp, key, hash) -> me_value; if (v == NULL) - err_setval(KeyError, key); + PyErr_SetObject(PyExc_KeyError, key); else - INCREF(v); + Py_INCREF(v); return v; } static int mapping_ass_sub(mp, v, w) mappingobject *mp; - object *v, *w; + PyObject *v, *w; { if (w == NULL) - return mappingremove((object *)mp, v); + return PyDict_DelItem((PyObject *)mp, v); else - return mappinginsert((object *)mp, v, w); + return PyDict_SetItem((PyObject *)mp, v, w); } -static mapping_methods mapping_as_mapping = { +static PyMappingMethods mapping_as_mapping = { (inquiry)mapping_length, /*mp_length*/ (binaryfunc)mapping_subscript, /*mp_subscript*/ (objobjargproc)mapping_ass_sub, /*mp_ass_subscript*/ }; -static object * +static PyObject * mapping_keys(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *key = mp->ma_table[i].me_key; - INCREF(key); - setlistitem(v, j, key); + PyObject *key = mp->ma_table[i].me_key; + Py_INCREF(key); + PyList_SetItem(v, j, key); j++; } } return v; } -static object * +static PyObject * mapping_values(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *value = mp->ma_table[i].me_value; - INCREF(value); - setlistitem(v, j, value); + PyObject *value = mp->ma_table[i].me_value; + Py_INCREF(value); + PyList_SetItem(v, j, value); j++; } } return v; } -static object * +static PyObject * mapping_items(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - register object *v; + register PyObject *v; register int i, j; - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - v = newlistobject(mp->ma_used); + v = PyList_New(mp->ma_used); if (v == NULL) return NULL; for (i = 0, j = 0; i < mp->ma_size; i++) { if (mp->ma_table[i].me_value != NULL) { - object *key = mp->ma_table[i].me_key; - object *value = mp->ma_table[i].me_value; - object *item = newtupleobject(2); + PyObject *key = mp->ma_table[i].me_key; + PyObject *value = mp->ma_table[i].me_value; + PyObject *item = PyTuple_New(2); if (item == NULL) { - DECREF(v); + Py_DECREF(v); return NULL; } - INCREF(key); - settupleitem(item, 0, key); - INCREF(value); - settupleitem(item, 1, value); - setlistitem(v, j, item); + Py_INCREF(key); + PyTuple_SetItem(item, 0, key); + Py_INCREF(value); + PyTuple_SetItem(item, 1, value); + PyList_SetItem(v, j, item); j++; } } @@ -677,47 +679,47 @@ mapping_items(mp, args) } int -getmappingsize(mp) - object *mp; +PyDict_Size(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return 0; } return ((mappingobject *)mp)->ma_used; } -object * -getmappingkeys(mp) - object *mp; +PyObject * +PyDict_Keys(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_keys((mappingobject *)mp, (object *)NULL); + return mapping_keys((mappingobject *)mp, (PyObject *)NULL); } -object * -getmappingvalues(mp) - object *mp; +PyObject * +PyDict_Values(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_values((mappingobject *)mp, (object *)NULL); + return mapping_values((mappingobject *)mp, (PyObject *)NULL); } -object * -getmappingitems(mp) - object *mp; +PyObject * +PyDict_Items(mp) + PyObject *mp; { - if (mp == NULL || !is_mappingobject(mp)) { - err_badcall(); + if (mp == NULL || !PyDict_Check(mp)) { + PyErr_BadInternalCall(); return NULL; } - return mapping_items((mappingobject *)mp, (object *)NULL); + return mapping_items((mappingobject *)mp, (PyObject *)NULL); } #define NEWCMP @@ -728,25 +730,26 @@ getmappingitems(mp) is different or absent. The value is returned too, through the pval argument. No reference counts are incremented. */ -static object * +static PyObject * characterize(a, b, pval) mappingobject *a; mappingobject *b; - object **pval; + PyObject **pval; { - object *diff = NULL; + PyObject *diff = NULL; int i; *pval = NULL; for (i = 0; i < a->ma_size; i++) { if (a->ma_table[i].me_value != NULL) { - object *key = a->ma_table[i].me_key; - object *aval, *bval; - if (diff != NULL && cmpobject(key, diff) > 0) + PyObject *key = a->ma_table[i].me_key; + PyObject *aval, *bval; + if (diff != NULL && PyObject_Compare(key, diff) > 0) continue; aval = a->ma_table[i].me_value; - bval = mappinglookup((object *)b, key); - if (bval == NULL || cmpobject(aval, bval) != 0) { + bval = PyDict_GetItem((PyObject *)b, key); + if (bval == NULL || PyObject_Compare(aval, bval) != 0) + { diff = key; *pval = aval; } @@ -759,7 +762,7 @@ static int mapping_compare(a, b) mappingobject *a, *b; { - object *adiff, *bdiff, *aval, *bval; + PyObject *adiff, *bdiff, *aval, *bval; int res; /* Compare lengths first */ @@ -773,9 +776,9 @@ mapping_compare(a, b) return 0; /* a is a subset with the same length */ bdiff = characterize(b, a, &bval); /* bdiff == NULL would be impossible now */ - res = cmpobject(adiff, bdiff); + res = PyObject_Compare(adiff, bdiff); if (res == 0) - res = cmpobject(aval, bval); + res = PyObject_Compare(aval, bval); return res; } @@ -785,7 +788,7 @@ static int mapping_compare(a, b) mappingobject *a, *b; { - object *akeys, *bkeys; + PyObject *akeys, *bkeys; int i, n, res; if (a == b) return 0; @@ -799,51 +802,51 @@ mapping_compare(a, b) if (b->ma_used == 0) return 1; } - akeys = mapping_keys(a, (object *)NULL); - bkeys = mapping_keys(b, (object *)NULL); + akeys = mapping_keys(a, (PyObject *)NULL); + bkeys = mapping_keys(b, (PyObject *)NULL); if (akeys == NULL || bkeys == NULL) { /* Oops, out of memory -- what to do? */ /* For now, sort on address! */ - XDECREF(akeys); - XDECREF(bkeys); + Py_XDECREF(akeys); + Py_XDECREF(bkeys); if (a < b) return -1; else return 1; } - sortlist(akeys); - sortlist(bkeys); + PyList_Sort(akeys); + PyList_Sort(bkeys); n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ res = 0; for (i = 0; i < n; i++) { - object *akey, *bkey, *aval, *bval; + PyObject *akey, *bkey, *aval, *bval; long ahash, bhash; - akey = getlistitem(akeys, i); - bkey = getlistitem(bkeys, i); - res = cmpobject(akey, bkey); + akey = PyList_GetItem(akeys, i); + bkey = PyList_GetItem(bkeys, i); + res = PyObject_Compare(akey, bkey); if (res != 0) break; #ifdef CACHE_HASH - if (!is_stringobject(akey) || - (ahash = ((stringobject *) akey)->ob_shash) == -1) + if (!PyString_Check(akey) || + (ahash = ((PyStringObject *) akey)->ob_shash) == -1) #endif { - ahash = hashobject(akey); + ahash = PyObject_Hash(akey); if (ahash == -1) - err_clear(); /* Don't want errors here */ + PyErr_Clear(); /* Don't want errors here */ } #ifdef CACHE_HASH - if (!is_stringobject(bkey) || - (bhash = ((stringobject *) bkey)->ob_shash) == -1) + if (!PyString_Check(bkey) || + (bhash = ((PyStringObject *) bkey)->ob_shash) == -1) #endif { - bhash = hashobject(bkey); + bhash = PyObject_Hash(bkey); if (bhash == -1) - err_clear(); /* Don't want errors here */ + PyErr_Clear(); /* Don't want errors here */ } aval = lookmapping(a, akey, ahash) -> me_value; bval = lookmapping(b, bkey, bhash) -> me_value; - res = cmpobject(aval, bval); + res = PyObject_Compare(aval, bval); if (res != 0) break; } @@ -853,67 +856,67 @@ mapping_compare(a, b) else if (a->ma_used > b->ma_used) res = 1; } - DECREF(akeys); - DECREF(bkeys); + Py_DECREF(akeys); + Py_DECREF(bkeys); return res; } #endif /* !NEWCMP */ -static object * +static PyObject * mapping_has_key(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - object *key; + PyObject *key; long hash; register long ok; - if (!getargs(args, "O", &key)) + if (!PyArg_Parse(args, "O", &key)) return NULL; #ifdef CACHE_HASH - if (!is_stringobject(key) || - (hash = ((stringobject *) key)->ob_shash) == -1) + if (!PyString_Check(key) || + (hash = ((PyStringObject *) key)->ob_shash) == -1) #endif { - hash = hashobject(key); + hash = PyObject_Hash(key); if (hash == -1) return NULL; } ok = mp->ma_size != 0 && lookmapping(mp, key, hash)->me_value != NULL; - return newintobject(ok); + return PyInt_FromLong(ok); } -static object * +static PyObject * mapping_clear(mp, args) register mappingobject *mp; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_NoArgs(args)) return NULL; - mappingclear((object *)mp); - INCREF(None); - return None; + PyDict_Clear((PyObject *)mp); + Py_INCREF(Py_None); + return Py_None; } -static struct methodlist mapp_methods[] = { - {"clear", (method)mapping_clear}, - {"has_key", (method)mapping_has_key}, - {"items", (method)mapping_items}, - {"keys", (method)mapping_keys}, - {"values", (method)mapping_values}, +static PyMethodDef mapp_methods[] = { + {"clear", (PyCFunction)mapping_clear}, + {"has_key", (PyCFunction)mapping_has_key}, + {"items", (PyCFunction)mapping_items}, + {"keys", (PyCFunction)mapping_keys}, + {"values", (PyCFunction)mapping_values}, {NULL, NULL} /* sentinel */ }; -static object * +static PyObject * mapping_getattr(mp, name) mappingobject *mp; char *name; { - return findmethod(mapp_methods, (object *)mp, name); + return Py_FindMethod(mapp_methods, (PyObject *)mp, name); } -typeobject Mappingtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyDict_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "dictionary", sizeof(mappingobject), @@ -931,100 +934,100 @@ typeobject Mappingtype = { /* For backward compatibility with old dictionary interface */ -static object *last_name_object; +static PyObject *last_name_object; static char *last_name_char; /* NULL or == getstringvalue(last_name_object) */ -object * -getattro(v, name) - object *v; - object *name; +PyObject * +PyObject_GetAttr(v, name) + PyObject *v; + PyObject *name; { if (v->ob_type->tp_getattro != NULL) return (*v->ob_type->tp_getattro)(v, name); if (name != last_name_object) { - XDECREF(last_name_object); - INCREF(name); + Py_XDECREF(last_name_object); + Py_INCREF(name); last_name_object = name; - last_name_char = getstringvalue(name); + last_name_char = PyString_AsString(name); } - return getattr(v, last_name_char); + return PyObject_GetAttrString(v, last_name_char); } int -setattro(v, name, value) - object *v; - object *name; - object *value; +PyObject_SetAttr(v, name, value) + PyObject *v; + PyObject *name; + PyObject *value; { int err; - INCREF(name); + Py_INCREF(name); PyString_InternInPlace(&name); if (v->ob_type->tp_setattro != NULL) err = (*v->ob_type->tp_setattro)(v, name, value); else { if (name != last_name_object) { - XDECREF(last_name_object); - INCREF(name); + Py_XDECREF(last_name_object); + Py_INCREF(name); last_name_object = name; - last_name_char = getstringvalue(name); + last_name_char = PyString_AsString(name); } - err = setattr(v, last_name_char, value); + err = PyObject_SetAttrString(v, last_name_char, value); } - DECREF(name); + Py_DECREF(name); return err; } -object * -dictlookup(v, key) - object *v; +PyObject * +PyDict_GetItemString(v, key) + PyObject *v; char *key; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return NULL; } PyString_InternInPlace(&last_name_object); - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappinglookup(v, last_name_object); + return PyDict_GetItem(v, last_name_object); } int -dictinsert(v, key, item) - object *v; +PyDict_SetItemString(v, key, item) + PyObject *v; char *key; - object *item; + PyObject *item; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return -1; } PyString_InternInPlace(&last_name_object); - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappinginsert(v, last_name_object, item); + return PyDict_SetItem(v, last_name_object, item); } int -dictremove(v, key) - object *v; +PyDict_DelItemString(v, key) + PyObject *v; char *key; { if (key != last_name_char) { - XDECREF(last_name_object); - last_name_object = newstringobject(key); + Py_XDECREF(last_name_object); + last_name_object = PyString_FromString(key); if (last_name_object == NULL) { last_name_char = NULL; return -1; } - last_name_char = getstringvalue(last_name_object); + last_name_char = PyString_AsString(last_name_object); } - return mappingremove(v, last_name_object); + return PyDict_DelItem(v, last_name_object); } diff --git a/Objects/methodobject.c b/Objects/methodobject.c index cb09223..c0befa9 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -31,111 +31,113 @@ PERFORMANCE OF THIS SOFTWARE. /* Method object implementation */ -#include "allobjects.h" +#include "Python.h" #include "token.h" typedef struct { - OB_HEAD - struct methodlist *m_ml; - object *m_self; -} methodobject; + PyObject_HEAD + PyMethodDef *m_ml; + PyObject *m_self; +} PyCFunctionObject; -object * -newmethodobject(ml, self) - struct methodlist *ml; - object *self; +PyObject * +PyCFunction_New(ml, self) + PyMethodDef *ml; + PyObject *self; { - methodobject *op = NEWOBJ(methodobject, &Methodtype); + PyCFunctionObject *op = PyObject_NEW(PyCFunctionObject, + &PyCFunction_Type); if (op != NULL) { op->m_ml = ml; - XINCREF(self); + Py_XINCREF(self); op->m_self = self; } - return (object *)op; + return (PyObject *)op; } -method -getmethod(op) - object *op; +PyCFunction +PyCFunction_GetFunction(op) + PyObject *op; { - if (!is_methodobject(op)) { - err_badcall(); + if (!PyCFunction_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((methodobject *)op) -> m_ml -> ml_meth; + return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; } -object * -getself(op) - object *op; +PyObject * +PyCFunction_GetSelf(op) + PyObject *op; { - if (!is_methodobject(op)) { - err_badcall(); + if (!PyCFunction_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((methodobject *)op) -> m_self; + return ((PyCFunctionObject *)op) -> m_self; } int -getflags(op) - object *op; +PyCFunction_GetFlags(op) + PyObject *op; { - if (!is_methodobject(op)) { - err_badcall(); + if (!PyCFunction_Check(op)) { + PyErr_BadInternalCall(); return -1; } - return ((methodobject *)op) -> m_ml -> ml_flags; + return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; } /* Methods (the standard built-in methods, that is) */ static void meth_dealloc(m) - methodobject *m; + PyCFunctionObject *m; { - XDECREF(m->m_self); + Py_XDECREF(m->m_self); free((char *)m); } -static object * +static PyObject * meth_getattr(m, name) - methodobject *m; + PyCFunctionObject *m; char *name; { if (strcmp(name, "__name__") == 0) { - return newstringobject(m->m_ml->ml_name); + return PyString_FromString(m->m_ml->ml_name); } if (strcmp(name, "__doc__") == 0) { char *doc = m->m_ml->ml_doc; if (doc != NULL) - return newstringobject(doc); - INCREF(None); - return None; + return PyString_FromString(doc); + Py_INCREF(Py_None); + return Py_None; } if (strcmp(name, "__self__") == 0) { - object *self; - if (getrestricted()) { - err_setstr(RuntimeError, + PyObject *self; + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, "method.__self__ not accessible in restricted mode"); return NULL; } self = m->m_self; if (self == NULL) - self = None; - INCREF(self); + self = Py_None; + Py_INCREF(self); return self; } if (strcmp(name, "__members__") == 0) { - return mkvalue("[sss]", "__doc__", "__name__", "__self__"); + return Py_BuildValue("[sss]", + "__doc__", "__name__", "__self__"); } - err_setstr(AttributeError, name); + PyErr_SetString(PyExc_AttributeError, name); return NULL; } -static object * +static PyObject * meth_repr(m) - methodobject *m; + PyCFunctionObject *m; { char buf[200]; if (m->m_self == NULL) @@ -145,15 +147,15 @@ meth_repr(m) "<built-in method %.80s of %.80s object at %lx>", m->m_ml->ml_name, m->m_self->ob_type->tp_name, (long)m->m_self); - return newstringobject(buf); + return PyString_FromString(buf); } static int meth_compare(a, b) - methodobject *a, *b; + PyCFunctionObject *a, *b; { if (a->m_self != b->m_self) - return cmpobject(a->m_self, b->m_self); + return PyObject_Compare(a->m_self, b->m_self); if (a->m_ml->ml_meth == b->m_ml->ml_meth) return 0; if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0) @@ -164,24 +166,24 @@ meth_compare(a, b) static long meth_hash(a) - methodobject *a; + PyCFunctionObject *a; { long x; if (a->m_self == NULL) x = 0; else { - x = hashobject(a->m_self); + x = PyObject_Hash(a->m_self); if (x == -1) return -1; } return x ^ (long) a->m_ml->ml_meth; } -typeobject Methodtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyCFunction_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "builtin_function_or_method", - sizeof(methodobject), + sizeof(PyCFunctionObject), 0, (destructor)meth_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -197,71 +199,71 @@ typeobject Methodtype = { /* List all methods in a chain -- helper for findmethodinchain */ -static object * +static PyObject * listmethodchain(chain) - struct methodchain *chain; + PyMethodChain *chain; { - struct methodchain *c; - struct methodlist *ml; + PyMethodChain *c; + PyMethodDef *ml; int i, n; - object *v; + PyObject *v; n = 0; for (c = chain; c != NULL; c = c->link) { for (ml = c->methods; ml->ml_name != NULL; ml++) n++; } - v = newlistobject(n); + v = PyList_New(n); if (v == NULL) return NULL; i = 0; for (c = chain; c != NULL; c = c->link) { for (ml = c->methods; ml->ml_name != NULL; ml++) { - setlistitem(v, i, newstringobject(ml->ml_name)); + PyList_SetItem(v, i, PyString_FromString(ml->ml_name)); i++; } } - if (err_occurred()) { - DECREF(v); + if (PyErr_Occurred()) { + Py_DECREF(v); return NULL; } - sortlist(v); + PyList_Sort(v); return v; } /* Find a method in a method chain */ -object * -findmethodinchain(chain, self, name) - struct methodchain *chain; - object *self; +PyObject * +Py_FindMethodInChain(chain, self, name) + PyMethodChain *chain; + PyObject *self; char *name; { if (strcmp(name, "__methods__") == 0) return listmethodchain(chain); while (chain != NULL) { - struct methodlist *ml = chain->methods; + PyMethodDef *ml = chain->methods; for (; ml->ml_name != NULL; ml++) { if (name[0] == ml->ml_name[0] && strcmp(name+1, ml->ml_name+1) == 0) - return newmethodobject(ml, self); + return PyCFunction_New(ml, self); } chain = chain->link; } - err_setstr(AttributeError, name); + PyErr_SetString(PyExc_AttributeError, name); return NULL; } /* Find a method in a single method list */ -object * -findmethod(methods, self, name) - struct methodlist *methods; - object *self; +PyObject * +Py_FindMethod(methods, self, name) + PyMethodDef *methods; + PyObject *self; char *name; { - struct methodchain chain; + PyMethodChain chain; chain.methods = methods; chain.link = NULL; - return findmethodinchain(&chain, self, name); + return Py_FindMethodInChain(&chain, self, name); } diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 1922c17..0612119 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -31,153 +31,154 @@ PERFORMANCE OF THIS SOFTWARE. /* Module object implementation */ -#include "allobjects.h" -#include "ceval.h" +#include "Python.h" typedef struct { - OB_HEAD - object *md_dict; -} moduleobject; + PyObject_HEAD + PyObject *md_dict; +} PyModuleObject; -object * -newmoduleobject(name) +PyObject * +PyModule_New(name) char *name; { - moduleobject *m; - object *nameobj; - m = NEWOBJ(moduleobject, &Moduletype); + PyModuleObject *m; + PyObject *nameobj; + m = PyObject_NEW(PyModuleObject, &PyModule_Type); if (m == NULL) return NULL; - nameobj = newstringobject(name); - m->md_dict = newdictobject(); + nameobj = PyString_FromString(name); + m->md_dict = PyDict_New(); if (m->md_dict == NULL || nameobj == NULL) goto fail; - if (dictinsert(m->md_dict, "__name__", nameobj) != 0) + if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0) goto fail; - if (dictinsert(m->md_dict, "__doc__", None) != 0) + if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) goto fail; - DECREF(nameobj); - return (object *)m; + Py_DECREF(nameobj); + return (PyObject *)m; fail: - XDECREF(nameobj); - DECREF(m); + Py_XDECREF(nameobj); + Py_DECREF(m); return NULL; } -object * -getmoduledict(m) - object *m; +PyObject * +PyModule_GetDict(m) + PyObject *m; { - if (!is_moduleobject(m)) { - err_badcall(); + if (!PyModule_Check(m)) { + PyErr_BadInternalCall(); return NULL; } - return ((moduleobject *)m) -> md_dict; + return ((PyModuleObject *)m) -> md_dict; } char * -getmodulename(m) - object *m; +PyModule_GetName(m) + PyObject *m; { - object *nameobj; - if (!is_moduleobject(m)) { - err_badarg(); + PyObject *nameobj; + if (!PyModule_Check(m)) { + PyErr_BadArgument(); return NULL; } - nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__"); - if (nameobj == NULL || !is_stringobject(nameobj)) { - err_setstr(SystemError, "nameless module"); + nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, + "__name__"); + if (nameobj == NULL || !PyString_Check(nameobj)) { + PyErr_SetString(PyExc_SystemError, "nameless module"); return NULL; } - return getstringvalue(nameobj); + return PyString_AsString(nameobj); } /* Methods */ static void module_dealloc(m) - moduleobject *m; + PyModuleObject *m; { if (m->md_dict != NULL) { - mappingclear(m->md_dict); - DECREF(m->md_dict); + PyDict_Clear(m->md_dict); + Py_DECREF(m->md_dict); } free((char *)m); } -static object * +static PyObject * module_repr(m) - moduleobject *m; + PyModuleObject *m; { char buf[100]; - char *name = getmodulename((object *)m); + char *name = PyModule_GetName((PyObject *)m); if (name == NULL) { - err_clear(); + PyErr_Clear(); name = "?"; } sprintf(buf, "<module '%.80s'>", name); - return newstringobject(buf); + return PyString_FromString(buf); } -static object * +static PyObject * module_getattr(m, name) - moduleobject *m; + PyModuleObject *m; char *name; { - object *res; + PyObject *res; if (strcmp(name, "__dict__") == 0) { - INCREF(m->md_dict); + Py_INCREF(m->md_dict); return m->md_dict; } - res = dictlookup(m->md_dict, name); + res = PyDict_GetItemString(m->md_dict, name); if (res == NULL) - err_setstr(AttributeError, name); + PyErr_SetString(PyExc_AttributeError, name); else { #ifdef SUPPORT_OBSOLETE_ACCESS - if (is_accessobject(res)) - res = getaccessvalue(res, getglobals()); + if (PyAccess_Check(res)) + res = PyAccess_AsValue(res, PyEval_GetGlobals()); else #endif - INCREF(res); + Py_INCREF(res); } return res; } static int module_setattr(m, name, v) - moduleobject *m; + PyModuleObject *m; char *name; - object *v; + PyObject *v; { #ifdef SUPPORT_OBSOLETE_ACCESS - object *ac; + PyObject *ac; #endif if (name[0] == '_' && strcmp(name, "__dict__") == 0) { - err_setstr(TypeError, "read-only special attribute"); + PyErr_SetString(PyExc_TypeError, + "read-only special attribute"); return -1; } #ifdef SUPPORT_OBSOLETE_ACCESS - ac = dictlookup(m->md_dict, name); - if (ac != NULL && is_accessobject(ac)) - return setaccessvalue(ac, getglobals(), v); + ac = PyDict_GetItemString(m->md_dict, name); + if (ac != NULL && PyAccess_Check(ac)) + return PyAccess_SetValue(ac, PyEval_GetGlobals(), v); #endif if (v == NULL) { - int rv = dictremove(m->md_dict, name); + int rv = PyDict_DelItemString(m->md_dict, name); if (rv < 0) - err_setstr(AttributeError, + PyErr_SetString(PyExc_AttributeError, "delete non-existing module attribute"); return rv; } else - return dictinsert(m->md_dict, name, v); + return PyDict_SetItemString(m->md_dict, name, v); } -typeobject Moduletype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyModule_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, /*ob_size*/ "module", /*tp_name*/ - sizeof(moduleobject), /*tp_size*/ + sizeof(PyModuleObject), /*tp_size*/ 0, /*tp_itemsize*/ (destructor)module_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ diff --git a/Objects/object.c b/Objects/object.c index fc1d086..a2198e6 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -31,10 +31,10 @@ PERFORMANCE OF THIS SOFTWARE. /* Generic object operations; and implementation of None (NoObject) */ -#include "allobjects.h" +#include "Python.h" #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) -long ref_total; +long _Py_RefTotal; #endif /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. @@ -42,14 +42,14 @@ long ref_total; Do not call them otherwise, they do not initialize the object! */ #ifdef COUNT_ALLOCS -static typeobject *type_list; +static PyTypeObject *type_list; extern int tuple_zero_allocs, fast_tuple_allocs; extern int quick_int_allocs, quick_neg_int_allocs; extern int null_strings, one_strings; void dump_counts() { - typeobject *tp; + PyTypeObject *tp; for (tp = type_list; tp; tp = tp->tp_next) fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", @@ -92,12 +92,12 @@ get_counts() void inc_count(tp) - typeobject *tp; + PyTypeObject *tp; { if (tp->tp_alloc == 0) { /* first time; insert in linked list */ if (tp->tp_next != NULL) /* sanity check */ - fatal("XXX inc_count sanity check"); + Py_FatalError("XXX inc_count sanity check"); tp->tp_next = type_list; type_list = tp; } @@ -108,35 +108,35 @@ inc_count(tp) #endif #ifndef MS_COREDLL -object * -newobject(tp) - typeobject *tp; +PyObject * +_PyObject_New(tp) + PyTypeObject *tp; #else -object * -newobject(tp,op) - typeobject *tp; +PyObject * +_PyObject_New(tp,op) + PyTypeObject *tp; PyObject *op; #endif { #ifndef MS_COREDLL - object *op = (object *) malloc(tp->tp_basicsize); + PyObject *op = (PyObject *) malloc(tp->tp_basicsize); #endif if (op == NULL) - return err_nomem(); + return PyErr_NoMemory(); op->ob_type = tp; - NEWREF(op); + _Py_NewReference(op); return op; } #ifndef MS_COREDLL varobject * -newvarobject(tp, size) - typeobject *tp; +_PyObject_NewVar(tp, size) + PyTypeObject *tp; int size; #else varobject * -newvarobject(tp, size, op) - typeobject *tp; +_PyObject_NewVar(tp, size, op) + PyTypeObject *tp; int size; varobject *op; #endif @@ -146,21 +146,21 @@ newvarobject(tp, size, op) malloc(tp->tp_basicsize + size * tp->tp_itemsize); #endif if (op == NULL) - return (varobject *)err_nomem(); + return (varobject *)PyErr_NoMemory(); op->ob_type = tp; op->ob_size = size; - NEWREF(op); + _Py_NewReference(op); return op; } int -printobject(op, fp, flags) - object *op; +PyObject_Print(op, fp, flags) + PyObject *op; FILE *fp; int flags; { int ret = 0; - if (sigcheck()) + if (PyErr_CheckSignals()) return -1; if (op == NULL) { fprintf(fp, "<nil>"); @@ -175,22 +175,23 @@ printobject(op, fp, flags) op->ob_type->tp_name, (long)op); } else { - object *s; - if (flags & PRINT_RAW) - s = strobject(op); + PyObject *s; + if (flags & Py_PRINT_RAW) + s = PyObject_Str(op); else - s = reprobject(op); + s = PyObject_Repr(op); if (s == NULL) ret = -1; - else if (!is_stringobject(s)) { - err_setstr(TypeError, + else if (!PyString_Check(s)) { + PyErr_SetString(PyExc_TypeError, "repr not string"); ret = -1; } else { - fprintf(fp, "%s", getstringvalue(s)); + fprintf(fp, "%s", + PyString_AsString(s)); } - XDECREF(s); + Py_XDECREF(s); } } else @@ -198,7 +199,7 @@ printobject(op, fp, flags) } if (ret == 0) { if (ferror(fp)) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); clearerr(fp); ret = -1; } @@ -206,104 +207,104 @@ printobject(op, fp, flags) return ret; } -object * -reprobject(v) - object *v; +PyObject * +PyObject_Repr(v) + PyObject *v; { - if (sigcheck()) + if (PyErr_CheckSignals()) return NULL; if (v == NULL) - return newstringobject("<NULL>"); + return PyString_FromString("<NULL>"); else if (v->ob_type->tp_repr == NULL) { char buf[120]; sprintf(buf, "<%.80s object at %lx>", v->ob_type->tp_name, (long)v); - return newstringobject(buf); + return PyString_FromString(buf); } else return (*v->ob_type->tp_repr)(v); } -object * -strobject(v) - object *v; +PyObject * +PyObject_Str(v) + PyObject *v; { if (v == NULL) - return newstringobject("<NULL>"); - else if (is_stringobject(v)) { - INCREF(v); + return PyString_FromString("<NULL>"); + else if (PyString_Check(v)) { + Py_INCREF(v); return v; } else if (v->ob_type->tp_str != NULL) return (*v->ob_type->tp_str)(v); else { - object *func; - object *res; - if (!is_instanceobject(v) || - (func = getattr(v, "__str__")) == NULL) { - err_clear(); - return reprobject(v); + PyObject *func; + PyObject *res; + if (!PyInstance_Check(v) || + (func = PyObject_GetAttrString(v, "__str__")) == NULL) { + PyErr_Clear(); + return PyObject_Repr(v); } - res = call_object(func, (object *)NULL); - DECREF(func); + res = PyEval_CallObject(func, (PyObject *)NULL); + Py_DECREF(func); return res; } } -static object * +static PyObject * do_cmp(v, w) - object *v, *w; + PyObject *v, *w; { /* __rcmp__ actually won't be called unless __cmp__ isn't defined, because the check in cmpobject() reverses the objects first. - This is intentional -- it makes no sense to define cmp(x,y) different - than -cmp(y,x). */ - if (is_instanceobject(v) || is_instanceobject(w)) - return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp); - return newintobject((long)cmpobject(v, w)); + This is intentional -- it makes no sense to define cmp(x,y) + different than -cmp(y,x). */ + if (PyInstance_Check(v) || PyInstance_Check(w)) + return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); + return PyInt_FromLong((long)PyObject_Compare(v, w)); } int -cmpobject(v, w) - object *v, *w; +PyObject_Compare(v, w) + PyObject *v, *w; { - typeobject *tp; + PyTypeObject *tp; if (v == w) return 0; if (v == NULL) return -1; if (w == NULL) return 1; - if (is_instanceobject(v) || is_instanceobject(w)) { - object *res; + if (PyInstance_Check(v) || PyInstance_Check(w)) { + PyObject *res; int c; - if (!is_instanceobject(v)) - return -cmpobject(w, v); + if (!PyInstance_Check(v)) + return -PyObject_Compare(w, v); res = do_cmp(v, w); if (res == NULL) { - err_clear(); + PyErr_Clear(); return (v < w) ? -1 : 1; } - if (!is_intobject(res)) { - DECREF(res); + if (!PyInt_Check(res)) { + Py_DECREF(res); return (v < w) ? -1 : 1; } - c = getintvalue(res); - DECREF(res); + c = PyInt_AsLong(res); + Py_DECREF(res); return (c < 0) ? -1 : (c > 0) ? 1 : 0; } if ((tp = v->ob_type) != w->ob_type) { if (tp->tp_as_number != NULL && w->ob_type->tp_as_number != NULL) { - if (coerce(&v, &w) != 0) { - err_clear(); + if (PyNumber_Coerce(&v, &w) != 0) { + PyErr_Clear(); /* XXX Should report the error, XXX but the interface isn't there... */ } else { int cmp = (*v->ob_type->tp_compare)(v, w); - DECREF(v); - DECREF(w); + Py_DECREF(v); + Py_DECREF(w); return cmp; } } @@ -315,36 +316,36 @@ cmpobject(v, w) } long -hashobject(v) - object *v; +PyObject_Hash(v) + PyObject *v; { - typeobject *tp = v->ob_type; + PyTypeObject *tp = v->ob_type; if (tp->tp_hash != NULL) return (*tp->tp_hash)(v); if (tp->tp_compare == NULL) return (long) v; /* Use address as hash value */ /* If there's a cmp but no hash defined, the object can't be hashed */ - err_setstr(TypeError, "unhashable type"); + PyErr_SetString(PyExc_TypeError, "unhashable type"); return -1; } -object * -getattr(v, name) - object *v; +PyObject * +PyObject_GetAttrString(v, name) + PyObject *v; char *name; { if (v->ob_type->tp_getattro != NULL) { - object *w, *res; + PyObject *w, *res; w = PyString_InternFromString(name); if (w == NULL) return NULL; res = (*v->ob_type->tp_getattro)(v, w); - XDECREF(w); + Py_XDECREF(w); return res; } if (v->ob_type->tp_getattr == NULL) { - err_setstr(AttributeError, "attribute-less object"); + PyErr_SetString(PyExc_AttributeError, "attribute-less object"); return NULL; } else { @@ -353,42 +354,42 @@ getattr(v, name) } int -hasattr(v, name) - object *v; +PyObject_HasAttrString(v, name) + PyObject *v; char *name; { - object *res = getattr(v, name); + PyObject *res = PyObject_GetAttrString(v, name); if (res != NULL) { - DECREF(res); + Py_DECREF(res); return 1; } - err_clear(); + PyErr_Clear(); return 0; } int -setattr(v, name, w) - object *v; +PyObject_SetAttrString(v, name, w) + PyObject *v; char *name; - object *w; + PyObject *w; { if (v->ob_type->tp_setattro != NULL) { - object *s; + PyObject *s; int res; s = PyString_InternFromString(name); if (s == NULL) return -1; res = (*v->ob_type->tp_setattro)(v, s, w); - XDECREF(s); + Py_XDECREF(s); return res; } if (v->ob_type->tp_setattr == NULL) { if (v->ob_type->tp_getattr == NULL) - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "attribute-less object (assign or del)"); else - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "object has read-only attributes"); return -1; } @@ -401,11 +402,11 @@ setattr(v, name, w) Return -1 if an error occurred */ int -testbool(v) - object *v; +PyObject_IsTrue(v) + PyObject *v; { int res; - if (v == None) + if (v == Py_None) res = 0; else if (v->ob_type->tp_as_number != NULL) res = (*v->ob_type->tp_as_number->nb_nonzero)(v); @@ -427,16 +428,16 @@ testbool(v) */ int -coerce(pv, pw) - object **pv, **pw; +PyNumber_Coerce(pv, pw) + PyObject **pv, **pw; { - register object *v = *pv; - register object *w = *pw; + register PyObject *v = *pv; + register PyObject *w = *pw; int res; - if (v->ob_type == w->ob_type && !is_instanceobject(v)) { - INCREF(v); - INCREF(w); + if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { + Py_INCREF(v); + Py_INCREF(w); return 0; } if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { @@ -449,7 +450,7 @@ coerce(pv, pw) if (res <= 0) return res; } - err_setstr(TypeError, "number coercion failed"); + PyErr_SetString(PyExc_TypeError, "number coercion failed"); return -1; } @@ -457,26 +458,26 @@ coerce(pv, pw) /* Test whether an object can be called */ int -callable(x) - object *x; +PyCallable_Check(x) + PyObject *x; { if (x == NULL) return 0; if (x->ob_type->tp_call != NULL || - is_funcobject(x) || - is_instancemethodobject(x) || - is_methodobject(x) || - is_classobject(x)) + PyFunction_Check(x) || + PyMethod_Check(x) || + PyCFunction_Check(x) || + PyClass_Check(x)) return 1; - if (is_instanceobject(x)) { - object *call = getattr(x, "__call__"); + if (PyInstance_Check(x)) { + PyObject *call = PyObject_GetAttrString(x, "__call__"); if (call == NULL) { - err_clear(); + PyErr_Clear(); return 0; } /* Could test recursively but don't, for fear of endless recursion if some joker sets self.__call__ = self */ - DECREF(call); + Py_DECREF(call); return 1; } return 0; @@ -490,15 +491,15 @@ so there is exactly one (which is indestructible, by the way). */ /* ARGSUSED */ -static object * +static PyObject * none_repr(op) - object *op; + PyObject *op; { - return newstringobject("None"); + return PyString_FromString("None"); } -static typeobject Notype = { - OB_HEAD_INIT(&Typetype) +static PyTypeObject PyNothing_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "None", 0, @@ -515,20 +516,20 @@ static typeobject Notype = { 0, /*tp_hash */ }; -object NoObject = { - OB_HEAD_INIT(&Notype) +PyObject _Py_NoneStruct = { + PyObject_HEAD_INIT(&PyNothing_Type) }; #ifdef Py_TRACE_REFS -static object refchain = {&refchain, &refchain}; +static PyObject refchain = {&refchain, &refchain}; void -NEWREF(op) - object *op; +_Py_NewReference(op) + PyObject *op; { - ref_total++; + _Py_RefTotal++; op->ob_refcnt = 1; op->_ob_next = refchain._ob_next; op->_ob_prev = &refchain; @@ -540,22 +541,22 @@ NEWREF(op) } void -UNREF(op) - register object *op; +_Py_ForgetReference(op) + register PyObject *op; { - register object *p; + register PyObject *p; if (op->ob_refcnt < 0) - fatal("UNREF negative refcnt"); + Py_FatalError("UNREF negative refcnt"); if (op == &refchain || op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) - fatal("UNREF invalid object"); + Py_FatalError("UNREF invalid object"); #ifdef SLOW_UNREF_CHECK for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { if (p == op) break; } if (p == &refchain) /* Not found */ - fatal("UNREF unknown object"); + Py_FatalError("UNREF unknown object"); #endif op->_ob_next->_ob_prev = op->_ob_prev; op->_ob_prev->_ob_next = op->_ob_next; @@ -566,11 +567,11 @@ UNREF(op) } void -DELREF(op) - object *op; +_Py_Dealloc(op) + PyObject *op; { destructor dealloc = op->ob_type->tp_dealloc; - UNREF(op); + _Py_ForgetReference(op); op->ob_type = NULL; (*dealloc)(op); } @@ -579,14 +580,14 @@ void _Py_PrintReferences(fp) FILE *fp; { - object *op; + PyObject *op; fprintf(fp, "Remaining objects (except strings referenced once):\n"); for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { - if (op->ob_refcnt == 1 && is_stringobject(op)) + if (op->ob_refcnt == 1 && PyString_Check(op)) continue; /* Will be printed elsewhere */ fprintf(fp, "[%d] ", op->ob_refcnt); - if (printobject(op, fp, 0) != 0) - err_clear(); + if (PyObject_Print(op, fp, 0) != 0) + PyErr_Clear(); putc('\n', fp); } } @@ -630,4 +631,4 @@ PyTypeObject *_Py_cobject_hack = &PyCObject_Type; /* Hack to force loading of abstract.o */ -int (*_Py_abstract_hack) FPROTO((PyObject *)) = &PyObject_Length; +int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index ee7af08..0bf643f 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -31,10 +31,10 @@ PERFORMANCE OF THIS SOFTWARE. /* Range object implementation */ -#include "allobjects.h" +#include "Python.h" typedef struct { - OB_HEAD + PyObject_HEAD long start; long step; long len; @@ -42,39 +42,40 @@ typedef struct { } rangeobject; -object * -newrangeobject(start, len, step, reps) +PyObject * +PyRange_New(start, len, step, reps) long start, len, step; int reps; { - rangeobject *obj = NEWOBJ(rangeobject, &Rangetype); + rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); obj->start = start; obj->len = len; obj->step = step; obj->reps = reps; - return (object *) obj; + return (PyObject *) obj; } static void range_dealloc(r) rangeobject *r; { - DEL(r); + PyMem_DEL(r); } -static object * +static PyObject * range_item(r, i) rangeobject *r; int i; { if (i < 0 || i >= r->len * r->reps) { - err_setstr(IndexError, "range object index out of range"); + PyErr_SetString(PyExc_IndexError, + "range object index out of range"); return NULL; } - return newintobject(r->start + (i % r->len) * r->step); + return PyInt_FromLong(r->start + (i % r->len) * r->step); } static int @@ -107,7 +108,7 @@ range_print(r, fp, flags) return 0; } -static object * +static PyObject * range_repr(r) rangeobject *r; { @@ -117,33 +118,33 @@ range_repr(r) r->start + r->len * r->step, r->step, r->reps); - return newstringobject(buf); + return PyString_FromString(buf); } -static object * +static PyObject * range_concat(r, obj) rangeobject *r; - object *obj; + PyObject *obj; { - err_setstr(TypeError, "cannot concatenate range objects"); + PyErr_SetString(PyExc_TypeError, "cannot concatenate range objects"); return NULL; } -static object * +static PyObject * range_repeat(r, n) rangeobject *r; int n; { if (n < 0) - return (object *) newrangeobject(0, 0, 1, 1); + return (PyObject *) PyRange_New(0, 0, 1, 1); else if (n == 1) { - INCREF(r); - return (object *) r; + Py_INCREF(r); + return (PyObject *) r; } else - return (object *) newrangeobject( + return (PyObject *) PyRange_New( r->start, r->len, r->step, @@ -167,13 +168,14 @@ range_compare(r1, r2) return r1->reps - r2->reps; } -static object * +static PyObject * range_slice(r, low, high) rangeobject *r; int low, high; { if (r->reps != 1) { - err_setstr(TypeError, "cannot slice a replicated range"); + PyErr_SetString(PyExc_TypeError, + "cannot slice a replicated range"); return NULL; } if (low < 0) @@ -188,55 +190,54 @@ range_slice(r, low, high) high = r->len; if (low == 0 && high == r->len) { - INCREF(r); - return (object *) r; + Py_INCREF(r); + return (PyObject *) r; } - return (object *) newrangeobject( + return (PyObject *) PyRange_New( low * r->step + r->start, high - low, r->step, 1); } -static object * +static PyObject * range_tolist(self, args) rangeobject *self; -object *args; +PyObject *args; { - object *thelist; + PyObject *thelist; int j; int len = self->len * self->reps; - if (! getargs(args, "")) + if (! PyArg_Parse(args, "")) return NULL; - if ((thelist = newlistobject(len)) == NULL) + if ((thelist = PyList_New(len)) == NULL) return NULL; for (j = 0; j < len; ++j) - if ((setlistitem(thelist, j, - (object *) newintobject( - self->start + (j % self->len) * self->step))) < 0) + if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong( + self->start + (j % self->len) * self->step))) < 0) return NULL; return thelist; } -static object * +static PyObject * range_getattr(r, name) rangeobject *r; char *name; { - static struct methodlist range_methods[] = { - {"tolist", (method)range_tolist}, + static PyMethodDef range_methods[] = { + {"tolist", (PyCFunction)range_tolist}, {NULL, NULL} }; - return findmethod(range_methods, (object *) r, name); + return Py_FindMethod(range_methods, (PyObject *) r, name); } -static sequence_methods range_as_sequence = { +static PySequenceMethods range_as_sequence = { (inquiry)range_length, /*sq_length*/ (binaryfunc)range_concat, /*sq_concat*/ (intargfunc)range_repeat, /*sq_repeat*/ @@ -246,8 +247,8 @@ static sequence_methods range_as_sequence = { 0, /*sq_ass_slice*/ }; -typeobject Rangetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyRange_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, /* Number of items for varobject */ "xrange", /* Name of this type */ sizeof(rangeobject), /* Basic object size */ diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 881cdab..1f95aa1 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -31,7 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. /* String object implementation */ -#include "allobjects.h" +#include "Python.h" #include <ctype.h> @@ -47,9 +47,9 @@ int null_strings, one_strings; #endif #endif -static stringobject *characters[UCHAR_MAX + 1]; +static PyStringObject *characters[UCHAR_MAX + 1]; #ifndef DONT_SHARE_SHORT_STRINGS -static stringobject *nullstring; +static PyStringObject *nullstring; #endif /* @@ -67,33 +67,35 @@ static stringobject *nullstring; newsizedstringobject() with a NULL first argument, because in the future these routines may try to do even more sharing of objects. */ -object * -newsizedstringobject(str, size) +PyObject * +PyString_FromStringAndSize(str, size) const char *str; int size; { - register stringobject *op; + register PyStringObject *op; #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0 && (op = nullstring) != NULL) { #ifdef COUNT_ALLOCS null_strings++; #endif - INCREF(op); - return (object *)op; + Py_INCREF(op); + return (PyObject *)op; } - if (size == 1 && str != NULL && (op = characters[*str & UCHAR_MAX]) != NULL) { + if (size == 1 && str != NULL && + (op = characters[*str & UCHAR_MAX]) != NULL) + { #ifdef COUNT_ALLOCS one_strings++; #endif - INCREF(op); - return (object *)op; + Py_INCREF(op); + return (PyObject *)op; } #endif /* DONT_SHARE_SHORT_STRINGS */ - op = (stringobject *) - malloc(sizeof(stringobject) + size * sizeof(char)); + op = (PyStringObject *) + malloc(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) - return err_nomem(); - op->ob_type = &Stringtype; + return PyErr_NoMemory(); + op->ob_type = &PyString_Type; op->ob_size = size; #ifdef CACHE_HASH op->ob_shash = -1; @@ -101,49 +103,49 @@ newsizedstringobject(str, size) #ifdef INTERN_STRINGS op->ob_sinterned = NULL; #endif - NEWREF(op); + _Py_NewReference(op); if (str != NULL) memcpy(op->ob_sval, str, size); op->ob_sval[size] = '\0'; #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0) { nullstring = op; - INCREF(op); + Py_INCREF(op); } else if (size == 1 && str != NULL) { characters[*str & UCHAR_MAX] = op; - INCREF(op); + Py_INCREF(op); } #endif - return (object *) op; + return (PyObject *) op; } -object * -newstringobject(str) +PyObject * +PyString_FromString(str) const char *str; { register unsigned int size = strlen(str); - register stringobject *op; + register PyStringObject *op; #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0 && (op = nullstring) != NULL) { #ifdef COUNT_ALLOCS null_strings++; #endif - INCREF(op); - return (object *)op; + Py_INCREF(op); + return (PyObject *)op; } if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { #ifdef COUNT_ALLOCS one_strings++; #endif - INCREF(op); - return (object *)op; + Py_INCREF(op); + return (PyObject *)op; } #endif /* DONT_SHARE_SHORT_STRINGS */ - op = (stringobject *) - malloc(sizeof(stringobject) + size * sizeof(char)); + op = (PyStringObject *) + malloc(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) - return err_nomem(); - op->ob_type = &Stringtype; + return PyErr_NoMemory(); + op->ob_type = &PyString_Type; op->ob_size = size; #ifdef CACHE_HASH op->ob_shash = -1; @@ -151,54 +153,54 @@ newstringobject(str) #ifdef INTERN_STRINGS op->ob_sinterned = NULL; #endif - NEWREF(op); + _Py_NewReference(op); strcpy(op->ob_sval, str); #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0) { nullstring = op; - INCREF(op); + Py_INCREF(op); } else if (size == 1) { characters[*str & UCHAR_MAX] = op; - INCREF(op); + Py_INCREF(op); } #endif - return (object *) op; + return (PyObject *) op; } static void string_dealloc(op) - object *op; + PyObject *op; { - DEL(op); + PyMem_DEL(op); } int -getstringsize(op) - register object *op; +PyString_Size(op) + register PyObject *op; { - if (!is_stringobject(op)) { - err_badcall(); + if (!PyString_Check(op)) { + PyErr_BadInternalCall(); return -1; } - return ((stringobject *)op) -> ob_size; + return ((PyStringObject *)op) -> ob_size; } /*const*/ char * -getstringvalue(op) - register object *op; +PyString_AsString(op) + register PyObject *op; { - if (!is_stringobject(op)) { - err_badcall(); + if (!PyString_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return ((stringobject *)op) -> ob_sval; + return ((PyStringObject *)op) -> ob_sval; } /* Methods */ static int string_print(op, fp, flags) - stringobject *op; + PyStringObject *op; FILE *fp; int flags; { @@ -206,7 +208,7 @@ string_print(op, fp, flags) char c; int quote; /* XXX Ought to check for interrupts when writing long strings */ - if (flags & PRINT_RAW) { + if (flags & Py_PRINT_RAW) { fwrite(op->ob_sval, 1, (int) op->ob_size, fp); return 0; } @@ -230,13 +232,13 @@ string_print(op, fp, flags) return 0; } -static object * +static PyObject * string_repr(op) - register stringobject *op; + register PyStringObject *op; { /* XXX overflow? */ int newsize = 2 + 4 * op->ob_size * sizeof(char); - object *v = newsizedstringobject((char *)NULL, newsize); + PyObject *v = PyString_FromStringAndSize((char *)NULL, newsize); if (v == NULL) { return NULL; } @@ -251,7 +253,7 @@ string_repr(op) if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) quote = '"'; - p = ((stringobject *)v)->ob_sval; + p = ((PyStringObject *)v)->ob_sval; *p++ = quote; for (i = 0; i < op->ob_size; i++) { c = op->ob_sval[i]; @@ -267,45 +269,46 @@ string_repr(op) } *p++ = quote; *p = '\0'; - resizestring(&v, (int) (p - ((stringobject *)v)->ob_sval)); + _PyString_Resize( + &v, (int) (p - ((PyStringObject *)v)->ob_sval)); return v; } } static int string_length(a) - stringobject *a; + PyStringObject *a; { return a->ob_size; } -static object * +static PyObject * string_concat(a, bb) - register stringobject *a; - register object *bb; + register PyStringObject *a; + register PyObject *bb; { register unsigned int size; - register stringobject *op; - if (!is_stringobject(bb)) { - err_badarg(); + register PyStringObject *op; + if (!PyString_Check(bb)) { + PyErr_BadArgument(); return NULL; } -#define b ((stringobject *)bb) +#define b ((PyStringObject *)bb) /* Optimize cases with empty left or right operand */ if (a->ob_size == 0) { - INCREF(bb); + Py_INCREF(bb); return bb; } if (b->ob_size == 0) { - INCREF(a); - return (object *)a; + Py_INCREF(a); + return (PyObject *)a; } size = a->ob_size + b->ob_size; - op = (stringobject *) - malloc(sizeof(stringobject) + size * sizeof(char)); + op = (PyStringObject *) + malloc(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) - return err_nomem(); - op->ob_type = &Stringtype; + return PyErr_NoMemory(); + op->ob_type = &PyString_Type; op->ob_size = size; #ifdef CACHE_HASH op->ob_shash = -1; @@ -313,34 +316,34 @@ string_concat(a, bb) #ifdef INTERN_STRINGS op->ob_sinterned = NULL; #endif - NEWREF(op); + _Py_NewReference(op); memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); op->ob_sval[size] = '\0'; - return (object *) op; + return (PyObject *) op; #undef b } -static object * +static PyObject * string_repeat(a, n) - register stringobject *a; + register PyStringObject *a; register int n; { register int i; register int size; - register stringobject *op; + register PyStringObject *op; if (n < 0) n = 0; size = a->ob_size * n; if (size == a->ob_size) { - INCREF(a); - return (object *)a; + Py_INCREF(a); + return (PyObject *)a; } - op = (stringobject *) - malloc(sizeof(stringobject) + size * sizeof(char)); + op = (PyStringObject *) + malloc(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) - return err_nomem(); - op->ob_type = &Stringtype; + return PyErr_NoMemory(); + op->ob_type = &PyString_Type; op->ob_size = size; #ifdef CACHE_HASH op->ob_shash = -1; @@ -348,18 +351,18 @@ string_repeat(a, n) #ifdef INTERN_STRINGS op->ob_sinterned = NULL; #endif - NEWREF(op); + _Py_NewReference(op); for (i = 0; i < size; i += a->ob_size) memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); op->ob_sval[size] = '\0'; - return (object *) op; + return (PyObject *) op; } /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ -static object * +static PyObject * string_slice(a, i, j) - register stringobject *a; + register PyStringObject *a; register int i, j; /* May be negative! */ { if (i < 0) @@ -369,45 +372,45 @@ string_slice(a, i, j) if (j > a->ob_size) j = a->ob_size; if (i == 0 && j == a->ob_size) { /* It's the same as a */ - INCREF(a); - return (object *)a; + Py_INCREF(a); + return (PyObject *)a; } if (j < i) j = i; - return newsizedstringobject(a->ob_sval + i, (int) (j-i)); + return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); } -static object * +static PyObject * string_item(a, i) - stringobject *a; + PyStringObject *a; register int i; { int c; - object *v; + PyObject *v; if (i < 0 || i >= a->ob_size) { - err_setstr(IndexError, "string index out of range"); + PyErr_SetString(PyExc_IndexError, "string index out of range"); return NULL; } c = a->ob_sval[i] & UCHAR_MAX; - v = (object *) characters[c]; + v = (PyObject *) characters[c]; #ifdef COUNT_ALLOCS if (v != NULL) one_strings++; #endif if (v == NULL) { - v = newsizedstringobject((char *)NULL, 1); + v = PyString_FromStringAndSize((char *)NULL, 1); if (v == NULL) return NULL; - characters[c] = (stringobject *) v; - ((stringobject *)v)->ob_sval[0] = c; + characters[c] = (PyStringObject *) v; + ((PyStringObject *)v)->ob_sval[0] = c; } - INCREF(v); + Py_INCREF(v); return v; } static int string_compare(a, b) - stringobject *a, *b; + PyStringObject *a, *b; { int len_a = a->ob_size, len_b = b->ob_size; int min_len = (len_a < len_b) ? len_a : len_b; @@ -424,7 +427,7 @@ string_compare(a, b) static long string_hash(a) - stringobject *a; + PyStringObject *a; { register int len; register unsigned char *p; @@ -436,7 +439,7 @@ string_hash(a) #ifdef INTERN_STRINGS if (a->ob_sinterned != NULL) return (a->ob_shash = - ((stringobject *)(a->ob_sinterned))->ob_shash); + ((PyStringObject *)(a->ob_sinterned))->ob_shash); #endif #endif len = a->ob_size; @@ -453,7 +456,7 @@ string_hash(a) return x; } -static sequence_methods string_as_sequence = { +static PySequenceMethods string_as_sequence = { (inquiry)string_length, /*sq_length*/ (binaryfunc)string_concat, /*sq_concat*/ (intargfunc)string_repeat, /*sq_repeat*/ @@ -463,11 +466,11 @@ static sequence_methods string_as_sequence = { 0, /*sq_ass_slice*/ }; -typeobject Stringtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyString_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "string", - sizeof(stringobject), + sizeof(PyStringObject), sizeof(char), (destructor)string_dealloc, /*tp_dealloc*/ (printfunc)string_print, /*tp_print*/ @@ -489,30 +492,30 @@ typeobject Stringtype = { }; void -joinstring(pv, w) - register object **pv; - register object *w; +PyString_Concat(pv, w) + register PyObject **pv; + register PyObject *w; { - register object *v; + register PyObject *v; if (*pv == NULL) return; - if (w == NULL || !is_stringobject(*pv)) { - DECREF(*pv); + if (w == NULL || !PyString_Check(*pv)) { + Py_DECREF(*pv); *pv = NULL; return; } - v = string_concat((stringobject *) *pv, w); - DECREF(*pv); + v = string_concat((PyStringObject *) *pv, w); + Py_DECREF(*pv); *pv = v; } void -joinstring_decref(pv, w) - register object **pv; - register object *w; +PyString_ConcatAndDel(pv, w) + register PyObject **pv; + register PyObject *w; { - joinstring(pv, w); - XDECREF(w); + PyString_Concat(pv, w); + Py_XDECREF(w); } @@ -524,34 +527,34 @@ joinstring_decref(pv, w) already be known to some other part of the code... */ int -resizestring(pv, newsize) - object **pv; +_PyString_Resize(pv, newsize) + PyObject **pv; int newsize; { - register object *v; - register stringobject *sv; + register PyObject *v; + register PyStringObject *sv; v = *pv; - if (!is_stringobject(v) || v->ob_refcnt != 1) { + if (!PyString_Check(v) || v->ob_refcnt != 1) { *pv = 0; - DECREF(v); - err_badcall(); + Py_DECREF(v); + PyErr_BadInternalCall(); return -1; } /* XXX UNREF/NEWREF interface should be more symmetrical */ #ifdef Py_REF_DEBUG --_Py_RefTotal; #endif - UNREF(v); - *pv = (object *) + _Py_ForgetReference(v); + *pv = (PyObject *) realloc((char *)v, - sizeof(stringobject) + newsize * sizeof(char)); + sizeof(PyStringObject) + newsize * sizeof(char)); if (*pv == NULL) { - DEL(v); - err_nomem(); + PyMem_DEL(v); + PyErr_NoMemory(); return -1; } - NEWREF(*pv); - sv = (stringobject *) *pv; + _Py_NewReference(*pv); + sv = (PyStringObject *) *pv; sv->ob_size = newsize; sv->ob_sval[newsize] = '\0'; return 0; @@ -559,9 +562,9 @@ resizestring(pv, newsize) /* Helpers for formatstring */ -static object * +static PyObject * getnextarg(args, arglen, p_argidx) - object *args; + PyObject *args; int arglen; int *p_argidx; { @@ -571,9 +574,10 @@ getnextarg(args, arglen, p_argidx) if (arglen < 0) return args; else - return gettupleitem(args, argidx); + return PyTuple_GetItem(args, argidx); } - err_setstr(TypeError, "not enough arguments for format string"); + PyErr_SetString(PyExc_TypeError, + "not enough arguments for format string"); return NULL; } @@ -583,7 +587,7 @@ getnextarg(args, arglen, p_argidx) #define F_ALT (1<<3) #define F_ZERO (1<<4) -extern double fabs PROTO((double)); +extern double fabs Py_PROTO((double)); static int formatfloat(buf, flags, prec, type, v) @@ -591,11 +595,11 @@ formatfloat(buf, flags, prec, type, v) int flags; int prec; int type; - object *v; + PyObject *v; { char fmt[20]; double x; - if (!getargs(v, "d;float argument required", &x)) + if (!PyArg_Parse(v, "d;float argument required", &x)) return -1; if (prec < 0) prec = 6; @@ -614,11 +618,11 @@ formatint(buf, flags, prec, type, v) int flags; int prec; int type; - object *v; + PyObject *v; { char fmt[20]; long x; - if (!getargs(v, "l;int argument required", &x)) + if (!PyArg_Parse(v, "l;int argument required", &x)) return -1; if (prec < 0) prec = 1; @@ -630,14 +634,14 @@ formatint(buf, flags, prec, type, v) static int formatchar(buf, v) char *buf; - object *v; + PyObject *v; { - if (is_stringobject(v)) { - if (!getargs(v, "c;%c requires int or char", &buf[0])) + if (PyString_Check(v)) { + if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0])) return -1; } else { - if (!getargs(v, "b;%c requires int or char", &buf[0])) + if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0])) return -1; } buf[1] = '\0'; @@ -647,29 +651,29 @@ formatchar(buf, v) /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ -object * -formatstring(format, args) - object *format; - object *args; +PyObject * +PyString_Format(format, args) + PyObject *format; + PyObject *args; { char *fmt, *res; int fmtcnt, rescnt, reslen, arglen, argidx; int args_owned = 0; - object *result; - object *dict = NULL; - if (format == NULL || !is_stringobject(format) || args == NULL) { - err_badcall(); + PyObject *result; + PyObject *dict = NULL; + if (format == NULL || !PyString_Check(format) || args == NULL) { + PyErr_BadInternalCall(); return NULL; } - fmt = getstringvalue(format); - fmtcnt = getstringsize(format); + fmt = PyString_AsString(format); + fmtcnt = PyString_Size(format); reslen = rescnt = fmtcnt + 100; - result = newsizedstringobject((char *)NULL, reslen); + result = PyString_FromStringAndSize((char *)NULL, reslen); if (result == NULL) return NULL; - res = getstringvalue(result); - if (is_tupleobject(args)) { - arglen = gettuplesize(args); + res = PyString_AsString(result); + if (PyTuple_Check(args)) { + arglen = PyTuple_Size(args); argidx = 0; } else { @@ -683,9 +687,10 @@ formatstring(format, args) if (--rescnt < 0) { rescnt = fmtcnt + 100; reslen += rescnt; - if (resizestring(&result, reslen) < 0) + if (_PyString_Resize(&result, reslen) < 0) return NULL; - res = getstringvalue(result) + reslen - rescnt; + res = PyString_AsString(result) + + reslen - rescnt; --rescnt; } *res++ = *fmt++; @@ -698,8 +703,8 @@ formatstring(format, args) int size = 0; int c = '\0'; int fill; - object *v = NULL; - object *temp = NULL; + PyObject *v = NULL; + PyObject *temp = NULL; char *buf; int sign; int len; @@ -708,10 +713,10 @@ formatstring(format, args) if (*fmt == '(') { char *keystart; int keylen; - object *key; + PyObject *key; if (dict == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "format requires a mapping"); goto error; } @@ -723,19 +728,20 @@ formatstring(format, args) keylen = fmt - keystart; ++fmt; if (fmtcnt < 0) { - err_setstr(ValueError, + PyErr_SetString(PyExc_ValueError, "incomplete format key"); goto error; } - key = newsizedstringobject(keystart, keylen); + key = PyString_FromStringAndSize(keystart, + keylen); if (key == NULL) goto error; if (args_owned) { - DECREF(args); + Py_DECREF(args); args_owned = 0; } args = PyObject_GetItem(dict, key); - DECREF(key); + Py_DECREF(key); if (args == NULL) { goto error; } @@ -757,11 +763,12 @@ formatstring(format, args) v = getnextarg(args, arglen, &argidx); if (v == NULL) goto error; - if (!is_intobject(v)) { - err_setstr(TypeError, "* wants int"); + if (!PyInt_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "* wants int"); goto error; } - width = getintvalue(v); + width = PyInt_AsLong(v); if (width < 0) width = 0; if (--fmtcnt >= 0) @@ -774,8 +781,9 @@ formatstring(format, args) if (!isdigit(c)) break; if ((width*10) / 10 != width) { - err_setstr(ValueError, - "width too big"); + PyErr_SetString( + PyExc_ValueError, + "width too big"); goto error; } width = width*10 + (c - '0'); @@ -789,12 +797,13 @@ formatstring(format, args) v = getnextarg(args, arglen, &argidx); if (v == NULL) goto error; - if (!is_intobject(v)) { - err_setstr(TypeError, - "* wants int"); + if (!PyInt_Check(v)) { + PyErr_SetString( + PyExc_TypeError, + "* wants int"); goto error; } - prec = getintvalue(v); + prec = PyInt_AsLong(v); if (prec < 0) prec = 0; if (--fmtcnt >= 0) @@ -807,7 +816,8 @@ formatstring(format, args) if (!isdigit(c)) break; if ((prec*10) / 10 != prec) { - err_setstr(ValueError, + PyErr_SetString( + PyExc_ValueError, "prec too big"); goto error; } @@ -823,7 +833,8 @@ formatstring(format, args) } } if (fmtcnt < 0) { - err_setstr(ValueError, "incomplete format"); + PyErr_SetString(PyExc_ValueError, + "incomplete format"); goto error; } if (c != '%') { @@ -839,11 +850,11 @@ formatstring(format, args) len = 1; break; case 's': - temp = strobject(v); + temp = PyObject_Str(v); if (temp == NULL) goto error; - buf = getstringvalue(temp); - len = getstringsize(temp); + buf = PyString_AsString(temp); + len = PyString_Size(temp); if (prec >= 0 && len > prec) len = prec; break; @@ -895,7 +906,7 @@ formatstring(format, args) goto error; break; default: - err_setstr(ValueError, + PyErr_SetString(PyExc_ValueError, "unsupported format character"); goto error; } @@ -917,9 +928,10 @@ formatstring(format, args) reslen -= rescnt; rescnt = width + fmtcnt + 100; reslen += rescnt; - if (resizestring(&result, reslen) < 0) + if (_PyString_Resize(&result, reslen) < 0) return NULL; - res = getstringvalue(result) + reslen - rescnt; + res = PyString_AsString(result) + + reslen - rescnt; } if (sign) { if (fill != ' ') @@ -944,25 +956,26 @@ formatstring(format, args) *res++ = ' '; } if (dict && (argidx < arglen) && c != '%') { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "not all arguments converted"); goto error; } - XDECREF(temp); + Py_XDECREF(temp); } /* '%' */ } /* until end */ if (argidx < arglen && !dict) { - err_setstr(TypeError, "not all arguments converted"); + PyErr_SetString(PyExc_TypeError, + "not all arguments converted"); goto error; } if (args_owned) - DECREF(args); - resizestring(&result, reslen - rescnt); + Py_DECREF(args); + _PyString_Resize(&result, reslen - rescnt); return result; error: - DECREF(result); + Py_DECREF(result); if (args_owned) - DECREF(args); + Py_DECREF(args); return NULL; } diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index fd53c30..7dc4dc8 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -31,7 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. /* Tuple object implementation */ -#include "allobjects.h" +#include "Python.h" #ifndef MAXSAVESIZE #define MAXSAVESIZE 20 @@ -41,109 +41,112 @@ PERFORMANCE OF THIS SOFTWARE. /* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty tuple () of which at most one instance will be allocated. */ -static tupleobject *free_tuples[MAXSAVESIZE]; +static PyTupleObject *free_tuples[MAXSAVESIZE]; #endif #ifdef COUNT_ALLOCS int fast_tuple_allocs; int tuple_zero_allocs; #endif -object * -newtupleobject(size) +PyObject * +PyTuple_New(size) register int size; { register int i; - register tupleobject *op; + register PyTupleObject *op; if (size < 0) { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } #if MAXSAVESIZE > 0 if (size == 0 && free_tuples[0]) { op = free_tuples[0]; - INCREF(op); + Py_INCREF(op); #ifdef COUNT_ALLOCS tuple_zero_allocs++; #endif - return (object *) op; + return (PyObject *) op; } - if (0 < size && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) { - free_tuples[size] = (tupleobject *) op->ob_item[0]; + if (0 < size && size < MAXSAVESIZE && + (op = free_tuples[size]) != NULL) + { + free_tuples[size] = (PyTupleObject *) op->ob_item[0]; #ifdef COUNT_ALLOCS fast_tuple_allocs++; #endif } else #endif { - op = (tupleobject *) - malloc(sizeof(tupleobject) + size * sizeof(object *)); + op = (PyTupleObject *) malloc( + sizeof(PyTupleObject) + size * sizeof(PyObject *)); if (op == NULL) - return err_nomem(); + return PyErr_NoMemory(); } - op->ob_type = &Tupletype; + op->ob_type = &PyTuple_Type; op->ob_size = size; for (i = 0; i < size; i++) op->ob_item[i] = NULL; - NEWREF(op); + _Py_NewReference(op); #if MAXSAVESIZE > 0 if (size == 0) { free_tuples[0] = op; - INCREF(op); /* extra INCREF so that this is never freed */ + Py_INCREF(op); /* extra INCREF so that this is never freed */ } #endif - return (object *) op; + return (PyObject *) op; } int -gettuplesize(op) - register object *op; +PyTuple_Size(op) + register PyObject *op; { - if (!is_tupleobject(op)) { - err_badcall(); + if (!PyTuple_Check(op)) { + PyErr_BadInternalCall(); return -1; } else - return ((tupleobject *)op)->ob_size; + return ((PyTupleObject *)op)->ob_size; } -object * -gettupleitem(op, i) - register object *op; +PyObject * +PyTuple_GetItem(op, i) + register PyObject *op; register int i; { - if (!is_tupleobject(op)) { - err_badcall(); + if (!PyTuple_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - if (i < 0 || i >= ((tupleobject *)op) -> ob_size) { - err_setstr(IndexError, "tuple index out of range"); + if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { + PyErr_SetString(PyExc_IndexError, "tuple index out of range"); return NULL; } - return ((tupleobject *)op) -> ob_item[i]; + return ((PyTupleObject *)op) -> ob_item[i]; } int -settupleitem(op, i, newitem) - register object *op; +PyTuple_SetItem(op, i, newitem) + register PyObject *op; register int i; - object *newitem; + PyObject *newitem; { - register object *olditem; - register object **p; - if (!is_tupleobject(op)) { - XDECREF(newitem); - err_badcall(); + register PyObject *olditem; + register PyObject **p; + if (!PyTuple_Check(op)) { + Py_XDECREF(newitem); + PyErr_BadInternalCall(); return -1; } - if (i < 0 || i >= ((tupleobject *)op) -> ob_size) { - XDECREF(newitem); - err_setstr(IndexError, "tuple assignment index out of range"); + if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { + Py_XDECREF(newitem); + PyErr_SetString(PyExc_IndexError, + "tuple assignment index out of range"); return -1; } - p = ((tupleobject *)op) -> ob_item + i; + p = ((PyTupleObject *)op) -> ob_item + i; olditem = *p; *p = newitem; - XDECREF(olditem); + Py_XDECREF(olditem); return 0; } @@ -151,14 +154,14 @@ settupleitem(op, i, newitem) static void tupledealloc(op) - register tupleobject *op; + register PyTupleObject *op; { register int i; for (i = 0; i < op->ob_size; i++) - XDECREF(op->ob_item[i]); + Py_XDECREF(op->ob_item[i]); #if MAXSAVESIZE > 0 if (0 < op->ob_size && op->ob_size < MAXSAVESIZE) { - op->ob_item[0] = (object *) free_tuples[op->ob_size]; + op->ob_item[0] = (PyObject *) free_tuples[op->ob_size]; free_tuples[op->ob_size] = op; } else #endif @@ -167,7 +170,7 @@ tupledealloc(op) static int tupleprint(op, fp, flags) - tupleobject *op; + PyTupleObject *op; FILE *fp; int flags; { @@ -176,7 +179,7 @@ tupleprint(op, fp, flags) for (i = 0; i < op->ob_size; i++) { if (i > 0) fprintf(fp, ", "); - if (printobject(op->ob_item[i], fp, 0) != 0) + if (PyObject_Print(op->ob_item[i], fp, 0) != 0) return -1; } if (op->ob_size == 1) @@ -185,35 +188,35 @@ tupleprint(op, fp, flags) return 0; } -static object * +static PyObject * tuplerepr(v) - tupleobject *v; + PyTupleObject *v; { - object *s, *comma; + PyObject *s, *comma; int i; - s = newstringobject("("); - comma = newstringobject(", "); + s = PyString_FromString("("); + comma = PyString_FromString(", "); for (i = 0; i < v->ob_size && s != NULL; i++) { if (i > 0) - joinstring(&s, comma); - joinstring_decref(&s, reprobject(v->ob_item[i])); + PyString_Concat(&s, comma); + PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); } - DECREF(comma); + Py_DECREF(comma); if (v->ob_size == 1) - joinstring_decref(&s, newstringobject(",")); - joinstring_decref(&s, newstringobject(")")); + PyString_ConcatAndDel(&s, PyString_FromString(",")); + PyString_ConcatAndDel(&s, PyString_FromString(")")); return s; } static int tuplecompare(v, w) - register tupleobject *v, *w; + register PyTupleObject *v, *w; { register int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; register int i; for (i = 0; i < len; i++) { - int cmp = cmpobject(v->ob_item[i], w->ob_item[i]); + int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]); if (cmp != 0) return cmp; } @@ -222,15 +225,15 @@ tuplecompare(v, w) static long tuplehash(v) - tupleobject *v; + PyTupleObject *v; { register long x, y; register int len = v->ob_size; - register object **p; + register PyObject **p; x = 0x345678L; p = v->ob_item; while (--len >= 0) { - y = hashobject(*p++); + y = PyObject_Hash(*p++); if (y == -1) return -1; x = (1000003*x) ^ y; @@ -243,30 +246,30 @@ tuplehash(v) static int tuplelength(a) - tupleobject *a; + PyTupleObject *a; { return a->ob_size; } -static object * +static PyObject * tupleitem(a, i) - register tupleobject *a; + register PyTupleObject *a; register int i; { if (i < 0 || i >= a->ob_size) { - err_setstr(IndexError, "tuple index out of range"); + PyErr_SetString(PyExc_IndexError, "tuple index out of range"); return NULL; } - INCREF(a->ob_item[i]); + Py_INCREF(a->ob_item[i]); return a->ob_item[i]; } -static object * +static PyObject * tupleslice(a, ilow, ihigh) - register tupleobject *a; + register PyTupleObject *a; register int ilow, ihigh; { - register tupleobject *np; + register PyTupleObject *np; register int i; if (ilow < 0) ilow = 0; @@ -276,97 +279,97 @@ tupleslice(a, ilow, ihigh) ihigh = ilow; if (ilow == 0 && ihigh == a->ob_size) { /* XXX can only do this if tuples are immutable! */ - INCREF(a); - return (object *)a; + Py_INCREF(a); + return (PyObject *)a; } - np = (tupleobject *)newtupleobject(ihigh - ilow); + np = (PyTupleObject *)PyTuple_New(ihigh - ilow); if (np == NULL) return NULL; for (i = ilow; i < ihigh; i++) { - object *v = a->ob_item[i]; - INCREF(v); + PyObject *v = a->ob_item[i]; + Py_INCREF(v); np->ob_item[i - ilow] = v; } - return (object *)np; + return (PyObject *)np; } -object * -gettupleslice(op, i, j) - object *op; +PyObject * +PyTuple_GetSlice(op, i, j) + PyObject *op; int i, j; { - if (op == NULL || !is_tupleobject(op)) { - err_badcall(); + if (op == NULL || !PyTuple_Check(op)) { + PyErr_BadInternalCall(); return NULL; } - return tupleslice((tupleobject *)op, i, j); + return tupleslice((PyTupleObject *)op, i, j); } -static object * +static PyObject * tupleconcat(a, bb) - register tupleobject *a; - register object *bb; + register PyTupleObject *a; + register PyObject *bb; { register int size; register int i; - tupleobject *np; - if (!is_tupleobject(bb)) { - err_badarg(); + PyTupleObject *np; + if (!PyTuple_Check(bb)) { + PyErr_BadArgument(); return NULL; } -#define b ((tupleobject *)bb) +#define b ((PyTupleObject *)bb) size = a->ob_size + b->ob_size; - np = (tupleobject *) newtupleobject(size); + np = (PyTupleObject *) PyTuple_New(size); if (np == NULL) { return NULL; } for (i = 0; i < a->ob_size; i++) { - object *v = a->ob_item[i]; - INCREF(v); + PyObject *v = a->ob_item[i]; + Py_INCREF(v); np->ob_item[i] = v; } for (i = 0; i < b->ob_size; i++) { - object *v = b->ob_item[i]; - INCREF(v); + PyObject *v = b->ob_item[i]; + Py_INCREF(v); np->ob_item[i + a->ob_size] = v; } - return (object *)np; + return (PyObject *)np; #undef b } -static object * +static PyObject * tuplerepeat(a, n) - tupleobject *a; + PyTupleObject *a; int n; { int i, j; int size; - tupleobject *np; - object **p; + PyTupleObject *np; + PyObject **p; if (n < 0) n = 0; if (a->ob_size*n == a->ob_size) { /* Since tuples are immutable, we can return a shared copy in this case */ - INCREF(a); - return (object *)a; + Py_INCREF(a); + return (PyObject *)a; } size = a->ob_size * n; - np = (tupleobject *) newtupleobject(size); + np = (PyTupleObject *) PyTuple_New(size); if (np == NULL) return NULL; p = np->ob_item; for (i = 0; i < n; i++) { for (j = 0; j < a->ob_size; j++) { *p = a->ob_item[j]; - INCREF(*p); + Py_INCREF(*p); p++; } } - return (object *) np; + return (PyObject *) np; } -static sequence_methods tuple_as_sequence = { +static PySequenceMethods tuple_as_sequence = { (inquiry)tuplelength, /*sq_length*/ (binaryfunc)tupleconcat, /*sq_concat*/ (intargfunc)tuplerepeat, /*sq_repeat*/ @@ -376,12 +379,12 @@ static sequence_methods tuple_as_sequence = { 0, /*sq_ass_slice*/ }; -typeobject Tupletype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyTuple_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "tuple", - sizeof(tupleobject) - sizeof(object *), - sizeof(object *), + sizeof(PyTupleObject) - sizeof(PyObject *), + sizeof(PyObject *), (destructor)tupledealloc, /*tp_dealloc*/ (printfunc)tupleprint, /*tp_print*/ 0, /*tp_getattr*/ @@ -404,21 +407,21 @@ typeobject Tupletype = { front, otherwise it will grow or shrink at the end. */ int -resizetuple(pv, newsize, last_is_sticky) - object **pv; +_PyTuple_Resize(pv, newsize, last_is_sticky) + PyObject **pv; int newsize; int last_is_sticky; { - register tupleobject *v; - register tupleobject *sv; + register PyTupleObject *v; + register PyTupleObject *sv; int i; int sizediff; - v = (tupleobject *) *pv; - if (v == NULL || !is_tupleobject(v) || v->ob_refcnt != 1) { + v = (PyTupleObject *) *pv; + if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) { *pv = 0; - DECREF(v); - err_badcall(); + Py_DECREF(v); + PyErr_BadInternalCall(); return -1; } sizediff = newsize - v->ob_size; @@ -428,29 +431,30 @@ resizetuple(pv, newsize, last_is_sticky) #ifdef Py_REF_DEBUG --_Py_RefTotal; #endif - UNREF(v); + _Py_ForgetReference(v); if (last_is_sticky && sizediff < 0) { - /* shrinking: move entries to the front and zero moved entries */ + /* shrinking: + move entries to the front and zero moved entries */ for (i = 0; i < newsize; i++) { - XDECREF(v->ob_item[i]); + Py_XDECREF(v->ob_item[i]); v->ob_item[i] = v->ob_item[i - sizediff]; v->ob_item[i - sizediff] = NULL; } } for (i = newsize; i < v->ob_size; i++) { - XDECREF(v->ob_item[i]); + Py_XDECREF(v->ob_item[i]); v->ob_item[i] = NULL; } - sv = (tupleobject *) + sv = (PyTupleObject *) realloc((char *)v, - sizeof(tupleobject) + newsize * sizeof(object *)); - *pv = (object *) sv; + sizeof(PyTupleObject) + newsize * sizeof(PyObject *)); + *pv = (PyObject *) sv; if (sv == NULL) { - DEL(v); - err_nomem(); + PyMem_DEL(v); + PyErr_NoMemory(); return -1; } - NEWREF(sv); + _Py_NewReference(sv); for (i = sv->ob_size; i < newsize; i++) sv->ob_item[i] = NULL; if (last_is_sticky && sizediff > 0) { diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6ec69af..abf8e22 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -31,44 +31,44 @@ PERFORMANCE OF THIS SOFTWARE. /* Type object implementation */ -#include "allobjects.h" +#include "Python.h" /* Type object implementation */ -static object * +static PyObject * type_getattr(t, name) - typeobject *t; + PyTypeObject *t; char *name; { if (strcmp(name, "__name__") == 0) - return newstringobject(t->tp_name); + return PyString_FromString(t->tp_name); if (strcmp(name, "__doc__") == 0) { char *doc = t->tp_doc; if (doc != NULL) - return newstringobject(doc); - INCREF(None); - return None; + return PyString_FromString(doc); + Py_INCREF(Py_None); + return Py_None; } if (strcmp(name, "__members__") == 0) - return mkvalue("[ss]", "__doc__", "__name__"); - err_setstr(AttributeError, name); + return Py_BuildValue("[ss]", "__doc__", "__name__"); + PyErr_SetString(PyExc_AttributeError, name); return NULL; } -static object * +static PyObject * type_repr(v) - typeobject *v; + PyTypeObject *v; { char buf[100]; sprintf(buf, "<type '%.80s'>", v->tp_name); - return newstringobject(buf); + return PyString_FromString(buf); } -typeobject Typetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyType_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, /* Number of items for varobject */ "type", /* Name of this type */ - sizeof(typeobject), /* Basic object size */ + sizeof(PyTypeObject), /* Basic object size */ 0, /* Item size for varobject */ 0, /*tp_dealloc*/ 0, /*tp_print*/ |