Hard to find bugs, misleading exceptions

Ferenczi Viktor cx at cx.hu
Mon Feb 24 15:12:41 EST 2003


Recently I've debugged my source code more than an hour to find a very
instructive bug. The simplified problem is demonstrated in the following
interactive session:

ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> l=[
...  (1,2),
...  (3,4),
...  (5,6),
...  (7,8)
... ]
>>> l
[(1, 2), (3, 4), (5, 6), (7, 8)]
>>> l=[
...  (1,2),
...  (3,4),
...  (5,6)
...  (7,8)
... ]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'tuple' object is not callable
>>>

At the first sight both lists seems to be ok, but the second caused an
exception. As you probably noticed, a comma is missing in the second one.
Imagine a long list with tuples of complicated objects. Editing of the
source code (adding new line(s), copy-paste, etc) may lose at least one
comma once in a while. Most of the copy-paste problems can be prevented by
adding a comma to the last line, but if you insert a new line in the middle
of the list you can easily forget the comma.

You will got the "TypeError: 'tuple' object is not callable" exception
pointing to the first line of your list. Obviously, the real problem is not
there. The traceback points to the first line, but the real bug is in the
4th or 5th line. The error message is misleading and can easily cause hours
of hopeless debugging in the worst case. It's hard to find the location
where something is called.

In my opinion, the exception text should be more verbose when the "called"
object is an internal object that is not callable: "TypeError: 'tuple'
object is not callable, this can be caused by a lost comma". Additionally, a
syntax warning should be raised when the called object and the begining ( of
the parameter list is not in the same source line. (It's not a real syntax
error, but VERY misleading and uncommon.)

Alternatively a "bug hunter" module could be implemented to point out common
mistakes in the source code, passing the runtime exception to the module may
help.

If you had some hard to debug scenarios, please share it and it's solution
with the python community. This can save time and improve productivity of
the community as a whole. Is there a mailing list somewhere about this
problem?

- Complex -







More information about the Python-list mailing list