2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

This commit was manufactured by cvs2svn to create tag 'start'.

[SVN r2692]
This commit is contained in:
nobody
2005-08-20 22:18:16 +00:00
parent e9d0a911c3
commit 03c956b84a
2 changed files with 0 additions and 233 deletions

View File

@@ -1,168 +0,0 @@
// -*- mode:c++ -*-
//
// Module python_iterator.cpp
//
// Copyright (c) 2003 Raoul M. Gough
//
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
// at http://www.boost.org/LICENSE_1_0.txt)
//
// History
// =======
// 2003/ 9/10 rmg File creation
//
// $Id$
//
#include <boost/python/suite/indexing/python_iterator.hpp>
////////////////////////////////////////////////////////////////////////////
// python_iterator factory
///////////////////////////////////////////////////////////////////////////
std::auto_ptr<boost::python::indexing::python_iterator>
boost::python::indexing::make_iterator (boost::python::object temp)
{
std::auto_ptr<python_iterator> result;
try
{
result.reset (new python_iter_iterator (temp));
}
catch (boost::python::error_already_set const &)
{
PyErr_Clear ();
try
{
result.reset (new python_getitem_iterator (temp));
}
catch (boost::python::error_already_set const &)
{
PyErr_Clear ();
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////
// Base class (virtual) destructor
///////////////////////////////////////////////////////////////////////////
boost::python::indexing::python_iterator::~python_iterator ()
{
}
////////////////////////////////////////////////////////////////////////////
// python_getitem_iterator constructor
///////////////////////////////////////////////////////////////////////////
boost::python::indexing::python_getitem_iterator
::python_getitem_iterator (boost::python::object obj)
: mGetitemMethod (obj.attr ("__getitem__"))
, mIndex (0)
, mCurrent()
{
}
////////////////////////////////////////////////////////////////////////////
// Get our next item (if any)
///////////////////////////////////////////////////////////////////////////
bool boost::python::indexing::python_getitem_iterator::next ()
{
bool result = true; // Assume success
try
{
mCurrent = mGetitemMethod (mIndex);
++mIndex;
}
catch (boost::python::error_already_set const &)
{
if (PyErr_ExceptionMatches (PyExc_IndexError))
{
// Eat this exception
PyErr_Clear ();
mCurrent = boost::python::object ();
result = false;
}
else
{
// Pass it up the line
throw;
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////
// Return our current item
///////////////////////////////////////////////////////////////////////////
boost::python::object
boost::python::indexing::python_getitem_iterator::current () const
{
return mCurrent;
}
////////////////////////////////////////////////////////////////////////////
// python_iter_iterator constructor
///////////////////////////////////////////////////////////////////////////
boost::python::indexing::python_iter_iterator
::python_iter_iterator (boost::python::object obj)
: mNextMethod (obj.attr ("__iter__")().attr ("next"))
, mCurrent()
{
}
////////////////////////////////////////////////////////////////////////////
// Get our next item (if any)
///////////////////////////////////////////////////////////////////////////
bool boost::python::indexing::python_iter_iterator::next ()
{
bool result = true; // Assume success
try
{
mCurrent = mNextMethod ();
}
catch (boost::python::error_already_set const &)
{
if (PyErr_ExceptionMatches (PyExc_StopIteration))
{
// Eat this exception
PyErr_Clear ();
mCurrent = boost::python::object ();
result = false;
}
else
{
// Pass it up the line
throw;
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////
// Return our current item
///////////////////////////////////////////////////////////////////////////
boost::python::object
boost::python::indexing::python_iter_iterator::current () const
{
return mCurrent;
}

View File

@@ -1,65 +0,0 @@
// -*- mode:c++ -*-
//
// Module slice.cpp
//
// Copyright (c) 2003 Raoul M. Gough
//
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
// at http://www.boost.org/LICENSE_1_0.txt)
//
// History
// =======
// 2003/ 9/10 rmg File creation
//
// $Id$
//
#include <boost/python/suite/indexing/slice.hpp>
/////////////////////////////////////////////////////////////////////////////
// Check that setLength has been called, and throw otherwise
/////////////////////////////////////////////////////////////////////////////
void boost::python::indexing::slice::validate () const
{
if (mDirection == 0)
{
PyErr_SetString (PyExc_RuntimeError
, "slice access attempted before setLength called");
boost::python::throw_error_already_set();
}
}
/////////////////////////////////////////////////////////////////////////////
// Set up our member variables for a sequence of a given length
/////////////////////////////////////////////////////////////////////////////
void boost::python::indexing::slice::setLength (int sequenceLength)
{
PySlice_GetIndices ((PySliceObject *) this->ptr()
, sequenceLength
, &mStart
, &mStop
, &mStep);
if (mStep == 0)
{
// Can happen with Python prior to 2.3
PyErr_SetString (PyExc_ValueError, "slice step cannot be zero");
boost::python::throw_error_already_set ();
}
mStart = std::max (0, std::min (sequenceLength, mStart));
mStop = std::max (0, std::min (sequenceLength, mStop));
mDirection = (mStep > 0) ? 1 : -1;
}
/////////////////////////////////////////////////////////////////////////////
// Check if an index is within the range of this slice
/////////////////////////////////////////////////////////////////////////////
bool boost::python::indexing::slice::inRange (int index)
{
return ((mStop - index) * mDirection) > 0;
}