Commit da21ce3e authored by Barry Warsaw's avatar Barry Warsaw

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.
parent 0ab31b85
...@@ -497,10 +497,8 @@ simple example: ...@@ -497,10 +497,8 @@ simple example:
static PyObject * static PyObject *
newdatatype_repr(newdatatypeobject * obj) newdatatype_repr(newdatatypeobject * obj)
{ {
char buf[4096]; return PyString_FromFormat("Repr-ified_newdatatype{{size:\%d}}",
sprintf(buf, "Repr-ified_newdatatype{{size:%d}}",
obj->obj_UnderlyingDatatypePtr->size); obj->obj_UnderlyingDatatypePtr->size);
return PyString_FromString(buf);
} }
\end{verbatim} \end{verbatim}
...@@ -512,7 +510,7 @@ The \member{tp_str} handler is to \function{str()} what the ...@@ -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 \member{tp_repr} handler described above is to \function{repr()}; that
is, it is called when Python code calls \function{str()} on an is, it is called when Python code calls \function{str()} on an
instance of your object. It's implementation is very similar to the 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 human consumption. It \member{tp_str} is not specified, the
\member{tp_repr} handler is used instead. \member{tp_repr} handler is used instead.
...@@ -522,13 +520,9 @@ Here is a simple example: ...@@ -522,13 +520,9 @@ Here is a simple example:
static PyObject * static PyObject *
newdatatype_str(newdatatypeobject * obj) newdatatype_str(newdatatypeobject * obj)
{ {
PyObject *pyString; return PyString_FromFormat("Stringified_newdatatype{{size:\%d}}",
char buf[4096];
sprintf(buf, "Stringified_newdatatype{{size:%d}}",
obj->obj_UnderlyingDatatypePtr->size obj->obj_UnderlyingDatatypePtr->size
); );
pyString = PyString_FromString(buf);
return pyString;
} }
\end{verbatim} \end{verbatim}
...@@ -610,9 +604,7 @@ an exception; if this were really all you wanted, the ...@@ -610,9 +604,7 @@ an exception; if this were really all you wanted, the
static int static int
newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v) newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
{ {
char buf[1024]; (void)PyErr_Format(PyExc_RuntimeError, "Read-only attribute: \%s", name);
sprintf(buf, "Set attribute not supported for attribute %s", name);
PyErr_SetString(PyExc_RuntimeError, buf);
return -1; return -1;
} }
\end{verbatim} \end{verbatim}
...@@ -740,16 +732,16 @@ newdatatype_call(newdatatypeobject *obj, PyObject *args, PyObject *other) ...@@ -740,16 +732,16 @@ newdatatype_call(newdatatypeobject *obj, PyObject *args, PyObject *other)
char *arg1; char *arg1;
char *arg2; char *arg2;
char *arg3; char *arg3;
char buf[4096];
if (!PyArg_ParseTuple(args, "sss:call", &arg1, &arg2, &arg3)) { if (!PyArg_ParseTuple(args, "sss:call", &arg1, &arg2, &arg3)) {
return NULL; return NULL;
} }
sprintf(buf, result = PyString_FromFormat(
"Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n", "Returning -- value: [\%d] arg1: [\%s] arg2: [\%s] arg3: [\%s]\n",
obj->obj_UnderlyingDatatypePtr->size, obj->obj_UnderlyingDatatypePtr->size,
arg1, arg2, arg3); arg1, arg2, arg3);
printf(buf); printf("\%s", PyString_AS_STRING(result));
return PyString_FromString(buf); return result;
} }
\end{verbatim} \end{verbatim}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment