diff options
author | Barry Warsaw <barry@python.org> | 2001-08-24 18:34:26 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2001-08-24 18:34:26 (GMT) |
commit | 7ce3694a527afe425a2b9df65c049b0ef4e75960 (patch) | |
tree | 089937f432c69e85afbfc8308d5ebc86dd2c2c49 /Objects | |
parent | dadace004b4b94dcc4437bafc9c8407fbb1bed74 (diff) | |
download | cpython-7ce3694a527afe425a2b9df65c049b0ef4e75960.zip cpython-7ce3694a527afe425a2b9df65c049b0ef4e75960.tar.gz cpython-7ce3694a527afe425a2b9df65c049b0ef4e75960.tar.bz2 |
repr's converted to using PyString_FromFormat() instead of sprintf'ing
into a hardcoded char* buffer.
Closes patch #454743.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bufferobject.c | 20 | ||||
-rw-r--r-- | Objects/cellobject.c | 12 | ||||
-rw-r--r-- | Objects/classobject.c | 33 | ||||
-rw-r--r-- | Objects/descrobject.c | 18 | ||||
-rw-r--r-- | Objects/fileobject.c | 12 | ||||
-rw-r--r-- | Objects/funcobject.c | 11 | ||||
-rw-r--r-- | Objects/methodobject.c | 14 | ||||
-rw-r--r-- | Objects/moduleobject.c | 19 | ||||
-rw-r--r-- | Objects/object.c | 10 | ||||
-rw-r--r-- | Objects/rangeobject.c | 38 | ||||
-rw-r--r-- | 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, "<cell at %p: empty>", op); - else - sprintf(buf, "<cell at %p: %.80s object at %p>", - op, op->ob_ref->ob_type->tp_name, op->ob_ref); - return PyString_FromString(buf); + return PyString_FromFormat("<cell at %p: empty>", op); + + return PyString_FromFormat("<cell at %p: %.80s object at %p>", + 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, "<class ?.%.100s at %p>", name, op); + return PyString_FromFormat("<class ?.s at %p>", name, op); else - sprintf(buf, "<class %.50s.%.50s at %p>", - PyString_AsString(mod), - name, op); - return PyString_FromString(buf); + return PyString_FromFormat("<class %s.%s at %p>", + 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, "<?.%.100s instance at %p>", - cname, inst); + return PyString_FromFormat("<?.%s instance at %p>", + 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, "<unbound method %.100s.%.100s>", - sklassname, sfuncname); + result = PyString_FromFormat("<unbound method %s.%s>", + 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, "<bound method %.60s.%.60s of %.60s>", - sklassname, sfuncname, PyString_AS_STRING(selfrepr)); + result = PyString_FromFormat("<bound method %s.%s of %s>", + 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, - "<method '%.300s' of '%.100s' objects>"); + "<method '%s' of '%s' objects>"); } static PyObject * member_repr(PyMemberDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - "<member '%.300s' of '%.100s' objects>"); + "<member '%s' of '%s' objects>"); } static PyObject * getset_repr(PyGetSetDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - "<attribute '%.300s' of '%.100s' objects>"); + "<attribute '%s' of '%s' objects>"); } static PyObject * wrapper_repr(PyWrapperDescrObject *descr) { return descr_repr((PyDescrObject *)descr, - "<slot wrapper '%.300s' of '%.100s' objects>"); + "<slot wrapper '%s' of '%s' objects>"); } 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, "<anonymous function at %p>", op); - else - sprintf(buf, "<function %.100s at %p>", - PyString_AsString(op->func_name), - op); - return PyString_FromString(buf); + return PyString_FromFormat("<anonymous function at %p>", op); + return PyString_FromFormat("<function %s at %p>", + 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, "<built-in function %.80s>", m->m_ml->ml_name); - else - sprintf(buf, - "<built-in method %.80s of %.80s object at %p>", - m->m_ml->ml_name, m->m_self->ob_type->tp_name, - m->m_self); - return PyString_FromString(buf); + return PyString_FromFormat("<built-in function %s>", + m->m_ml->ml_name); + return PyString_FromFormat("<built-in method %s of %s object at %p>", + 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("<module '' (built-in)>") + 1; - static int template2len = sizeof("<module '' from ''>") + 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, "<module '%s' (built-in)>", name); - } - else { - buf = PyObject_MALLOC( - sizeof(char) * (strlen(name) + strlen(filename) + - template2len)); - sprintf(buf, "<module '%s' from '%s'>", name, filename); + return PyString_FromFormat("<module '%s' (built-in)>", name); } - rtn = PyString_FromString(buf); - PyObject_FREE(buf); - return rtn; + return PyString_FromFormat("<module '%s' from '%s'>", 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("<NULL>"); - 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, "<type '%.80s.%.80s'>", - PyString_AS_STRING(mod), - PyString_AS_STRING(name)); + + if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { + rtn = PyString_FromFormat("<type '%s.%s'>", + PyString_AS_STRING(mod), + PyString_AS_STRING(name)); + } else - sprintf(buf, "<type '%.80s'>", type->tp_name); + rtn = PyString_FromFormat("<type '%s'>", 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 * |