[Python-Dev] How long the wrong type of argument should we limit (or not) in the error message (C-api)?

Nick Coghlan ncoghlan at gmail.com
Sun Dec 15 00:10:08 CET 2013


On 14 December 2013 14:56, Vajrasky Kok <sky.kok at speaklikeaking.com> wrote:
> Greetings,
>
> When fixing/adding error message for wrong type of argument in C code,
> I am always confused, how long the wrong type is the ideal?
>
> The type of error message that I am talking about:
>
> "Blabla() argument 1 must be integer not wrong_type".
>
> We have inconsistency in CPython code, for example:
>
> Python/sysmodule.c
> ===============
>         PyErr_Format(PyExc_TypeError,
>                         "can't intern %.400s", s->ob_type->tp_name);
>
> Modules/_json.c
> ============
>         PyErr_Format(PyExc_TypeError,
>                      "first argument must be a string, not %.80s",
>                      Py_TYPE(pystr)->tp_name);
>
>
> Objects/typeobject.c
> ===============
>         PyErr_Format(PyExc_TypeError,
>                      "can only assign string to %s.__name__, not '%s'",
>                      type->tp_name, Py_TYPE(value)->tp_name);
>
> So is it %.400s or %.80s or %s? I vote for %s.
>
> Other thing is which one is more preferable? Py_TYPE(value)->tp_name
> or value->ob_type->tp_name? I vote for Py_TYPE(value)->tp_name.
>
> Or this is just a matter of taste?

The idiom has shifted over time, but the preference more recently is
definitely for length limiting user provided identifiers (which are
generally type names) to limit the maximum length of error messages
(to add another variant to the mix, PEP 7 has "%.100s" in an example
about breaking long lines that happens to include reporting
TypeError).

The question should probably be addressed directly in PEP 7, and I'd
be inclined to just bless the "%.400s" variant for future code.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list