[IronPython] Checking for Python exception in C#

Tristan Zajonc tristanz at gmail.com
Tue Jun 8 17:26:23 CEST 2010


Hi,

I'm trying to get my head around the IronPython API by implementing a simple
work-around for what I believe is a bug in the next() builtin,which I
reported here: http://ironpython.codeplex.com/workitem/27383.  This is my
first look inside IronPython so this question, I assume, is pretty basic.
How can I handle Python exceptions returned by TryInvokeUnaryOperator?  I'm
currently trying to do this to implement the default value argument in the
next builtin.

        public static object next(CodeContext/*!*/ context, object o) {
            object value;
            if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "next", out
value)) {
                return value;
            }
            throw PythonOps.TypeError("{0} object is not an iterator.",
PythonTypeOps.GetName(o));
        }

        public static object next(CodeContext/*!*/ context, object o, object
defaultVal) {
            object value = next(context, o);
            // This doesn't work.
            if (value is StopIterationException) {
                return defaultVal;
            }
            return value;
        }

Thanks,
Tristan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100608/91387843/attachment.html>


More information about the Ironpython-users mailing list