This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes ArgumentError lists arguments from 1, not 0
Type: behavior Stage:
Components: ctypes Versions: Python 3.2
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, perey
Priority: normal Keywords:

Created on 2012-06-04 05:50 by perey, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg162251 - (view) Author: Timothy Pederick (perey) Date: 2012-06-04 05:50
The ctypes ArgumentError exception indicates the location of the problem by argument number. It counts arguments starting from 1, not 0 as is typical in Python.

Observed
--------
An example (anonymised) traceback:
    Traceback (most recent call last):
        ...
        foreign_function(a, b, c, d)
    ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

The error here was with the argument "b".

Expected
--------
Standard, zero-indexed Python counting would suggest that argument 2 should mean "c", and "b" would be argument 1.

Rationale
---------
This may be as intended, but for me it violates the principle of least surprise.

I *think* this is the vicinity of the bug:
http://hg.python.org/cpython/file/696d3631a4a1/Modules/_ctypes/callproc.c#l1103

    _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);

If I'm right and the "i+1" (here and/or in subsequent lines) is the cause, that definitely makes it look intentional.
msg199797 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-13 20:42
IMO we always use 1-based numbering when counting things.

Try e.g.

>>> format('a', None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: format expects arg 2 to be string or unicode, not NoneType
History
Date User Action Args
2022-04-11 14:57:31adminsetgithub: 59204
2013-10-13 20:42:42georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg199797

resolution: wont fix
2012-06-04 05:50:04pereycreate