summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2001-08-16 20:39:24 (GMT)
committerBarry Warsaw <barry@python.org>2001-08-16 20:39:24 (GMT)
commit2907fe6ce7376e73d84c8a29dedd37d8b3e4a225 (patch)
tree5b6037aed9b5b2e8cefce3e8f8f26eb353db3eae /Objects/moduleobject.c
parente791a6421fae40dd7aae1f67fd6b6d737c510af7 (diff)
downloadcpython-2907fe6ce7376e73d84c8a29dedd37d8b3e4a225.zip
cpython-2907fe6ce7376e73d84c8a29dedd37d8b3e4a225.tar.gz
cpython-2907fe6ce7376e73d84c8a29dedd37d8b3e4a225.tar.bz2
module_repr(): Instead of fixing the maximum buf size to 400,
calculate it on the fly. This way even modules with long package names get an accurate repr instead of a truncated one. The extra malloc/free cost shouldn't be a problem in a repr function. Closes SF bug #437984
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r--Objects/moduleobject.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 7faa3bb..2ccf0bf 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -157,9 +157,14 @@ module_dealloc(PyModuleObject *m)
static PyObject *
module_repr(PyModuleObject *m)
{
- char buf[400];
+ 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) {
PyErr_Clear();
@@ -168,12 +173,19 @@ module_repr(PyModuleObject *m)
filename = PyModule_GetFilename((PyObject *)m);
if (filename == NULL) {
PyErr_Clear();
- sprintf(buf, "<module '%.80s' (built-in)>", name);
- } else {
- sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
+ buf = PyObject_MALLOC(
+ sizeof(char) * (strlen(name) + template1len));
+ sprintf(buf, "<module '%s' (built-in)>", name);
}
-
- return PyString_FromString(buf);
+ else {
+ buf = PyObject_MALLOC(
+ sizeof(char) * (strlen(name) + strlen(filename) +
+ template2len));
+ sprintf(buf, "<module '%s' from '%s'>", name, filename);
+ }
+ rtn = PyString_FromString(buf);
+ PyObject_FREE(buf);
+ return rtn;
}
/* We only need a traverse function, no clear function: If the module