[Python-Dev] PyString_FromFormat

Guido van Rossum guido at python.org
Mon Jun 19 16:59:57 CEST 2006

On 6/19/06, Kristján V. Jónsson <kristjan at ccpgames.com> wrote:
> One thing I have often lamented having in PyString_FromFormat (and cousins,
> like PyErr_Format) is to be able to integrate PyObject pointers.  Adding
> something like %S and %R (for str() and repr() respectively) seems very
> useful to me.  Is there any reason why this isn´t there?

Asking "why" a particular feature is omitted rarely is a good starting
point. We collectively probably don't remember, or misremember the
discussion if there was any; the most likely reason is simply that
nobody thought it was useful at the time, and nobody who thought it
*was* useful put enough effort in to provide the feature.

If I had to make a guess, %S and %R as you propose have the very real
possibility to fail. PyString_FromFormat() currently only fails if it
runs out of memory. This is especially helpful for PyErr_Format() --
the last thing you want to hapen during formatting of an error message
is to get an error in the formatting. But there are always ways to
handle that if the use case is strong enough.

If you want to gather use cases, you could scour the Python source
code for calls to either API immediately preceded by a call to
PyObject_Str/Repr() to produce a string to be included into the
message. If you find many, you are not alone and you have a good use

Personally, I think it's not worth the trouble. But I wouldn't
necessarily reject a patch (not that I'm in the business of accepting
individual patches any more -- others will weigh in there).

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list