[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/sdpython/pythonnet/master/src/runtime/pyiter.cs [Back]  [Original]

using System;
using System.Collections.Generic;

namespace Python.Runtime
{
    /// 
    /// Represents a standard Python iterator object. See the documentation at
    /// PY2: https://docs.python.org/2/c-api/iterator.html
    /// PY3: https://docs.python.org/3/c-api/iterator.html
    /// for details.
    /// 
    public class PyIter : PyObject, IEnumerator
    {
        private PyObject _current;

        /// 
        /// PyIter Constructor
        /// 
        /// 
        /// Creates a new PyIter from an existing iterator reference. Note
        /// that the instance assumes ownership of the object reference.
        /// The object reference is not checked for type-correctness.
        /// 
        public PyIter(IntPtr ptr) : base(ptr)
        {
        }

        /// 
        /// PyIter factory function.
        /// 
        /// 
        /// Create a new PyIter from a given iterable.  Like doing "iter(iterable)" in python.
        /// 
        /// 
        /// 
        public static PyIter GetIter(PyObject iterable)
        {
            if (iterable == null)
            {
                throw new ArgumentNullException();
            }
            IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
            PythonException.ThrowIfIsNull(val);
            return new PyIter(val);
        }

        protected override void Dispose(bool disposing)
        {
            _current = null;
            base.Dispose(disposing);
        }

        public bool MoveNext()
        {
            NewReference next = Runtime.PyIter_Next(Reference);
            if (next.IsNull())
            {
                if (Exceptions.ErrorOccurred())
                {
                    throw new PythonException();
                }

                // stop holding the previous object, if there was one
                _current = null;
                return false;
            }

            _current = next.MoveToPyObject();
            return true;
        }

        public void Reset()
        {
            throw new NotSupportedException();
        }

        public PyObject Current => _current;
        object System.Collections.IEnumerator.Current => _current;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page