Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
This commit is contained in:
parent
c94e2b5c12
commit
c3b2a4afe8
|
@ -230,23 +230,37 @@ make_tuple(PyStructSequence *obj)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
structseq_repr(PyStructSequence *obj)
|
structseq_repr(PyStructSequence *obj)
|
||||||
{
|
{
|
||||||
PyObject *tup, *val, *repr;
|
/* buffer and type size were chosen well considered. */
|
||||||
PyTypeObject *typ = Py_TYPE(obj);
|
#define REPR_BUFFER_SIZE 512
|
||||||
int i, len;
|
#define TYPE_MAXSIZE 100
|
||||||
char buf[250+5]; /* "...)\0" */
|
|
||||||
char *cname, *crepr;
|
|
||||||
char *pbuf = buf;
|
|
||||||
char *endbuf = &buf[250];
|
|
||||||
|
|
||||||
strncpy(pbuf, typ->tp_name, 50);
|
PyObject *tup;
|
||||||
pbuf += strlen(typ->tp_name) > 50 ? 50 : strlen(typ->tp_name);
|
PyTypeObject *typ = Py_TYPE(obj);
|
||||||
*pbuf++ = '(';
|
int i, removelast = 0;
|
||||||
|
Py_ssize_t len;
|
||||||
|
char buf[REPR_BUFFER_SIZE];
|
||||||
|
char *endofbuf, *pbuf = buf;
|
||||||
|
|
||||||
|
/* pointer to end of writeable buffer; safes space for "...)\0" */
|
||||||
|
endofbuf= &buf[REPR_BUFFER_SIZE-5];
|
||||||
|
|
||||||
if ((tup = make_tuple(obj)) == NULL) {
|
if ((tup = make_tuple(obj)) == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* "typename(", limited to TYPE_MAXSIZE */
|
||||||
|
len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
|
||||||
|
strlen(typ->tp_name);
|
||||||
|
strncpy(pbuf, typ->tp_name, len);
|
||||||
|
pbuf += len;
|
||||||
|
*pbuf++ = '(';
|
||||||
|
|
||||||
for (i=0; i < VISIBLE_SIZE(obj); i++) {
|
for (i=0; i < VISIBLE_SIZE(obj); i++) {
|
||||||
|
PyObject *val, *repr;
|
||||||
|
char *cname, *crepr;
|
||||||
|
|
||||||
cname = typ->tp_members[i].name;
|
cname = typ->tp_members[i].name;
|
||||||
|
|
||||||
val = PyTuple_GetItem(tup, i);
|
val = PyTuple_GetItem(tup, i);
|
||||||
if (cname == NULL || val == NULL) {
|
if (cname == NULL || val == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -262,8 +276,10 @@ structseq_repr(PyStructSequence *obj)
|
||||||
Py_DECREF(repr);
|
Py_DECREF(repr);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* + 3: keep space for "=" and ", " */
|
||||||
len = strlen(cname) + strlen(crepr) + 3;
|
len = strlen(cname) + strlen(crepr) + 3;
|
||||||
if ((pbuf+len) < endbuf) {
|
if ((pbuf+len) <= endofbuf) {
|
||||||
strcpy(pbuf, cname);
|
strcpy(pbuf, cname);
|
||||||
pbuf += strlen(cname);
|
pbuf += strlen(cname);
|
||||||
*pbuf++ = '=';
|
*pbuf++ = '=';
|
||||||
|
@ -271,23 +287,26 @@ structseq_repr(PyStructSequence *obj)
|
||||||
pbuf += strlen(crepr);
|
pbuf += strlen(crepr);
|
||||||
*pbuf++ = ',';
|
*pbuf++ = ',';
|
||||||
*pbuf++ = ' ';
|
*pbuf++ = ' ';
|
||||||
|
removelast = 1;
|
||||||
Py_DECREF(repr);
|
Py_DECREF(repr);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
strcpy(pbuf, "...");
|
strcpy(pbuf, "...");
|
||||||
pbuf += 5;
|
pbuf += 3;
|
||||||
|
removelast = 0;
|
||||||
Py_DECREF(repr);
|
Py_DECREF(repr);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Py_DECREF(tup);
|
Py_DECREF(tup);
|
||||||
|
if (removelast) {
|
||||||
|
/* overwrite last ", " */
|
||||||
pbuf-=2;
|
pbuf-=2;
|
||||||
|
}
|
||||||
*pbuf++ = ')';
|
*pbuf++ = ')';
|
||||||
*pbuf = '\0';
|
*pbuf = '\0';
|
||||||
|
|
||||||
repr = PyString_FromString(buf);
|
return PyString_FromString(buf);
|
||||||
return repr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
Loading…
Reference in New Issue