summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-03-21 12:26:24 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-03-21 12:26:24 (GMT)
commit499dfcf29da7b7d1ca11f697cece773936c2ba42 (patch)
tree5b295eac3254ac6d6f0253d037081d7456ff9d7c /Objects
parentbfc7bf06a63b5bf4cac0dae7c0d370fbc81563f9 (diff)
downloadcpython-499dfcf29da7b7d1ca11f697cece773936c2ba42.zip
cpython-499dfcf29da7b7d1ca11f697cece773936c2ba42.tar.gz
cpython-499dfcf29da7b7d1ca11f697cece773936c2ba42.tar.bz2
Issue #10833: Use PyUnicode_FromFormat() and PyErr_Format() instead of
PyOS_snprintf().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/weakrefobject.c58
1 files changed, 27 insertions, 31 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index dfaa1b9..72ecdd0 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -157,34 +157,31 @@ static PyObject *
weakref_repr(PyWeakReference *self)
{
char buffer[256];
- if (PyWeakref_GET_OBJECT(self) == Py_None) {
- PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
+ PyObject *name, *repr;
+
+ if (PyWeakref_GET_OBJECT(self) == Py_None)
+ return PyUnicode_FromFormat("<weakref at %p; dead>", self);
+
+ name = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), "__name__");
+ if (name == NULL || !PyUnicode_Check(name)) {
+ if (name == NULL)
+ PyErr_Clear();
+ repr = PyUnicode_FromFormat(
+ "<weakref at %p; to '%s' at %p>",
+ self,
+ Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
+ PyWeakref_GET_OBJECT(self));
}
else {
- char *name = NULL;
- PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
- "__name__");
- if (nameobj == NULL)
- PyErr_Clear();
- else if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
- if (name)
- PyOS_snprintf(buffer, sizeof(buffer),
- "<weakref at %p; to '%.50s' at %p (%s)>",
- self,
- Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
- PyWeakref_GET_OBJECT(self),
- name);
- else
- PyOS_snprintf(buffer, sizeof(buffer),
- "<weakref at %p; to '%.50s' at %p>",
- self,
- Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
- PyWeakref_GET_OBJECT(self));
-
- Py_XDECREF(nameobj);
+ repr = PyUnicode_FromFormat(
+ "<weakref at %p; to '%s' at %p (%U)>",
+ self,
+ Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
+ PyWeakref_GET_OBJECT(self),
+ name);
}
- return PyUnicode_FromString(buffer);
+ Py_XDECREF(name);
+ return repr;
}
/* Weak references only support equality, not ordering. Two weak references
@@ -459,12 +456,11 @@ WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
static PyObject *
proxy_repr(PyWeakReference *proxy)
{
- char buf[160];
- PyOS_snprintf(buf, sizeof(buf),
- "<weakproxy at %p to %.100s at %p>", proxy,
- Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
- PyWeakref_GET_OBJECT(proxy));
- return PyUnicode_FromString(buf);
+ return PyUnicode_FromFormat(
+ "<weakproxy at %p to %s at %p>",
+ proxy,
+ Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
+ PyWeakref_GET_OBJECT(proxy));
}