From 7ce3694a527afe425a2b9df65c049b0ef4e75960 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Fri, 24 Aug 2001 18:34:26 +0000 Subject: repr's converted to using PyString_FromFormat() instead of sprintf'ing into a hardcoded char* buffer. Closes patch #454743. --- Objects/bufferobject.c | 20 +++++++------------- Objects/cellobject.c | 12 +++++------- Objects/classobject.c | 33 ++++++++++++++------------------- Objects/descrobject.c | 18 ++++++++---------- Objects/fileobject.c | 12 +++++------- Objects/funcobject.c | 11 ++++------- Objects/methodobject.c | 14 ++++++-------- Objects/moduleobject.c | 19 ++----------------- Objects/object.c | 10 ++++------ Objects/rangeobject.c | 38 +++++++++++++++++++------------------- Objects/typeobject.c | 44 +++++++++++++++++++++----------------------- 11 files changed, 95 insertions(+), 136 deletions(-) diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c index 4c213ce..242261f 100644 --- a/Objects/bufferobject.c +++ b/Objects/bufferobject.c @@ -184,28 +184,22 @@ buffer_compare(PyBufferObject *self, PyBufferObject *other) static PyObject * buffer_repr(PyBufferObject *self) { - char buf[300]; char *status = self->b_readonly ? "read-only" : "read-write"; if ( self->b_base == NULL ) - { - sprintf(buf, "<%s buffer ptr %p, size %d at %p>", - status, - self->b_ptr, - self->b_size, - self); - } + return PyString_FromFormat("<%s buffer ptr %p, size %d at %p>", + status, + self->b_ptr, + self->b_size, + self); else - { - sprintf(buf, "<%s buffer for %p, ptr %p, size %d at %p>", + return PyString_FromFormat( + "<%s buffer for %p, ptr %p, size %d at %p>", status, self->b_base, self->b_ptr, self->b_size, self); - } - - return PyString_FromString(buf); } static long diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 9a36776..47e5174 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -62,14 +62,12 @@ cell_compare(PyCellObject *a, PyCellObject *b) static PyObject * cell_repr(PyCellObject *op) { - char buf[256]; - if (op->ob_ref == NULL) - sprintf(buf, "", op); - else - sprintf(buf, "", - op, op->ob_ref->ob_type->tp_name, op->ob_ref); - return PyString_FromString(buf); + return PyString_FromFormat("", op); + + return PyString_FromFormat("", + op, op->ob_ref->ob_type->tp_name, + op->ob_ref); } static int diff --git a/Objects/classobject.c b/Objects/classobject.c index 0f39d4b..f1145a4 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -313,19 +313,17 @@ static PyObject * class_repr(PyClassObject *op) { PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); - char buf[140]; char *name; if (op->cl_name == NULL || !PyString_Check(op->cl_name)) name = "?"; else name = PyString_AsString(op->cl_name); if (mod == NULL || !PyString_Check(mod)) - sprintf(buf, "", name, op); + return PyString_FromFormat("", name, op); else - sprintf(buf, "", - PyString_AsString(mod), - name, op); - return PyString_FromString(buf); + return PyString_FromFormat("", + PyString_AsString(mod), + name, op); } static PyObject * @@ -776,7 +774,6 @@ instance_repr(PyInstanceObject *inst) reprstr = PyString_InternFromString("__repr__"); func = instance_getattr(inst, reprstr); if (func == NULL) { - char buf[140]; PyObject *classname = inst->in_class->cl_name; PyObject *mod = PyDict_GetItemString( inst->in_class->cl_dict, "__module__"); @@ -787,13 +784,12 @@ instance_repr(PyInstanceObject *inst) cname = "?"; PyErr_Clear(); if (mod == NULL || !PyString_Check(mod)) - sprintf(buf, "", - cname, inst); + return PyString_FromFormat("", + cname, inst); else - sprintf(buf, "<%.50s.%.50s instance at %p>", - PyString_AsString(mod), - cname, inst); - return PyString_FromString(buf); + return PyString_FromFormat("<%s.%s instance at %p>", + PyString_AsString(mod), + cname, inst); } res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); @@ -2042,7 +2038,6 @@ instancemethod_compare(PyMethodObject *a, PyMethodObject *b) static PyObject * instancemethod_repr(PyMethodObject *a) { - char buffer[240]; PyObject *self = a->im_self; PyObject *func = a->im_func; PyObject *klass = a->im_class; @@ -2072,8 +2067,8 @@ instancemethod_repr(PyMethodObject *a) sklassname = PyString_AS_STRING(klassname); } if (self == NULL) - sprintf(buffer, "", - sklassname, sfuncname); + result = PyString_FromFormat("", + sklassname, sfuncname); else { /* XXX Shouldn't use repr() here! */ PyObject *selfrepr = PyObject_Repr(self); @@ -2083,11 +2078,11 @@ instancemethod_repr(PyMethodObject *a) Py_DECREF(selfrepr); goto fail; } - sprintf(buffer, "", - sklassname, sfuncname, PyString_AS_STRING(selfrepr)); + result = PyString_FromFormat("", + sklassname, sfuncname, + PyString_AS_STRING(selfrepr)); Py_DECREF(selfrepr); } - result = PyString_FromString(buffer); fail: Py_XDECREF(funcname); Py_XDECREF(klassname); diff --git a/Objects/descrobject.c b/Objects/descrobject.c index efed1c0..44b20e2 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -55,38 +55,36 @@ descr_name(PyDescrObject *descr) static PyObject * descr_repr(PyDescrObject *descr, char *format) { - char buffer[500]; - - sprintf(buffer, format, descr_name(descr), descr->d_type->tp_name); - return PyString_FromString(buffer); + return PyString_FromFormat(format, descr_name(descr), + descr->d_type->tp_name); } static PyObject * method_repr(PyMethodDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - ""); + ""); } static PyObject * member_repr(PyMemberDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - ""); + ""); } static PyObject * getset_repr(PyGetSetDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - ""); + ""); } static PyObject * wrapper_repr(PyWrapperDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - ""); + ""); } static int @@ -100,8 +98,8 @@ descr_check(PyDescrObject *descr, PyObject *obj, PyTypeObject *type, } if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) { PyErr_Format(PyExc_TypeError, - "descriptor '%.200s' for '%.100s' objects " - "doesn't apply to '%.100s' object", + "descriptor '%s' for '%s' objects " + "doesn't apply to '%s' object", descr_name((PyDescrObject *)descr), descr->d_type->tp_name, obj->ob_type->tp_name); diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 0bb2f25..e18e2a2 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -179,13 +179,11 @@ file_dealloc(PyFileObject *f) static PyObject * file_repr(PyFileObject *f) { - char buf[300]; - sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>", - f->f_fp == NULL ? "closed" : "open", - PyString_AsString(f->f_name), - PyString_AsString(f->f_mode), - f); - return PyString_FromString(buf); + return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", + f->f_fp == NULL ? "closed" : "open", + PyString_AsString(f->f_name), + PyString_AsString(f->f_mode), + f); } static PyObject * diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 6532e58..6f56bf6 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -239,14 +239,11 @@ func_dealloc(PyFunctionObject *op) static PyObject* func_repr(PyFunctionObject *op) { - char buf[140]; if (op->func_name == Py_None) - sprintf(buf, "", op); - else - sprintf(buf, "", - PyString_AsString(op->func_name), - op); - return PyString_FromString(buf); + return PyString_FromFormat("", op); + return PyString_FromFormat("", + PyString_AsString(op->func_name), + op); } static int diff --git a/Objects/methodobject.c b/Objects/methodobject.c index cdba350..e766ba5 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -172,15 +172,13 @@ static struct getsetlist meth_getsets [] = { static PyObject * meth_repr(PyCFunctionObject *m) { - char buf[200]; if (m->m_self == NULL) - sprintf(buf, "", m->m_ml->ml_name); - else - sprintf(buf, - "", - m->m_ml->ml_name, m->m_self->ob_type->tp_name, - m->m_self); - return PyString_FromString(buf); + return PyString_FromFormat("", + m->m_ml->ml_name); + return PyString_FromFormat("", + m->m_ml->ml_name, + m->m_self->ob_type->tp_name, + m->m_self); } static int diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 2ccf0bf..ba81593 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -157,13 +157,8 @@ module_dealloc(PyModuleObject *m) static PyObject * module_repr(PyModuleObject *m) { - static int template1len = sizeof("") + 1; - static int template2len = sizeof("") + 1; - - char *buf; char *name; char *filename; - PyObject *rtn; name = PyModule_GetName((PyObject *)m); if (name == NULL) { @@ -173,19 +168,9 @@ module_repr(PyModuleObject *m) filename = PyModule_GetFilename((PyObject *)m); if (filename == NULL) { PyErr_Clear(); - buf = PyObject_MALLOC( - sizeof(char) * (strlen(name) + template1len)); - sprintf(buf, "", name); - } - else { - buf = PyObject_MALLOC( - sizeof(char) * (strlen(name) + strlen(filename) + - template2len)); - sprintf(buf, "", name, filename); + return PyString_FromFormat("", name); } - rtn = PyString_FromString(buf); - PyObject_FREE(buf); - return rtn; + return PyString_FromFormat("", name, filename); } /* We only need a traverse function, no clear function: If the module diff --git a/Objects/object.c b/Objects/object.c index 7e4a211..1d62690 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -222,6 +222,7 @@ void _PyGC_Dump(PyGC_Head* op) } #endif /* WITH_CYCLE_GC */ + PyObject * PyObject_Repr(PyObject *v) { @@ -235,12 +236,9 @@ PyObject_Repr(PyObject *v) #endif if (v == NULL) return PyString_FromString(""); - else if (v->ob_type->tp_repr == NULL) { - char buf[120]; - sprintf(buf, "<%.80s object at %p>", - v->ob_type->tp_name, v); - return PyString_FromString(buf); - } + else if (v->ob_type->tp_repr == NULL) + return PyString_FromFormat("<%s object at %p", + v->ob_type->tp_name, v); else { PyObject *res; res = (*v->ob_type->tp_repr)(v); diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 5ad86ed..24765f4 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -126,30 +126,30 @@ range_length(rangeobject *r) static PyObject * range_repr(rangeobject *r) { - /* buffers must be big enough to hold 3 longs + an int + - * a bit of "(xrange(...) * ...)" text. - */ - char buf1[250]; - char buf2[250]; - + PyObject *rtn; + if (r->start == 0 && r->step == 1) - sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step); + rtn = PyString_FromFormat("xrange(%ld)", + r->start + r->len * r->step); else if (r->step == 1) - sprintf(buf1, "xrange(%ld, %ld)", - r->start, - r->start + r->len * r->step); + rtn = PyString_FromFormat("xrange(%ld, %ld)", + r->start, + r->start + r->len * r->step); else - sprintf(buf1, "xrange(%ld, %ld, %ld)", - r->start, - r->start + r->len * r->step, - r->step); - - if (r->reps != 1) - sprintf(buf2, "(%s * %d)", buf1, r->reps); - - return PyString_FromString(r->reps == 1 ? buf1 : buf2); + rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)", + r->start, + r->start + r->len * r->step, + r->step); + if (r->reps != 1) { + PyObject *extra = PyString_FromFormat( + "(%s * %d)", + PyString_AS_STRING(rtn), r->reps); + Py_DECREF(rtn); + rtn = extra; + } + return rtn; } static PyObject * diff --git a/Objects/typeobject.c b/Objects/typeobject.c index bf2bdc0..5cc4161 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -113,8 +113,7 @@ type_compare(PyObject *v, PyObject *w) static PyObject * type_repr(PyTypeObject *type) { - PyObject *mod, *name; - char buf[200]; + PyObject *mod, *name, *rtn; mod = type_module(type, NULL); if (mod == NULL) @@ -126,15 +125,18 @@ type_repr(PyTypeObject *type) name = type_name(type, NULL); if (name == NULL) return NULL; - if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) - sprintf(buf, "", - PyString_AS_STRING(mod), - PyString_AS_STRING(name)); + + if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { + rtn = PyString_FromFormat("", + PyString_AS_STRING(mod), + PyString_AS_STRING(name)); + } else - sprintf(buf, "", type->tp_name); + rtn = PyString_FromFormat("", type->tp_name); + Py_XDECREF(mod); Py_DECREF(name); - return PyString_FromString(buf); + return rtn; } static PyObject * @@ -968,8 +970,7 @@ static PyObject * object_repr(PyObject *self) { PyTypeObject *type; - PyObject *mod, *name; - char buf[200]; + PyObject *mod, *name, *rtn; type = self->ob_type; mod = type_module(type, NULL); @@ -983,15 +984,16 @@ object_repr(PyObject *self) if (name == NULL) return NULL; if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) - sprintf(buf, "<%.80s.%.80s instance at %p>", - PyString_AS_STRING(mod), - PyString_AS_STRING(name), - self); + rtn = PyString_FromFormat("<%s.%s instance at %p>", + PyString_AS_STRING(mod), + PyString_AS_STRING(name), + self); else - sprintf(buf, "<%.80s instance at %p>", type->tp_name, self); + rtn = PyString_FromFormat("<%s instance at %p>", + type->tp_name, self); Py_XDECREF(mod); Py_DECREF(name); - return PyString_FromString(buf); + return rtn; } static PyObject * @@ -2590,13 +2592,9 @@ slot_tp_repr(PyObject *self) Py_DECREF(func); return res; } - else { - char buf[120]; - PyErr_Clear(); - sprintf(buf, "<%.80s object at %p>", - self->ob_type->tp_name, self); - return PyString_FromString(buf); - } + PyErr_Clear(); + return PyString_FromFormat("<%s object at %p>", + self->ob_type->tp_name, self); } static PyObject * -- cgit v0.12