Laurent LAPORTE Laurent LAPORTE - 3 months ago 13
Python Question

How string formats '%' operator is implemented?

How does string formats '%' operator is implemented in CPython 2.7?

Can't find any reference in the Python documentation.

Well, I fact I found the topic: String conversion and formatting, for

PyOS_snprintf
and
PyOS_vsnprintf
. But not sure it match my question.

See also my question Optional keys in string formats using '%' operator?

Answer

The implementation is in the PyString_Format that is called by the string_mod function in Objects/stringobject.c. The latter in turn is a slot method stored in PyString_Type->tp_as_number->nb_remainder.

The functions PyOS_*snprintf are not really related to the implementation of str.__mod__.

Comments