On Wed, Mar 4, 2020 at 2:58 AM Steven D'Aprano <steve@pearwood.info> wrote:
I don't know enough about the Python internals to give a definitive answer, but I'm assuming/hoping that there is a single, or at most a few, places in the interpreter that matches up arguments to formal parameters, and if there's a discrepency it currently raises TypeError. PyCall sounds promising :-)
In my experience, it's sprinkled all over the place. For example, here are the pertinent chunks of one of our functions that converts VAX floats to IEEE... static PyObject *vax_data_floats(PyObject *self, PyObject **args, Py_ssize_t nargs) { if (nargs != 2) { PyErr_Format(PyExc_TypeError, "floats: expected 2 arguments, 'bytes' and 'count'"); return NULL; } ... if (!PyLong_Check(py_count)) { PyErr_Format(PyExc_TypeError, "floats: second argument must be an int"); return NULL; } ... if (count*(Py_ssize_t)sizeof(float) > bytes_size) { PyErr_Format(PyExc_TypeError, "floats: argument 2 is larger than data (only %d bytes for requested %d floats)", bytes_size, count); return NULL; }