[Python-Dev] should list's call to __len__ swallow SystemExit?

Guido van Rossum guido at python.org
Wed Jan 14 06:21:10 CET 2009


There seems to be an unconditional PyErr_Clear() in
_PyObject_LengthHint(). I think that could and should be much more
careful; it probably should only ignore AttributeErrors (though there
may be unittests to the contrary).

On Tue, Jan 13, 2009 at 8:24 PM, Dino Viehland <dinov at microsoft.com> wrote:
> We had a bug reported that effectively boils down to we're not swallowing exceptions when list calls __len__ (http://www.codeplex.com/WorkItem/View.aspx?ProjectName=IronPython&WorkItemId=20598).
>
> We can obviously make the change to catch exceptions here in IronPython even if it seems like a bad idea to me ☺  But CPython seems to catch not only normal exceptions, but also SystemExit.  It seems like there's been a move away from this so I thought I'd mention it here.  I tested it on 2.6.1 and 3.0.
>
> import sys
> class A(object):
>    def __iter__(self): return iter(range(10))
>    def __len__(self):
>        try:
>            print('exiting')
>            sys.exit(1)
>        except Exception as e:
>            print('can I catch it?', e)
>
> list(A())
>
> which prints:
>
> exiting
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list