diff options
author | Barry Warsaw <barry@python.org> | 2001-08-29 01:41:58 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2001-08-29 01:41:58 (GMT) |
commit | da21ce3e3109c4a45f89386db42be4f0463aec11 (patch) | |
tree | 5c81c980c8db9f6b0f5a7459e85ec2d8dbf23926 /Doc | |
parent | 0ab31b85620b1ccc2617f4eeeeb151dab4d5abda (diff) | |
download | cpython-da21ce3e3109c4a45f89386db42be4f0463aec11.zip cpython-da21ce3e3109c4a45f89386db42be4f0463aec11.tar.gz cpython-da21ce3e3109c4a45f89386db42be4f0463aec11.tar.bz2 |
On Fred's suggestion, convert sprintf() examples to use
PyString_FromFormat(). Also fixed one grammar problem, and a few
other mark-up issues. Sample code not checked.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/ext/newtypes.tex | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/Doc/ext/newtypes.tex b/Doc/ext/newtypes.tex index 1741006..b3c9f62 100644 --- a/Doc/ext/newtypes.tex +++ b/Doc/ext/newtypes.tex @@ -497,10 +497,8 @@ simple example: static PyObject * newdatatype_repr(newdatatypeobject * obj) { - char buf[4096]; - sprintf(buf, "Repr-ified_newdatatype{{size:%d}}", + return PyString_FromFormat("Repr-ified_newdatatype{{size:\%d}}", obj->obj_UnderlyingDatatypePtr->size); - return PyString_FromString(buf); } \end{verbatim} @@ -512,7 +510,7 @@ The \member{tp_str} handler is to \function{str()} what the \member{tp_repr} handler described above is to \function{repr()}; that is, it is called when Python code calls \function{str()} on an instance of your object. It's implementation is very similar to the -\member{tp_repr} function, but the resulting string is intended to be +\member{tp_repr} function, but the resulting string is intended for human consumption. It \member{tp_str} is not specified, the \member{tp_repr} handler is used instead. @@ -522,13 +520,9 @@ Here is a simple example: static PyObject * newdatatype_str(newdatatypeobject * obj) { - PyObject *pyString; - char buf[4096]; - sprintf(buf, "Stringified_newdatatype{{size:%d}}", + return PyString_FromFormat("Stringified_newdatatype{{size:\%d}}", obj->obj_UnderlyingDatatypePtr->size ); - pyString = PyString_FromString(buf); - return pyString; } \end{verbatim} @@ -610,9 +604,7 @@ an exception; if this were really all you wanted, the static int newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v) { - char buf[1024]; - sprintf(buf, "Set attribute not supported for attribute %s", name); - PyErr_SetString(PyExc_RuntimeError, buf); + (void)PyErr_Format(PyExc_RuntimeError, "Read-only attribute: \%s", name); return -1; } \end{verbatim} @@ -740,16 +732,16 @@ newdatatype_call(newdatatypeobject *obj, PyObject *args, PyObject *other) char *arg1; char *arg2; char *arg3; - char buf[4096]; + if (!PyArg_ParseTuple(args, "sss:call", &arg1, &arg2, &arg3)) { return NULL; } - sprintf(buf, - "Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n", - obj->obj_UnderlyingDatatypePtr->size, - arg1, arg2, arg3); - printf(buf); - return PyString_FromString(buf); + result = PyString_FromFormat( + "Returning -- value: [\%d] arg1: [\%s] arg2: [\%s] arg3: [\%s]\n", + obj->obj_UnderlyingDatatypePtr->size, + arg1, arg2, arg3); + printf("\%s", PyString_AS_STRING(result)); + return result; } \end{verbatim} |