1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/********************************************
copyright 1999 McMillan Enterprises, Inc.
www.mcmillan-inc.com
modified for weave by eric jones.
*********************************************/
#include "object.h"
using namespace py;
//---------------------------------------------------------------------------
// object
//
// !! Wish I knew how to get these defined inline in object.h...
//---------------------------------------------------------------------------
object::keyed_ref object::operator [] (object& key) {
object rslt = PyObject_GetItem(_obj, key);
lose_ref(rslt);
if (!(PyObject*)rslt)
{
// don't throw error for when [] fails because it might be on left hand
// side (a[0] = 1). If the obj was just created, it will be filled
// with NULL values, and setting the values should be ok. However, we
// do want to catch index errors that might occur on the right hand side
// (obj = a[4] when a has len==3).
if (PyErr_ExceptionMatches(PyExc_KeyError))
PyErr_Clear(); // Ignore key errors
else if (PyErr_ExceptionMatches(PyExc_IndexError))
throw 1;
}
return object::keyed_ref(rslt, *this, key);
};
object::keyed_ref object::operator [] (const char* key) {
object _key = object(key);
return operator[](_key);
};
object::keyed_ref object::operator [] (const std::string& key) {
object _key = object(key);
return operator [](_key);
};
object::keyed_ref object::operator [] (int key) {
object _key = object(key);
return operator [](_key);
};
object::keyed_ref object::operator [] (double key) {
object _key = object(key);
return operator [](_key);
};
object::keyed_ref object::operator [] (const std::complex<double>& key) {
object _key = object(key);
return operator [](_key);
};
std::ostream& operator <<(std::ostream& os, py::object& obj)
{
os << obj.repr();
return os;
}
//---------------------------------------------------------------------------
// Fail method for throwing exceptions with a given message.
//---------------------------------------------------------------------------
void py::fail(PyObject* exc, const char* msg)
{
PyErr_SetString(exc, msg);
throw 1;
}
|