[issue29730] unoptimal calls to PyNumber_Check

Oren Milman report at bugs.python.org
Sun Mar 12 18:01:23 EDT 2017


Oren Milman added the comment:

I am sorry, but I guess I am missing something about startswith() and
endswith().
ISTM that PyTuple_Check() is already called by unicode_startswith,
unicode_endswith and _Py_bytes_tailmatch, which already raise a TypeError
with an appropriate error message
(e.g. "%s first arg must be bytes or a tuple of bytes, not %s").


I searched the codebase, and found that in most places, if PyIndex_Check(obj)
is true, then obj is described as 'integer' in error messages. rarely, obj
would be described as 'int'.
I found only two relevant TypeError messages of functions that accept an
integer type or None (BTW, I took the term 'integer type' from
https://docs.python.org/3.7/reference/atamodel.html?highlight=__index__#object.__index__.):
    - in Modules/posixmodule.c in dir_fd_converter():
      PyErr_Format(PyExc_TypeError,
                   "argument should be integer or None, not %.200s",
                   Py_TYPE(o)->tp_name);
    - in Modules/_functoolsmodule.c in lru_cache_new():
      PyErr_SetString(PyExc_TypeError, "maxsize should be integer or None");

so I changed the error messages in my patch to the form of
'argument should be ...' (which is, IMHO, much clearer).


also, now that the new PR was created, should I close the old one?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29730>
_______________________________________


More information about the Python-bugs-list mailing list